GUI Scripting Safari "Export as PDF" Dialog

Yes, admittedly, I am wrong.

Of course, after I posted this GUI script example, I ran it on the macrumors site and it only captures the first article on that site, not the entire landing page. So, back to the drawing board, when I have time.

I try to never use long delays. I instead test for GUI items existing to control program flow like so…

use scripting additions

-- replace the 12-hour clock (%I) with 24-hour clock (%H) if needed.
set dt to (do shell script "date -j \"+%Y%m%dT%I%M%S\"")
set fileName to "Safari (" & dt & ")" & ".pdf"
log (fileName) as text

tell application "Safari" to activate
tell application "System Events"
	tell process "Safari"
		-- set frontmost to true -- not needed, the activate command above already does this
		-- 1. Enter Reader Mode so we can have paginated PDF output
		try
			click menu item "Show Reader" of menu 1 of menu bar item "View" of menu bar 1
			repeat until exists menu item "Hide Reader" of menu 1 of menu bar item "View" of menu bar 1
				delay 0.5 -- Allow time to render
			end repeat
		end try
		
		-- 2. Trigger "Export as PDF"
		click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1
		
		-- 3. Handle the Save dialog (adjust delay for speed)
		repeat until exists sheet 1 of window 1
			delay 0.5
		end repeat
		if (value of attribute "AXIdentifier" of sheet 1 of window 1) = "save-panel" then
			set value of text field "Save As:" of sheet 1 of window 1 to fileName
			delay 0.5
			click button "Save" of sheet 1 of window 1
		end if
	end tell
end tell

I also try to avoid keystroke commands.

Robert. Thanks for the suggestion.

In my particular circumstance, the PDF cannot be paginated. The reason is that the content of the web page is financial data in a tabular format and splitting the PDF into pages is very undesirable. So, I simply disabled the reader portion of your script, but an error is returned. I assume that sheet 1 of window 1 is the issue, but that’s just a guess. BTW, I lengthened the delays to 1 second just for testing.

I’m on MacOS Sequoia. What OS are you on?

I’m on macOS Tahoe 26.5 Public Beta 3.

I’m not on Tahoe, but Apple probably changed the order of the gui elements

Copy the value to the clipboard.
Set the focus on the text field
Paste the clipboard
Press Return

Thanks Robert. I’ll spend some time learning UI Browser to see if I can get the correct GUI elements.

Hi @peavine

Try this script. It’s running fine here but I’m on Sequoia.

-- build the name of the pdf file
set {{a, b, c}, {d, e, f}} to {words of short date string, words of time string} of (current date)
set theName to c & "-" & b & "-" & c & " at " & d & "." & e & "." & f & ".pdf"
set thePath to ("" & (path to desktop) & theName)

tell application "Safari" to activate
tell application "System Events" to tell process "Safari"
	
	-- click menu item to open the 'save as pdf' panel
	set aMenu to a reference to (menu item 1 of menu 1 of menu bar item 3 of menu bar 1 whose name contains "export") -- chage "export" to suite your language
	if not (exists aMenu) then return "can't find export menu"
	click aMenu
	
	-- wait for sheet to open, returning error if it takes more than 2 seconds
	repeat with i from 1 to 20
		if exists sheet 1 of window 1 then exit repeat
		delay 0.1
	end repeat
	if i = 20 then return "save as panel can't open"
	
	-- in this sequence, you may need to add delays
	set value of text field 1 of sheet 1 of window 1 to theName -- the 'save as' text field is always the first
	click pop up button 1 of sheet 1 of window 1 -- there is only 1 popup button
	click menu item 1 of menu 1 of pop up button 1 of sheet 1 of window 1 -- the first is the desktop
	click button -1 of sheet 1 of window 1 -- the 'ok' button is always the last
	
end tell

delay 1 -- wait for the file to be available

tell application id "com.apple.Preview"
	activate
	open thePath
end tell

Be aware that with the AP site, I got 2 issues:
1- needed to scroll down the page at first to get all images in the pdf.
2- the pdf shows nothing in Acrobat DC but is perfect in Preview.

Thanks Jonas for the suggestion.

I received the following error, and I assume that’s because sheet 1 of window 1 doesn’t work on my Tahoe computer. My test site was macscripter.net.

System Events got an error: Can’t get text field 1 of sheet 1 of window 1 of process “Safari”. Invalid index.

I also received the following error, but that was because of the value returned on my computer for short date string. This was easily fixed.

error “Can’t get item 2 of {"2026.05.05"}.” number -1728 from item 2 of {“2026.05.05”}

Note sure. If it was the case, the script result should be “save as panel can’t open” (unless you’ve change it!).
Same thing if it can’t find the Export menu.
Does the ‘Save As’ panel opens or not at all?

In Tahoe the UI of the sheet is wrapped in a splitter group, see my first post.

1 Like

Jonas. The only changes I made were to fix the date issue and to delay 1 second while Safari activated. This is the result:

Thanks Stefan. I missed that completely. :frowning:
I will test this later today and report back.

I spent over an hour attempting to get my script to work reliably with the suggested solutions, but none worked reliably. So, I’m going to give up on this one. My original script is a bit slow and kludgey, but it does work reliably, and I’ll continue using that.

Thanks everyone for your suggestions–I greatly appreciate your help.

-1728 error is usually permissions error

@peavine, are you interested in a wkhtmltopdf based solution? It’s a command line tool to render HTML into PDF. You could either have Safari save the html or just pull it with curl. Quick here on slow intel hardware.

--https://blog.harawata.net/2011/06/convert-html-files-to-pdf-using-web-kit.html?m=1
on run
 set htmls to choose file with prompt "PDFに変換するHTMLファイルを指定してください。" of type {"public.html"} with multiple selections allowed
 my convertHtmlToPdfList(htmls)
end run

on open htmls
 my convertHtmlToPdfList(htmls)
end open

on convertHtmlToPdfList(htmls)
 tell application "Finder"
  set cmd to POSIX path of (path to me) & "Contents/MacOS/wkhtmltopdf"
--The above line expects you to place the executable in the  Contents/MacOS/ directory inside the application package.
--I would replace this with a posix path to the wkhtmltopdf binary. 
--i.e. set cmd to "/usr/local/bin/wkhtmltopdf"

  repeat with html in htmls
   if (name extension of html is "html") then
    my convertHtmlToPdf(cmd, html)
   end if
  end repeat
 end tell
end convertHtmlToPdfList

on convertHtmlToPdf(cmd, html)
 tell application "Finder"
  set htmlName to name of html
  set {dir, htmlUrl, pdfName} to {POSIX path of (container of html as alias), URL of html, text 1 thru -5 of htmlName & "pdf"}
 end tell
 do shell script "\"" & cmd & "\" \"" & htmlUrl & "\" \"" & dir & pdfName & "\""
end convertHtmlToPdf
1 Like

Loos like @paulskinner now found something that can do this (as I’m sure you already saw anyway):

Thanks Paul. I’ll take a look at that. Sounds promising.