Adobe Reader: get exe/pdf path

Post your working scripts, libraries and tools
User avatar
jeeswg
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
return

;==================================================

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
return

;==================================================

e:: ;Recent folder - get paths (pdfs)
;CSIDL_RECENT := 8
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
return

;==================================================

r:: ;Adobe Reader - get pdf from Document Properties dialog
if !(hWnd := WinExist("ahk_class AcrobatSDIWindow"))
	return
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
	return
}
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
return

;==================================================

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"))
	return
WinGetTitle, vWinTitle, % "ahk_id " hWnd
if (vWinTitle ~= " - Adobe Reader$")
	vName2 := SubStr(vWinTitle, 1, -15)
else
	vName2 := vWinTitle

vRootKey := "HKEY_CURRENT_USER"
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
return
If you have the full version of Adobe Reader, then you could use COM:
Retrieve filename and pagenumber from PDF - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=12377
Using COM with PDF files - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=8260
PDF file's current page No. ? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=33320

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
https://autohotkey.com/boards/viewtopic.php?f=7&t=33596
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