3

I'd like to be able to tell an active process to open a file using the Windows command line ideally -- but any solution using built-in Windows (7+) capabilities (eg powershell or vbscript) also works.

For example, if I have an instance of notepad open and know its processid is 1234, it would be great to be able to do something like:

notepad.exe "myfile.txt" /pid=1234

and have notepad try to open the file in the window for the existing process.

Is this possible?

Mac
  • 31
  • 2
  • For Notepad, a program that has no external control (unlike IE, Word, Autocad, etc) you can use VBS's `wshshell.appactivate 1234` and `wshshell.sendkeys "%FO" & "filename.ext"`. This is a flakey way to do things. –  May 20 '16 at 23:10

2 Answers2

0

You should use Autohotkey for this. Although this can be done in powershell too it is way more complicated then in specialized tool. In AHK you would simulate open file (the usual shortcut Ctrl + O), then AHK would recognize dialog and insert file name and ENTER at the end. That would handle number of standardized applications but you would still need to handle some unusual applications on case by case basis.

majkinetor
  • 8,730
  • 9
  • 54
  • 72
0

As a generic way that works for all applications that open files this is not possible. The program would have to expose some interface (like a COM interface) or listen to specific windows messages or network traffic and your controller would have to know exactly what method to call or what message to send to trigger the desired behavior.

Even for not running processes it is not guaranteed that the first comnand line argument will be interpreted as the name of a file that should be opened. However, in the pre-GUI days there was no other way so users have been expecting this for a long time. Hence this is considered basic behavior and any document oriented application will support it. This is not the case with your scenario and it would raise some issues too. With a multiple document UI, it could work. With a single document UI... probably not a good idea. Should the current document be discarded? Appended to? Allow the user to save yes, save no or cancel?

Martin Maat
  • 714
  • 4
  • 23