Page 1 of 1

Add PathToOpenedFile option to Process command

Posted: 30 Mar 2018, 14:41
by julesverne
Request: Adding another option to ahk Process command to retrieve the path of the file that is opened by the process. Not the process itself. I have found some code that may help make this reality possible.

So, recently I had to figure out how to discover the path of a file that was opened in PowerPoint (NOT the path of the PowerPoint executable). I eventually found this code:

Code: Select all

PPTvariable := ComObjActive("PowerPoint.Application").ActivePresentation.FullName
It worked well however it often would throw errors. One was a nul error due to the time between the PowerPoint process being started and the ActivePresentation being loaded, so I wouldn't get the path. Secondly was that the ahk script needed to run as admin and sometimes PowerPoint wasn't running as admin, so I would get an error and no path.

Obviously, Process command would be cool, however, it does not give the path of the opened file, it only gives the path to the process.

Eventually I found the answer in the forums here: https://autohotkey.com/boards/viewtopic.php?t=25530 that works almost as immediately as the process is started.

The Code is:

Code: Select all

Process, Exist, POWERPNT.EXE
If (Errorlevel <> 0)
	{
		NewPID = %ErrorLevel%
		; will get full path name of the currently open file.
		 wmi := ComObjGet("winmgmts:")
		; Run query to retrieve matching process(es).
		queryEnum := wmi.ExecQuery(""
			. "Select * from Win32_Process where ProcessId=" . NewPID)
			._NewEnum()
		; Get first matching process.
		if queryEnum[process]
		{
			haystack := StrSplit(process.CommandLine,"`"`"")[4]
		}
	}
Would it be possible to add this code into the process command as a separate option? Obviously it would start from the wmi line, rather than the first line of code but I left all that in there so you could have a simple test to start with. the haystack line splits the command via parenthesis and targets the 4th token as that is the path to the opened file.

Re: Add PathToOpenedFile option to Process command

Posted: 31 Mar 2018, 01:49
by lexikos
This is not possible in a general sense. Each program is different, and most of them don't provide any way to programmatically determine which file is open. Reading the command line is not reliable. There is no guarantee that the file specified on the command line (if there is one) for a given process was "opened" in any relevant sense, and certainly no guarantee that it is still open. In PowerPoint, or even Notepad, one can open a new file at any time via the GUI.

If you want the active PowerPoint file, you have some simple (but unreliable, as you noted) code for that. If you want whatever file was passed on the command line, you have some code for that. That's as good as its going to get.

Re: Add PathToOpenedFile option to Process command

Posted: 31 Mar 2018, 05:54
by julesverne
Hi lexicos,

My apologies, I made a big mistake about which command I was suggesting to add an additional option. I meant WinGet. Not Process. I'm not sure if that changes your answer or not.

I figured since WinGet already offers the process executable path as an output option, based on WinTitle, offering the open file path as an option, may be a useful option as well.

Again, my apologies for mixing the two commands up. I must've been pretty tired yesterday morning.

Thanks!
Jules

Re: Add PathToOpenedFile option to Process command

Posted: 31 Mar 2018, 05:56
by julesverne
And since WinGet can be searched to match by PID I thought that might be the hook.

Re: Add PathToOpenedFile option to Process command

Posted: 31 Mar 2018, 10:52
by jeeswg
I've listed various techniques for getting the path of the opened file here:

Get path of active window's directory - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 07#p177407