I am trying to create an AppleScript that takes each open tab in safari, presses a bookmarklet button then hits File…Print changes orientation to Landscape (clicking the Landscape radio button) and then saves as PDF. I have got some of the way there through the forums but my AppleScript knowledge is limited so would appreciate any assistance please. What I am missing is:
- Iterating though each tab
- Pressing the bookmarklet
- Changing the orientation
-- Keep code for Safari separate from the code for System Events.
tell application "Safari"
activate
-- Tell Safari, as the known frontmost application, to display the 'choose folder' dialog.
set SaveFolderPath to POSIX path of (choose folder with prompt "Select Folder to Save PDF Files")
tell window 1
set current tab to tab 1
set ctabs to (count tabs)
end tell
end tell
repeat with i from 1 to ctabs
tell application "System Events"
tell process "Safari"
click menu item "Print…" of menu "File" of menu bar 1
click menu item "Print…" of menu "File" of menu bar 1
repeat until exists sheet 1 of window 1
delay 1
end repeat
-- The sheet opens alread primed with a file name based on the tab name.
if (i is 1) then
-- Make sure the file gets saved to the folder chosen above.
-- This is only necessary first time round the repeat, as the folder becomes the default next time.
keystroke "g" using {command down, shift down}
repeat until exists sheet 1 of sheet 1 of window 1
delay 1
end repeat
log "passed delay"
tell sheet 1 of sheet 1 of window 1
set value of combo box 1 to SaveFolderPath -- NB. the folder path here, not the file name.
click button "Go"
end tell
end if
click button "Save" of sheet 1 of window 1
click menu item "Close tab" of menu "File" of menu bar 1
end tell
tell application "Safari" to display dialog "Close after 1 second..." giving up after 1
end tell
end repeat