Add PathToOpenedFile option to Process command
Posted: 30 Mar 2018, 14:41
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:
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:
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.
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
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]
}
}