GUI Scripting Safari "Export as PDF" Dialog

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.

This caught my attention, but as I don’t use Safari (I use Firefox) I tried converting your script to Firefox (posted below).

--set filename
set {year:y, month:m, day:d, hours:h, minutes:min, seconds:s} to (current date)
set theDate to ((y * 10000) + ((m as integer) * 100) + d) as text
set theTime to text 2 thru -1 of ((1000000 + (h * 10000) + (min * 100) + s) as text)
set fileName to "Furefox (" & theDate & " " & theTime & ")"

--confirm Safari is open with URL
tell application "Firefox" to set theURL to URL of front document
if theURL is missing value then display dialog "The frontmost Firefox window could not be found or did not return a URL." buttons {"OK"} cancel button 1 default button 1

--I normally run this script by way of FastScripts with Safari active
--as a result, the following is normally not necessary and is for testing only
tell application "System Events" to tell process "Firefox"
	set frontmost to true
	delay 1
end tell

--confirm "Export as "PDF dialog is available then display
set repeatNumber to 6
tell application "System Events" to tell process "Firefox"
	repeat with i from 1 to repeatNumber
		set menuAvailable to enabled of menu item "Export as PDF…" of menu "File" of menu bar 1
		if menuAvailable is true then
			exit repeat
		else if i = repeatNumber then
			display dialog "The \"Export as PDF\" menu item was not available." buttons {"OK"} cancel button 1 default button 1
		else
			delay 0.5
		end if
	end repeat
	click menu item "Export as PDF…" of menu "File" of menu bar 1
	delay 1 --a smaller number doesn't work for me
	keystroke fileName
	delay 0.5
end tell

But it returns an error “Firefox got an error: Can’t get URL of document 1.” Any suggestions?

Homer712. I don’t have Firefox installed on my computer, but I’ll provide what information I can.

According to Google AI, Firefox does not support AppleScript. So, the following line of the script will fail.

tell application "Firefox" to set theURL to URL of front document

It could be replaced with something else or skipped entirely if Firefox is open when the script is run. You might try the following followed by a delay.

tell application "Firefox" to activate

The rest of the script uses GUI scripting and may or may not work depending on menu items available in Firefox. Unfortunately, I don’t have any way to help with this.

I tried almost all of your suggestions and none of them worked, but then I have FireShot installed in Firefox, which captures an entire page and creates a PDF document.

Sorry for the fire drill.