#include-once ; Holds the name of acrobat Global $AcrobatName Global $acroLog = @AppDataDir & "\AcrobatLog.log" ; Set the Acrobat name __AcrobatPro_InitGetAcrobatName() Global Const $AcrobatPageEdit = "[CLASS:Edit; INSTANCE:2]" ; #FUNCTION# ==================================================================================================================== ; Name...........: _AcrobatPro_ExtractPages ; Description ...: This takes the first and last page, the filePath + fileName to save it as, and whether to delete the pages ; Syntax.........: _AcrobatPro_ExtractPages($firstPage, $lastPage, $FilePathName, $DeletePages) ; Parameters ....: ; Return values .: Success - Returns True ; Failure - Returns false and sets @error ; |1 - Couldn't get Acrobat ; |2 - Couldn't get the position of the sidebar ; Author ........: Seadoggie ; Modified.......: 3/5/2019 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _AcrobatPro_ExtractPages($firstPage, $lastPage, $FilePathName, $DeletePages) Local $acrobat ;<-- The handle of acrobat Local $WIN_POS_X ;<-- The x-position of Acrobat Local $WIN_POS_Y ;<-- The y-position of Acrobat Local $controlPos ;<-- The position of the extract button Local $extract ;<-- The handle of the extract popup Local $totalPages ;<-- The number of pages according to extract popup Local $confirmation ;<-- The handle of a confirmation dialog (Delete pages?) Local $newFiles = "" ;<-- The handle of the second acrobat application Local $WinSaveAs ;<-- The handle of a windows Save As dialog Local $MouseX ;<-- The x-position of the mouse Local $MouseY ;<-- The y-position of the mouse ;_DebugSetup() ; Make sure acrobat is open still $acrobat = WinActivate("[CLASS:AcrobatSDIWindow]","") WinWaitActive($acrobat, "", 2) if @error Then __AcrobatPro_ErrMsg("Error activating Acrobat", @error) Return SetError(1, 0, False) EndIf $WIN_POS_X = WinGetPos($acrobat,"")[0] + 30 $WIN_POS_Y = WinGetPos($acrobat,"")[1] if $WIN_POS_Y = -8 Then $WIN_POS_Y = 42 Else $WIN_POS_Y += 42 EndIf ; Get the side bar's position $controlPos = ControlGetPos($acrobat, "", "[CLASS:AVL_AVView; INSTANCE:9]") if @error Then __AcrobatPro_ErrMsg("Couldn't get control's position", @error) Return SetError(2, 0, False) ; If the width is 0 or if we're referencing the wrong control ElseIf ($controlPos[2] = 0) Or (ControlGetText($acrobat, "", "[CLASS:AVL_AVView; INSTANCE:9]") = "AVSplitterView") Then __AcrobatPro_Debug("I think the sidebar is closed... Attempting to open") Sleep(500) SendKeepActive($acrobat,"") ; Send Alt + V to open view menu Send("!v") ; Send s, n, p to open Page Thumbnails Send("snp") Sleep(250) $controlPos = ControlGetPos($acrobat, "", "[CLASS:AVL_AVView; INSTANCE:9]") if @error Then __AcrobatPro_ErrMsg("Couldn't get control's position", @error) Return SetError(2, 0, False) ElseIf $controlPos[2] = 0 Then __AcrobatPro_ErrMsg("Sidebar is still closed.") Return SetError(2, 0, False) EndIf EndIf ; Save the Mouse's position $MouseX = MouseGetPos(0) $MouseY = MouseGetPos(1) ; Click to open popup window MouseClick("left", $WIN_POS_X + 50, $WIN_POS_Y + 110, 1, 1) ; Move right back to where the mouse was MouseMove($MouseX, $MouseY, 1) __AcrobatPro_Debug("Waiting for popup") ; Wait for the popup window ;~ $popup = WinWait("[CLASS:#32768]","") SendKeepActive($acrobat, "") Send("x") ;<-- eXtract SendKeepActive("","") __AcrobatPro_Debug("Waiting for Extract") ; Wait for Extract Window $extract = WinWaitActive("[TITLE:Extract Pages; CLASS:#32770]","") ; Get the total number of pages in the PDF $totalPages = StringReplace(ControlGetText($extract, "", "[CLASS:Static; INSTANCE:3]"), "of ", "") ; If we're trying to extract all the pages If $totalPages = ($lastPage - $firstPage + 1) Then __AcrobatPro_Debug("Trying to extract remaining pages, clicking cancel") ControlClick($extract, "", "[CLASS:Button; INSTANCE:4]") __AcrobatPro_Debug("Clicked, waiting max 3 seconds to close") WinWaitClose($extract, "", 3) __AcrobatPro_Debug("Waiting for acrobat to be active again.") WinActivate($acrobat,"") WinWaitActive($acrobat,"") else ; If DeletePages doesn't match the current selection, toggle it If Not ($DeletePages = ControlCommand($extract, "", "[CLASS:Button; INSTANCE:1]", "IsChecked")) Then ControlClick($extract, "", "[CLASS:Button; INSTANCE:1]") EndIf ; Set the Edits ControlSetText($extract, "", "[CLASS:RICHEDIT50W; INSTANCE:1]", $firstPage) ControlSetText($extract, "", "[CLASS:RICHEDIT50W; INSTANCE:2]", $lastPage) ; Click OK ControlClick($extract, "", "[CLASS:Button; INSTANCE:3]") ; If we're deleting If $DeletePages Then __AcrobatPro_Debug("Waiting for Acrobat popup") ; Wait for confirmation $confirmation = WinWaitActive("[TITLE:Adobe Acrobat; CLASS:#32770]", "") ; Ignore confirmation of deleting pages ControlClick($confirmation, "", "[CLASS:Button; INSTANCE:2]") EndIf __AcrobatPro_Debug("Waiting for new acrobat called: " & "Pages from " & StringReplace(WinGetTitle($acrobat,""), ".pdf - " & $AcrobatName , ".*")) $newFiles = WinWait("[REGEXPTITLE:(Pages from " & StringReplace(WinGetTitle($acrobat,""), ".pdf - " & $AcrobatName , ".*)]"),"") WinActivate($newFiles,"") WinWaitActive($newFiles,"") SendKeepActive($newFiles,"") EndIf ; Send Ctrl+Shift+s <-- Save As Send("^S") __AcrobatPro_Debug("Waiting for Save as Popup") $WinSaveAs = WinWaitActive("Save As","") WinActivate($WinSaveAs, "") ; Send the path to save the file as If Not ControlSetText($WinSaveAs, "", "[CLASS:Edit; INSTANCE:1]", $FilePathName) Then __AcrobatPro_Debug("Unable to set [CLASS:Edit; INSTANCE:1]") If @error Then __AcrobatPro_Debug("Couldn't EditPaste") __AcrobatPro_Debug($FilePathName) ; Click Save ControlClick($WinSaveAs, "", 1) Local $overWrite = WinWait("[TITLE:Save As; CLASS:#32770]", "", 3) ; If $overWrite is a handle if IsHWnd($overWrite) Then ; click yes ControlClick($overWrite, "", 6) EndIf __AcrobatPro_Debug("Waiting for Save as popup to die") ; Wait for it to disappear WinWaitClose($WinSaveAs) ; Close acrobat's double if $newFiles <> "" Then WinClose($newFiles) Return SetError(0, 0, True) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _AcrobatPro_SaveAs ; Description ...: Saves the specified PDF as the file path + name, optionally overwritting ; Syntax.........: _AcrobatPro_SaveAs($filePathName, [$overWrite = False]) ; Parameters ....: ; Return values .: Success - Returns True ; Failure - Returns false and sets @error ; |1 - Couldn't get Acrobat ; |2 - Couldn't get the position of the sidebar ; Author ........: Seadoggie ; Modified.......: 3/5/2019 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _AcrobatPro_SaveAs($filePathName, $overWrite = False) ;~ Local $comboBox = "[CLASS:Edit; INSTANCE:1]" Local $acrobat = WinWait("[REGEXPTITLE:(?i)(" & $AcrobatName & ")]","") SendKeepActive($acrobat, "") ; Send save as Send("^+s") SendKeepActive("","") Local $saveAs = WinWait("[TITLEREGEXP:(?i).*Save As.*; CLASS:#32770]","") WinActivate($saveAs, "") WinWaitActive($saveAs, "", 1) Send($filePathName & "{ENTER}") If WinWait("[TITLE:Save As; CLASS:#32770]", "", 3) <> 0 Then Send($overWrite ? "y" : "n") EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _AcrobatPro_GetAcrobatName ; Description ...: Gets the local name for Acrobat's Window ; Syntax.........: _AcrobatPro_GetAcrobatName() ; Parameters ....: ; Return values .: $AcrobatName ; Author ........: Seadoggie ; Modified.......: 7/8/2019 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _AcrobatPro_GetAcrobatName($FileName = "") __AcrobatPro_Debug("[REGEXPTITLE:.*\Q" & $FileName & "\E.*" & $AcrobatName & ".*]") Return "[REGEXPTITLE:.*\Q" & $FileName & "\E.*" & $AcrobatName & ".*]" EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _AcrobatPro_SetPage ; Description ...: Sets the page of the current Acrobat window ; Syntax.........: _AcrobatPro_SetPage($PageNumber) ; Parameters ....: ; Return values .: True or False and @error = 1 if value is greater than page count ; Author ........: Seadoggie ; Modified.......: 7/8/2019 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _AcrobatPro_SetPage($pageNum) Local $acrobat = __AcrobatPro_MakeAcrobatWindowActive() Local $__temp_Result = ControlSetText($acrobat, "", $AcrobatPageEdit, $pageNum) ControlSend($acrobat, "", $AcrobatPageEdit, "{ENTER}") If __AcrobatPro_WinDelayExists("[TITLE:Adobe Acrobat; CLASS:#32770]", "", 250) Then SendKeepActive($acrobat,"") Send("{ENTER}") SendKeepActive("","") Return SetError(1, 0, False) EndIf Return $__temp_Result EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _AcrobatPro_GetPage ; Description ...: Gets the page of the current Acrobat window ; Syntax.........: _AcrobatPro_GetPage() ; Parameters ....: ; Return values .: See ControlGetText ; Author ........: Seadoggie ; Modified.......: 7/8/2019 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _AcrobatPro_GetPage() Local $acrobat = __AcrobatPro_MakeAcrobatWindowActive() ; Return the exact result of the ControlGetText Return SetError(@error, @extended, ControlGetText($acrobat, "", $AcrobatPageEdit)) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _AcrobatPro_GetCurrentPageText ; Description ...: Gets the text of the current page in the current Acrobat window ; Syntax.........: _AcrobatPro_GetCurrentPageText() ; Parameters ....: ; Return values .: $PageText ; Author ........: Seadoggie ; Modified.......: 7/8/2019 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _AcrobatPro_GetCurrentPageText() Local $__oldClipValue = ClipGet() Local $__acrobatRegExp = __AcrobatPro_RegExpAcrobatName() If Not WinActive($__acrobatRegExp, "") Then __AcrobatPro_Debug("Waiting for Acrobat") WinActivate($__acrobatRegExp, "") WinWaitActive($__acrobatRegExp, "") EndIf __AcrobatPro_Debug("Acrobat is ready") SendKeepActive($__acrobatRegExp, "") ; Select the whole page Send("^a") Sleep(50) ; Copy the text Send("^c") ; Wait for the clipboard to be populated Sleep(150) ; Get the text to return Local $text = ClipGet() ClipPut($__oldClipValue) return $text EndFunc ; ================================================ #INTERNAL USE ONLY =========================================================== Func __AcrobatPro_MakeAcrobatWindowActive() Local $__acrobatRegExp = __AcrobatPro_RegExpAcrobatName() If Not WinActive($__acrobatRegExp, "") Then __AcrobatPro_Debug("Making acrobat active, waiting for " & $__acrobatRegExp) WinActivate($__acrobatRegExp, "") WinWaitActive($__acrobatRegExp, "") __AcrobatPro_Debug("Window is active now") EndIf Return WinGetHandle($__acrobatRegExp, "") EndFunc Func __AcrobatPro_RegExpAcrobatName() __AcrobatPro_Debug("RegExp: REGEXPTITLE:.*" & $AcrobatName & ".*]") Return "[REGEXPTITLE:.*" & $AcrobatName & ".*]" EndFunc Func __AcrobatPro_InitGetAcrobatName() If FileExists("C:\Program Files\Adobe\Acrobat 2015\Acrobat\Acrobat.exe") Or _ FileExists("C:\Program Files (x86)\Adobe\Acrobat 2015\Acrobat\Acrobat.exe") Then $AcrobatName = "Adobe Acrobat Pro DC" ElseIf FileExists("C:\Program Files\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe") Or _ FileExists("C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe") Then $AcrobatName = "Adobe Acrobat Pro" Else ConsoleWrite("I don't know where your version of Acrobat is, please edit the code") $AcrobatName = "Adobe Acrobat" EndIf EndFunc Func __AcrobatPro_Debug($msg, $prefix = "+ ") If Not @Compiled Then ConsoleWrite($prefix & " " & $msg & @CRLF) If FileExists($acroLog) Then FileWrite($acroLog, $msg) EndFunc Func __AcrobatPro_ErrMsg($msg, $error = "") __AcrobatPro_Debug("! " & $msg & " Error: " & $error) EndFunc Func __AcrobatPro_WinDelayExists($title, $text, $timeout) Sleep($timeout) Return WinExists($title, $text) EndFunc