Selecting a dropdown menu in Safari

applescriptsafari

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?

Best Answer

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"