So I used this code from here to save a single webpage with Safari’s “Export as PDF…”
I am looping through a big array of URL’s, setting up Safari in Responsive Design Mode to save the PDF’s for mobile. I am even scrolling down the pages to load lazy images and dynamically modifying CSS with JavaScript but I left that out for now.
The problem is that I never get through my URL list, mostly the script stucks at the repeat until exists sheet 1 of window 1
right after the click menu item "Export as PDF…"
Here an example of the SE replies:
tell application "System Events"
click menu item "Export as PDF…" of menu "File" of menu bar 1 of process "Safari"
--> menu item "Export as PDF…" of menu "File" of menu bar item "File" of menu bar 1 of application process "Safari"
exists sheet 1 of window 1 of process "Safari"
--> false
exists sheet 1 of window 1 of process "Safari"
--> false
exists sheet 1 of window 1 of process "Safari"
--> false</code>
I have bumped up and inserted various delays, to no avail.
Sometimes I also get similar problems with other click menu item
and click button
Is there another (new?) way to do this, or to check if a button is indeed clicked or a sheet does exist?
Running through the array, the whole UI processing feels slow and laggy, hence all the delay
s.
I also tried throwing in some try - on error - end try
but it doesn’t help…
# declare variables
global G_url_list, G_url_list_counter, G_save_folder_path
# an array of URL's
set G_url_list to {"h**ps://www.morganscloud.com/2019/03/28/4-vital-anchor-selection-criteria-and-a-review-of-spade/", "h**ps://www.morganscloud.com/2019/03/14/15-steps-to-getting-securely-anchored/", "h**ps://www.morganscloud.com/2017/03/22/come-alongside-docking-made-easy/", "h**ps://www.morganscloud.com/2017/03/24/10-tips-to-make-coming-alongside-docking-easy/"}
# start index
set G_url_list_counter to 1
# folder to save the PDF's
set G_save_folder_path to "~/Desktop/"
# INIT
urlLoop(G_url_list)
# wait for page to finish loading in Safari
# works in macOS Catalina and macOS Big Sur
# may need adjusting for other versions of macOS
on waitPageLoaded()
tell application "System Events" to repeat until ¬
exists (buttons of groups of toolbar 1 of window 1 of ¬
process "Safari" whose name = "Reload this page")
delay 0.5
end repeat
end waitPageLoaded
# loop through URL list
# @ array - URL list
on urlLoop(url_arr)
repeat with i from 1 to (count url_arr)
set url_item to (item i of url_arr)
my loadURL(url_item)
# add to global counter
set G_url_list_counter to G_url_list_counter + 1
end repeat
end urlLoop
# make a new tab and load URL
# prepare Safari in Responsive Design Mode
# @ string - URL to load
on loadURL(url_item)
tell application "Safari"
activate
tell window 1
set current tab to make new tab with properties {URL:url_item}
my waitPageLoaded()
delay 2
# enter Responsive Design Mode, only after page is loaded
tell application "System Events" to tell process "Safari"
-- click menu item "Show Web Inspector" of menu "Develop" of menu bar 1
click menu item "Enter Responsive Design Mode" of menu "Develop" of menu bar 1
end tell
delay 2
end tell -- window 1 of Safari
end tell -- Safari app
my scrollPage()
end loadURL
# scroll the page to the bottom to load all lazy images
on scrollPage()
my saveToPDF()
end scrollPage
# save as PDF and close Safari tab
on saveToPDF()
tell application "Safari"
activate
tell application "System Events" to tell process "Safari"
# export PDF
click menu item "Export as PDF…" of menu "File" of menu bar 1
delay 4 -- give the dialog a few seconds to popup
repeat until exists sheet 1 of window 1
delay 0.5
end repeat
# define folder with folder shortcut
keystroke "g" using {command down, shift down}
repeat until exists sheet 1 of sheet 1 of window 1
delay 0.5
end repeat
# set path and go
tell sheet 1 of sheet 1 of window 1
set value of combo box 1 to G_save_folder_path
click button "Go"
end tell
# save
click button "save" of sheet 1 of window 1
delay 1 -- give the save process a second
# close tab
click menu item "Close Tab" of menu "File" of menu bar 1
delay 1 -- give the close tab a second
end tell -- System Events -- process Safari
end tell -- Safari app
end saveToPDF