I need to create an Applescript to open a certain website and export it as PDF using the menu "Save as PDF…" under the File menu. Then, I need to save that PDF on a certain location on my hard drive. I am encountering trouble trying to activate the dropdown menu in the first place.

I am trying this code but I get an error:

tell application "Safari" to open location "http://www.webiste.com/"
delay 1
    click menu item "Save as PDF..." of menu bar item "File"        
    end tell
end tell

I am adding a delay 1 to make sure all elements are loaded before firing the "Save as PDF..." menu. I haven't yet reached the point of determining the specific location where I want that PDF to be stored. I will use Automator to create a recurring event based on this apple script. Any ideas?

  • My menu says Export as PDF, not Save. It also has an ellipsis … not three stops ... [there is a distinct difference].
    – Tetsujin
    Jul 27, 2020 at 18:02
  • Sorry, my bad. It is "Export as PDF..." I corrected the ellipsis too but still getting the error on the words menu item: Syntax Error: Expected end of line, etc. but found class name. Jul 27, 2020 at 18:06
  • My Applescript it s bit rusty. I'd set a key command to the menu item instead, then all you need to do with the script is hit the key combo.
    – Tetsujin
    Jul 27, 2020 at 18:11

2 Answers 2


AppleScript doesn't know about menu items, so you will need to use an application that does, such as System Events. Menus can get confusing since menu items can also have menus (take a look at the scripting dictionary), but your command would look something like:

tell application "Safari"
   open location "http://www.website.com/" -- this command is from Standard Additions
   activate -- bring Safari to the front if it isn't already
end tell
delay 2
tell application "System Events" to click menu item "Export as PDF…" of menu "File" of menu bar item "File" of menu bar 1 of application process "Safari"
  • Thank you, red_manace. The only issue I had was this: "System Events got an error: Script Editor is not allowed assistive access". I Only hade to add access in the Accessibility tab to the Script Editor app. Now I need to figure out how to set the destination path and then press the "save button" in the dialog. – Jul 27, 2020 at 20:22

Tested under macOS High Sierra, here is how I do it:

set saveToLocation to "~/Documents"
set myURL to "http://www.webiste.com/"

tell application "Safari"
    make new document with properties {URL:myURL}
end tell

tell application "System Events"
    repeat until exists (buttons of ¬
        UI elements of ¬
        groups of ¬
        toolbar 1 of ¬
        window 1 of ¬
        process "Safari" whose ¬
        name = "Reload this page")
        delay 0.5
    end repeat
    tell application process "Safari"
        click menu item "Export as PDF…" of ¬
            menu "File" of ¬
            menu bar item "File" of ¬
            menu bar 1
        repeat until exists sheet 1 of window 1
            delay 0.05
        end repeat
        keystroke "g" using {shift down, command down}
        repeat until exists sheet 1 of sheet 1 of window 1
            delay 0.05
        end repeat
        set value of ¬
            combo box 1 of ¬
            sheet 1 of ¬
            sheet 1 of ¬
            window 1 to ¬
        click button "Go" of sheet 1 of sheet 1 of window 1
        click button "Save" of sheet 1 of window 1
    end tell
end tell

Note: The example AppleScript code is just that and does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

  • Will try to work around your idea. Thank you so much! Jul 28, 2020 at 1:29

You must log in to answer this question.

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