1

I'm trying to write an Applescript script that will open a pdf agile.pdf with PDF Expert and search for a page number.

I'm getting close with the following script:

do shell script "open agile.pdf"
delay 1.0

tell application "System Events" 
  tell application process "PDF Expert"
    click menu item "Go to Page..."  of menu 1 of menu bar item "Go"  of menu bar 1
    keystroke "42"
    key code 76
  end tell
end tell

The problem is my script as it is will only work if I've already "clicked into" a PDF Expert window.

Is it possible for me to tell Applescript to "click into the PDF Expert window after it opens?

Also, it is possible for me to tell Applescript to "wait until the window exists?

I was reading another answer that makes it look like the following will work but it doesn't:

repeat until window "agile" of process "PDF Expert" exists
end repeat

I've done some poking around with UIElementInspector as recommended in another answer. This is the output of the PDF Expert window:

<AXApplication: “PDF Expert”>
 <AXWindow: “agile”>
  <AXSplitGroup>
   <AXScrollArea>

Attributes:
   AXHorizontalScrollBar:  “<AXScrollBar>”
   AXParent:  “<AXSplitGroup>”
   AXChildren:  “<array of size 5>”
   AXFocused (W):  “1”
   AXSize:  “w=892 h=649”
   AXRole:  “AXScrollArea”
   AXTopLevelUIElement:  “<AXWindow: “agile”>”
   AXHelp:  “(null)”
   AXChildrenInNavigationOrder:  “<array of size 5>”
   AXPosition:  “x=292 y=-803”
   AXWindow:  “<AXWindow: “agile”>”
   AXRoleDescription:  “scroll area”
   AXVerticalScrollBar:  “<AXScrollBar>”
   AXContents:  “<array of size 3>”
   AXFrame:  “x=292 y=-803 w=892 h=649”

Actions:
   AXScrollLeftByPage - scroll left by a page
   AXScrollRightByPage - scroll right by a page
   AXScrollUpByPage - scroll up by a page
   AXScrollDownByPage - scroll down by a page
  • I don't have the app to test with, but tell application process "PDF Expert" to activate will make sure it comes to the front, & also will incidentally drop the other commands back to System Events. – Tetsujin Feb 20 '17 at 9:05
  • Thank you for the response, can you please provide an example. I don't know where to put your line in my code. – mbigras Feb 20 '17 at 9:33
  • I need to somehow tell Applescript to "click on the PDF Expert window" otherwise PDF Expert won't respond to the rest of the commands. So far activate application "PDF Expert" doesn't work. – mbigras Feb 20 '17 at 9:38
  • Why using a shell script to open the file; doesn't PDFExpert support the open command? You might check that in the dictionary of PDFExpert. It also should fully support the activate command. – Max Wyss Feb 20 '17 at 23:42
1

PDF Exper and keystroke "42" doesn't work for me.

However, using Skim and key code works:

do shell script "open -a Skim agile.pdf"
delay 0.1

tell application "System Events" 
  tell application process "Skim"
    tell application "System Events" to tell process "Skim" to click menu item "Go to Page…" of menu "Go" of menu bar 1
    delay 0.1
    key code {21, 19, 36}
  end tell
end tell
0

This should work for you

do shell script "open agile.pdf"
delay 1.0
activate application "PDF Expert"
delay 1 -- you may need to adjust this
tell application "System Events"
    tell process "PDF Expert"
        click menu item "Go to Page..." of menu 1 of menu bar item "Go" of menu bar 1
    end tell
end tell
  • Doesn't work, for some reason PDF Expert doesn't recognize Applescript commands unless it's been "clicked" on. I'm experimenting with Skim seems to be working better. – mbigras Feb 20 '17 at 10:10
  • That is quite odd because I just downloaded PDF expert and ran the script myself and it works.. You might want to try adjusting the delay times. I ran the script without your "do shell script command" I had a PDF expert already opened with a document opened. Then I ran the script. – wch1zpink Feb 20 '17 at 10:11
  • So strange...definitely only works for me if I first click into the window and then run the script. I'm also not able to keystroke "123", I have to key code {18,19,20} – mbigras Feb 20 '17 at 10:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .