Adobe Reader: get exe/pdf path

Post your working scripts, libraries and tools
User avatar
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Adobe Reader: get exe/pdf path

02 Apr 2018, 14:50

Some approaches for getting the path of the exe/document in Adobe Reader.
- get exe path
- get command line arguments (this may or may not contain a document path in the parameters) (note: in the command line, any paths are usually the *first* file to be opened, but not necessarily the *current* file that is open)
- list pdfs in the Recent folder
- get pdf path by temporarily showing the Document Properties dialog
- look up the name in the registry based on the window title (note: if the title bar shows the file name (e.g. MyFile.pdf), the script will work, otherwise if the title bar shows a custom title, the script won't work)

Code: Select all

q:: ;Adobe Reader - get exe path
WinGet, vPPath, ProcessPath, ahk_class AcrobatSDIWindow
Clipboard := vPPath
MsgBox, % vPPath


w:: ;Adobe Reader - get command line arguments
WinGet, vPID, PID, ahk_class AcrobatSDIWindow
oWMI := ComObjGet("winmgmts:")
oQueryEnum := oWMI.ExecQuery("Select * from Win32_Process where ProcessId=" vPID)._NewEnum()
if oQueryEnum[oProcess]
	vCmdLn := oProcess.CommandLine
oWMI := oQueryEnum := oProcess := ""
Clipboard := vCmdLn
MsgBox, % vCmdLn


e:: ;Recent folder - get paths (pdfs)
VarSetCapacity(vDirRecent, 260*2, 0)
DllCall("shell32\SHGetFolderPath", Ptr,0, Int,8, Ptr,0, UInt,0, Str,vDirRecent)
;MsgBox, % vDirRecent
vOutput := ""
Loop, Files, % vDirRecent "\*.lnk", F
	vPath := A_LoopFileFullPath
	FileGetShortcut, % vPath, vTarget
	SplitPath, vTarget, vName, vDir, vExt, vNameNoExt, vDrive
	if (vExt = "pdf")
		vOutput .= vTarget "`r`n"
Clipboard := vOutput
MsgBox, % vOutput


r:: ;Adobe Reader - get pdf from Document Properties dialog
if !(hWnd := WinExist("ahk_class AcrobatSDIWindow"))
BlockInput, On
PostMessage, 0x111, 6024,,, % "ahk_id " hWnd ;WM_COMMAND := 0x111 ;Properties...
;ControlSend, ahk_parent, {LCtrl down}{d}{LCtrl up}, % "ahk_id " hWnd ;Properties...
vDHW := A_DetectHiddenWindows
DetectHiddenWindows, Off
WinWait, Document Properties ahk_class #32770,, 3
DetectHiddenWindows, % vDHW
if ErrorLevel
	BlockInput, Off
hWnd2 := WinExist()
ControlGetText, vNameNoExt, Static2, % "ahk_id " hWnd2
ControlGetText, vDir, Static18, % "ahk_id " hWnd2
ControlSend,, {Esc}, % "ahk_id " hWnd2
BlockInput, Off
vPath := RTrim(vDir, "\") "\" vNameNoExt ".pdf"
Clipboard := vPath
MsgBox, % vPath


t:: ;Adobe Reader - get path via registry (only works if title bar shows filename: some pdfs show a custom title, some show the filename e.g. 'MyFile.pdf')
if !(hWnd := WinExist("ahk_class AcrobatSDIWindow"))
WinGetTitle, vWinTitle, % "ahk_id " hWnd
if (vWinTitle ~= " - Adobe Reader$")
	vName2 := SubStr(vWinTitle, 1, -15)
	vName2 := vWinTitle

vSubKey := "Software\Adobe\Acrobat Reader\11.0\AVGeneral\cRecentFiles"
vLen := StrLen(vName2)

vOutput := ""
Loop, 100
	RegRead, vPath, % vRootKey "\" vSubKey "\c" A_Index, tDIText
	if RegExMatch(vPath, "i)^/[A-Z]/")
		vPath := SubStr(vPath, 2, 1) ":" SubStr(vPath, 3)
	vPath := StrReplace(vPath, "/", "\")
	SplitPath, vPath, vName, vDir, vExt, vNameNoExt, vDrive

	if (SubStr(vName, 1, vLen) = vName2)
	|| (SubStr(vName, 1, vLen+1) = vName2 ".")
		vOutput .= vPath "`r`n"
Clipboard := vOutput
MsgBox, % vOutput
If you have the full version of Adobe Reader, then you could use COM:
Retrieve filename and pagenumber from PDF - AutoHotkey Community
Using COM with PDF files - AutoHotkey Community
PDF file's current page No. ? - AutoHotkey Community

I had wondered whether I could retrieve the window title and get various titles from pdf files via GetDetailsOf, and compare them, however, it appears that the title information for a pdf is not accessible this way, even though you can see 'Title' info in the 'PDF' tab, when you view the properties for a pdf file. Possibly there is another way to programmatically retrieve the title for a pdf.

The Xpdf command-line tools (e.g. pdftotext.exe, pdfinfo.exe) allow you to perform various actions on a pdf. You can retrieve command-line output text by using the JEE_RunGetStdOut function here:
jeeswg's documentation extension tutorial - AutoHotkey Community
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Scripts and Functions”

Who is online

Users browsing this forum: kczx3, robodesign and 27 guests