1
votes

I have an AppleScript to open move a Safari webpage into Reader view and then to Save As a PDF.

I would like it to Save As the PDF with default filename, but always in the same specific location. I'm not sure how to do this.

Currently the AppleScript runs as:

tell application "System Events"
tell application process "Safari"
    set frontmost to true

    repeat until window 1 exists
    end repeat

    # Render page in Safari Reader
    keystroke "r" using {command down, shift down}

    delay 0.02

    -- Print; wait until the sheet is visible   
    click menu item "Print…" of menu 1 of menu bar item "File" of menu bar 1

    repeat until sheet 1 of window 1 exists
    end repeat

    set thePopUp to first pop up button of sheet 1 of window 1 whose description is "Presets"
    click thePopUp
    click menu item "Default Settings" of menu 1 of thePopUp --replace if desired with your preferred preset

    click menu button "PDF" of sheet 1 of window 1
    click menu item "Save as PDF…" of menu 1 of menu button "PDF" of sheet 1 of window 1 -- Save as PDF...
    tell application "Finder"
        set filesavepath to "/Users/Ariel/Google Drive/To print"
    end tell
end tell

end tell

The final bits of the AppleScript, from 'tell application finder' do not actually make the Page save in my desired location. Please could you help me to this. Thank you for any help.

Ariel

1

1 Answers

1
votes

To select the path in the standard save as dialog, you can keystroke usual shortcuts :

  • command shift d = Desktop
  • command shift A = Application
  • command shift O = Documents
  • command shift H = Home
  • command shift G = new dialog to ask you the path from where you are.

Exemple, I want to save "my_Test" in folder Documents/mySafari/my_subfolder

tell application "Safari"
activate
tell application "System Events" to tell application process "Safari"
    keystroke "r" using {command down, shift down} -- read mode
    delay 0.5
    click menu item 17 of menu 1 of menu bar item 3 of menu bar 1 -- export as PDF (Safari 9.0.2 / El Capitain)
    delay 0.5
    keystroke "h" using {command down, shift down} -- go home directory
    keystroke "g" using {command down, shift down} -- go to dialog
    keystroke "mySafari/my_subfolder" -- path from Documents folder
    keystroke return -- close go to dialog
    keystroke "My_test" -- fill file name from save as dialog
    keystroke return -- close the save as dialog
end tell
end tell

Warnings :

1) the number of menu item "save as PDF" may change on your Safari version (I am using 9.0.2, then it is 17). if you use the menu title it could be easier, but then you are link to your system language !(it could be different than mine)

2) the folder path is also in local language: (main folders like library, pictures, desktop... are translated in local language. Use the local language version. For instance folder 'Pictures' in French OS is 'Images'.