Save Web Pages as PDF's

Recently my MacBook Pro went down and as a result lost a lot of my scripts. Trying to recreate some on an old machine and have run into what I thought was a simple issue. I open a number of documents in Safari then I want save them as PDF’s. The issue is I cannot work out how I save them where I need them. They seem to always go to the last Folder Saved to. Found a scrip that I thought may work but I have not been able to do so. Cannot get past the line that is supposed to select the folder location.

This is the error message I get

Result:
error “System Events got an error: Can’t set window "Save" of process "Safari" to "/Users/mitch06042016/Dropbox/2 Test Imac/".” number -10006 from window “Save” of process “Safari”

This is the script.


set SaveFolder to POSIX path of SaveFolder
display dialog SaveFolder
tell application "Safari" to activate
tell application "System Events"
	tell process "Safari"
		keystroke "p" using {command down} -- activate print menu item
		delay 2
		tell front window
			repeat until exists sheet 1
				delay 0.02
			end repeat
			tell sheet 1
				click menu button "PDF"
				repeat until exists menu 1 of menu button "PDF"
					delay 0.02
				end repeat
				click menu item "Save as PDF" of menu 1 of menu button "PDF"
			end tell
		end tell
		
		keystroke "g" using {shift down, command down} -- to select the folder location
		set value of text field 1 of sheet of window "Save" to SaveFolder
		-- Now click the Go button
		click button 1 of sheet 1 of sheet 1 of sheet 1 of window 1
		delay 2
		-- Set the file name and save
		set value of text field 1 of window "Save" to "TestResults.pdf"
		
		--click button "Save" of window "Save"
		click button "Go"
	end tell
end tell

Hopefully there is a simple answer but my searches have not found much other than the script above which I cannot get tow rod.

Peter

May you check if there is a menu item named “Save as PDF” in the PDF menu ?

In resources dedicated to High Sierra, I find no string “Save as PDF” but I find the French string “Enregistrer au format PDF”.

In resources dedicated to Mojave, I find the string “Save as PDF” and I find the French string “Enregistrer au format PDF”.

In resources dedicated to Catalina, I find no string “Save as PDF” and I don’t find the French string “Enregistrer au format PDF”.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 13 janvier 2020 12:24:59

Hi Yvan. There’s “Export as PDF…” in Safari’s “File” menu (Safari 12.1.2, macOS 10.14.6). That saves going through the Print dialog, although there aren’t as many options.

May you try to run this script with the Log History window open ?

set SaveFolder to (path to desktop folder) as text

set SaveFolder to POSIX path of SaveFolder

set pdfName to "TestResults.pdf"
set fullPath to SaveFolder & pdfName

tell application "System Events"
	if exists file fullPath then delete file fullPath
end tell

--display dialog SaveFolder
set theApp to "Safari" -- I code this way because all the rest of the script is the same for every application

tell application theApp to activate
tell application "System Events"
	tell process theApp
		keystroke "p" using {command down} -- activate print menu item
		tell window 1
			repeat until exists sheet 1
				delay 0.02
			end repeat
			tell sheet 1
				class of UI elements --> {group, static text, group, static text, pop up button, static text, pop up button, static text, text field, checkbox, static text, radio button, radio button, text field, static text, text field, static text, pop up button, static text, static text, radio group, static text, text field, pop up button, checkbox, checkbox, button, menu button, button, button, button}
				description of pop up buttons --> {"Imprimantes", "Préréglages", "Taille du papier", "PDE"}
				title of menu buttons
				tell menu button "PDF"
					get its value --> missing value
					click it
					repeat until exists menu 1
						delay 0.02
					end repeat
					get its value --> missing value
					name of menu items of menu 1 --> {"Ouvrir dans Aperçu", "Enregistrer au format PDF", "Enregistrer au format PostScript", missing value, "@ PDF-BAT.qfilter", "@ PDF-prépresse CMJN.qfilter", "@ PDF-web.qfilter", "@ PDFX3-ISO.qfilter", "Ajouter à iBooks", "Save as Adobe PDF", "Enregistrer en PDF-X", "Save PDF to Aperture", "Save PDF to folder as JPEG", "Save PDF to folder as TIFF", "Enregistrer le document PDF vers iPhoto", "Enregistrer sur iCloud Drive", "Enregistrer avec les reçus web", "Envoyer via Mail", "Envoyer via Messages", "Copie de AbracadabraPDF-web.qfilter", missing value, "Modifier le menu…"}
					
					click menu item 2 of menu 1 -- "Save as PDF"
				end tell -- menu button "PDF"
				
				keystroke "g" using {shift down, command down} -- to select the folder location
				repeat until exists sheet 1
					delay 0.02
				end repeat
				tell sheet 1
					repeat until exists sheet 1
						delay 0.02
					end repeat
					tell sheet 1
						set theClasses to class of UI elements --> {static text, combo box, button, button}
						name of buttons --> {"Aller", "Annuler"}
						if theClasses contains combo box then
							set value of combo box 1 to SaveFolder -- modern systems no longer use a text field here
						else
							set value of text field 1 to SaveFolder
						end if
						click button 1 --> "Aller"
					end tell -- sheet 1
					repeat while exists sheet 1
						delay 0.02
					end repeat
					class of UI elements --> {static text, text field, UI element, static text, text field, group, radio group, group, pop up button, text field, splitter group, text field, static text, button, text field, static text, text field, static text, static text, text field, button, button, button}
					name of text fields --> {"Enregistrer sous :", "Tags :", missing value, missing value, missing value, missing value, missing value}
					set value of text field 1 to pdfName
					name of buttons --> {"Options de sécurité…", "Nouveau dossier", "Annuler", "Enregistrer"}
					name of button -1 --> "Enregistrer"
					click button -1
				end tell -- sheet 1
			end tell --  sheet 1
		end tell --  window 1
		
	end tell
end tell

Here the log history is :


Don't click upon [Open this Scriplet in your Editor:], here is just a log history

tell current application
	path to desktop
		--> alias "SSD 1000:Users:**********:Desktop:"
end tell
tell application "System Events"
	exists file "/Users/**********/Desktop/TestResults.pdf"
		--> true
	delete file "/Users/**********/Desktop/TestResults.pdf"
end tell
tell application "Safari"
	activate
end tell
tell application "System Events"
	keystroke "p" using {command down}
	exists sheet 1 of window 1 of process "Safari"
		--> true
	get class of every UI element of sheet 1 of window 1 of process "Safari"
		--> {group, static text, group, static text, pop up button, static text, pop up button, static text, text field, checkbox, static text, radio button, radio button, text field, static text, text field, static text, pop up button, static text, static text, radio group, static text, text field, pop up button, checkbox, checkbox, button, menu button, button, button, button}
	get description of every pop up button of sheet 1 of window 1 of process "Safari"
		--> {"Imprimantes", "Préréglages", "Taille du papier", "PDE"}
	get title of every menu button of sheet 1 of window 1 of process "Safari"
		--> {"PDF"}
	get value of menu button "PDF" of sheet 1 of window 1 of process "Safari"
		--> missing value
	click menu button "PDF" of sheet 1 of window 1 of process "Safari"
		--> menu button "PDF" of sheet 1 of window "MacScripter / Save Web Pages as PDF's" of application process "Safari"
	exists menu 1 of menu button "PDF" of sheet 1 of window 1 of process "Safari"
		--> true
	get value of menu button "PDF" of sheet 1 of window 1 of process "Safari"
		--> missing value
	get name of every menu item of menu 1 of menu button "PDF" of sheet 1 of window 1 of process "Safari"
		--> {"Ouvrir dans Aperçu", "Enregistrer au format PDF", "Enregistrer au format PostScript", missing value, "@ PDF-BAT.qfilter", "@ PDF-prépresse CMJN.qfilter", "@ PDF-web.qfilter", "@ PDFX3-ISO.qfilter", "Ajouter à iBooks", "Save as Adobe PDF", "Enregistrer en PDF-X", "Save PDF to Aperture", "Save PDF to folder as JPEG", "Save PDF to folder as TIFF", "Enregistrer le document PDF vers iPhoto", "Enregistrer sur iCloud Drive", "Enregistrer avec les reçus web", "Envoyer via Mail", "Envoyer via Messages", "Copie de AbracadabraPDF-web.qfilter", missing value, "Modifier le menu…"}
	click menu item 2 of menu 1 of menu button "PDF" of sheet 1 of window 1 of process "Safari"
		--> menu item "Enregistrer au format PDF" of menu "PDF" of menu button "PDF" of sheet 1 of window "MacScripter / Save Web Pages as PDF's" of application process "Safari"
	keystroke "g" using {shift down, command down}
	exists sheet 1 of sheet 1 of window 1 of process "Safari"
		--> true
	exists sheet 1 of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> true
	get class of every UI element of sheet 1 of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> {static text, combo box, button, button}
	get name of every button of sheet 1 of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> {"Aller", "Annuler"}
	set value of combo box 1 of sheet 1 of sheet 1 of sheet 1 of window 1 of process "Safari" to "/Users/**********/Desktop/"
	click button 1 of sheet 1 of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> button "Aller" of sheet 1 of sheet 1 of sheet 1 of window "MacScripter / Save Web Pages as PDF's" of application process "Safari"
	exists sheet 1 of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> false
	get class of every UI element of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> {static text, text field, UI element, static text, text field, group, radio group, group, pop up button, text field, splitter group, text field, static text, button, text field, static text, text field, static text, static text, text field, button, button, button}
	get name of every text field of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> {"Enregistrer sous :", "Tags :", missing value, missing value, missing value, missing value, missing value}
	set value of text field 1 of sheet 1 of sheet 1 of window 1 of process "Safari" to "TestResults.pdf"
	get name of every button of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> {"Options de sécurité…", "Nouveau dossier", "Annuler", "Enregistrer"}
	get name of last button of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> "Enregistrer"
	click last button of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> button "Enregistrer" of sheet 1 of sheet 1 of window "MacScripter / Save Web Pages as PDF's" of application process "Safari"
end tell
Résultat :
button "Enregistrer" of sheet 1 of sheet 1 of window "MacScripter / Save Web Pages as PDF's" of application process "Safari" of application "System Events"

You are supposed to get something resembling to this history.

It would be fair to post here what you get on your side.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 13 janvier 2020 14:35:46

Thanks Nigel.

I’m aware of that.
I know that many users choose to take the Print road because this one is triggered by a shortcut.
There is no one for “Export as PDF…” and the wording of the menu item is localized so it’s easier to trigger the Print shortcut.
As you may see in the script posted above, we may work without grabbing the localized items descriptions.

My understanding is that, apart the name of the menu item which in the past was “Save As…”, became “Save As PDF…” and may be different in Catalina, there was an important change.
In old times, the path to the target folder was inserted in a text field object.
Since 10.9 or 10.10 (if my memory is right) the dialog no longer use such object, and replace it by a combo box object.

With a bit of luck, the posted script will work.

I’m mainly interested to know what is reported in the log to learn what the engineers may have changed.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 13 janvier 2020 14:50:40

There are a few issues.

– There is no window Save, all windows are sheets.

  • The UI element to enter text in the sheet which opens on ⇧⌘G is a Combo Box.
    – It’s highly recommended to wait for appearing and disappearing of all affected UI elements.

This works in Mojave 10.14.6


set SaveFolder to POSIX path of SaveFolder
display dialog SaveFolder
tell application "Safari" to activate
tell application "System Events"
	tell process "Safari"
		keystroke "p" using {command down} -- activate print menu item
		delay 2
		tell front window
			repeat until exists sheet 1
				delay 0.02
			end repeat
			tell sheet 1
				click menu button "PDF"
				repeat until exists menu 1 of menu button "PDF"
					delay 0.02
				end repeat
				click menu item 2 of menu 1 of menu button "PDF"
				
				repeat until exists sheet 1
					delay 0.02
				end repeat
				tell sheet 1
					keystroke "g" using {shift down, command down}
					repeat until exists sheet 1
						delay 0.02
					end repeat
					tell sheet 1
						set value of combo box 1 to SaveFolder
						-- Now click the Go button
						click button 1
					end tell
					repeat while exists sheet 1
						delay 0.02
					end repeat
					-- Set the file name and save
					set value of text field 1 to "TestResults.pdf"
					click button 3
				end tell
			end tell
		end tell
	end tell
end tell

Hello StefanK

According to your code, it seems that under Mojave, the button “Save” is the 3rd one.
Under High Sierra, it’s the 4th in the list {“Options de sécurité…”, “Nouveau dossier”, “Annuler”, “Enregistrer”}

Is this button the last one in the list for Mojave ?
If it is it would be better to trigger button -1 which would be OK for both system.

In fact our scripts are doing the same thing.
I just left instruction supposed to return useful informations if something doesn’t behave correctly under Catalina.

Here is a version triggering the menu item “Export As…”

This time I disabled the instructions supposed to return useful informations.


tell application "Finder" to set go_loc to localized string "FR24"
if go_loc is "FR24" then set go_loc to "Go"

set SaveFolder to (path to documents folder) as text

set SaveFolder to POSIX path of SaveFolder

set pdfName to "TestResults.pdf"
set fullPath to SaveFolder & pdfName

tell application "System Events"
	if exists file fullPath then delete file fullPath
end tell

--display dialog SaveFolder
set theApp to "Safari" -- I code this way because all the rest of the script is the same for every application

tell application theApp to activate

set mt to 3 -- index of File menu
tell application id "com.apple.systemevents" to tell process theApp
	tell menu bar 1
		-- get name of menu bar items --> {"Apple", "Safari", "Fichier", "Édition", "Présentation", "Historique", "Signets", "Développement", "Fenêtre", "Aide", "Debug"}
		-- get name of menu bar item mt --> "Fichier"
		tell menu bar item mt to tell menu 1
			set itemNames to get name of menu items --> {"Nouvelle fenêtre", "Nouvelle fenêtre privée", "Nouvel onglet", "Nouvel onglet à la fin", "Ouvrir un fichier…", "Ouvrir une adresse…", missing value, "Fermer", "Fermer", "Fermer toutes les fenêtres", "Fermer toutes les fenêtres", "Fermer l’onglet", "Fermer les autres onglets", "Enregistrer sous…", "Enregistrer sous…", missing value, "Partager", "Exporter au format PDF…", "Ouvrir dans Dashboard…", missing value, "Importer depuis", "Exporter les signets…", missing value, "Imprimer…"}
			set exportAsPdf_loc to ""
			repeat with anItem in itemNames
				if anItem contains "PDF" then
					set exportAsPdf_loc to anItem as text
					exit repeat
				end if
			end repeat
			click menu item exportAsPdf_loc
		end tell -- menu bar…
	end tell
	tell window 1
		repeat until exists sheet 1
			delay 0.02
		end repeat
		tell sheet 1
			-- class of UI elements --> {static text, text field, UI element, static text, text field, group, radio group, group, pop up button, text field, splitter group, button, button, button}
			-- get name of text fields --> {"Exporter comme :", "Tags :"}
			set value of text field 1 to pdfName
			
			keystroke "g" using {shift down, command down} -- to select the folder location
			repeat until exists sheet 1
				delay 0.02
			end repeat
			tell sheet 1
				set theClasses to class of UI elements --> {static text, combo box, button, button}
				-- name of buttons --> {"Aller", "Annuler"}
				if theClasses contains combo box then
					set value of combo box 1 to SaveFolder -- modern systems no longer use a text field here
				else
					set value of text field 1 to SaveFolder
				end if
				click button go_loc -->  may use the index 1 --> "Aller"
			end tell
			-- name of buttons --> {"Annuler", "Enregistrer"}
			-- name of button -1 --> "Enregistrer"
			click button -1
		end tell --  sheet 1
	end tell --  window 1
	
end tell -- System Events

I didn’t find a resource defining the localized strings for “Export As PDF…” so I use a loop to get it’s index in the list of items available.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 13 janvier 2020 17:19:33

@Yvan

In Mojave there are only 3 buttons, Save is the last one.

The localized strings are most likely in the framework which controls the print engine.

Thanks.

It would be better to click button-1 which works for every systems.

I know that there is the menu item “Enregistrer au format PDF” in the print dialog under Catalina but I really don’t find it in the resources.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 13 janvier 2020 21:49:13

I had the opportunity to test under Catalina.

With three systems we have three sets of buttons

High Sierra : four buttons : {“Options de sécurité…”, “Nouveau dossier”, “Annuler”, “Enregistrer”}
Mojave : three buttons : I guess that they are : {“Nouveau dossier”, “Annuler”, “Enregistrer”}
Catalina : five buttons : {missing value, “Options de sécurité…”, “Nouveau dossier”, “Annuler”, “Enregistrer”}

So it’s clear that the correct way to trigger the “Save” button is to click button -1 which is guarantee to trigger the last one in the three lists.

I will be glad to learn which are really the three buttons available in Mojave.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 14 janvier 2020 20:46:16

They are

{“Options de sécurité…”, “Annuler”, “Enregistrer”}

Thank you Stefan.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 15 janvier 2020 08:30:03

Yvan

Got my computer back to day and I am I the process of rebuilding just about everything. did not want you to think I had not tried your suggestion will do as soon as I can. Not sure how relevant it its but I am running on High Sierra, so my option is to export as a PDF.

Thanks again

Peter

The script in my message #7 was tested under 10.13.6 and under the current Catalina.
When I will have your feedback I will edit it disabling the instructions which are only available to get relevant infos about the interface elements.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 16 janvier 2020 09:46:36

Finally working more or less and just ran your script this is the log window result as you probably know the files is saved to the desktop. As I mentioned I want to save it in another folder.

Peter


tell current application
	path to desktop
		--> alias "Macintosh HD:Users:mitch06042016:Desktop:"
end tell
tell application "System Events"
	exists file "/Users/mitch06042016/Desktop/TestResults.pdf"
		--> true
	delete file "/Users/mitch06042016/Desktop/TestResults.pdf"
end tell
tell application "Safari"
	activate
end tell
tell application "System Events"
	keystroke "p" using {command down}
	exists sheet 1 of window 1 of process "Safari"
		--> true
	get class of every UI element of sheet 1 of window 1 of process "Safari"
		--> {group, static text, group, static text, pop up button, static text, pop up button, static text, text field, checkbox, checkbox, static text, radio button, radio button, text field, static text, text field, static text, pop up button, static text, static text, radio group, static text, text field, pop up button, checkbox, checkbox, button, menu button, button, image, static text, button, button, button}
	get description of every pop up button of sheet 1 of window 1 of process "Safari"
		--> {"Printers", "Presets", "Paper size", "PDE"}
	get title of every menu button of sheet 1 of window 1 of process "Safari"
		--> {"PDF"}
	get value of menu button "PDF" of sheet 1 of window 1 of process "Safari"
		--> missing value
	click menu button "PDF" of sheet 1 of window 1 of process "Safari"
		--> menu button "PDF" of sheet 1 of window "MacScripter / Save Web Pages as PDF's" of application process "Safari"
	exists menu 1 of menu button "PDF" of sheet 1 of window 1 of process "Safari"
		--> true
	get value of menu button "PDF" of sheet 1 of window 1 of process "Safari"
		--> missing value
	get name of every menu item of menu 1 of menu button "PDF" of sheet 1 of window 1 of process "Safari"
		--> {"Open in Preview", "Save as PDF", "Save as PostScript", missing value, "Add to iBooks", "Save to iCloud Drive", "Save to Web Receipts", "Send in Mail", "Send via Messages", missing value, "Edit Menu…"}
	click menu item 2 of menu 1 of menu button "PDF" of sheet 1 of window 1 of process "Safari"
		--> menu item "Save as PDF" of menu "PDF" of menu button "PDF" of sheet 1 of window "MacScripter / Save Web Pages as PDF's" of application process "Safari"
	keystroke "g" using {shift down, command down}
	exists sheet 1 of sheet 1 of window 1 of process "Safari"
		--> true
	exists sheet 1 of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> true
	get class of every UI element of sheet 1 of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> {static text, combo box, button, button}
	get name of every button of sheet 1 of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> {"Go", "Cancel"}
	set value of combo box 1 of sheet 1 of sheet 1 of sheet 1 of window 1 of process "Safari" to "/Users/mitch06042016/Desktop/"
	click button 1 of sheet 1 of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> button "Go" of sheet 1 of sheet 1 of sheet 1 of window "MacScripter / Save Web Pages as PDF's" of application process "Safari"
	exists sheet 1 of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> false
	get class of every UI element of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> {static text, text field, UI element, static text, text field, group, radio group, group, pop up button, text field, splitter group, text field, static text, button, text field, static text, text field, static text, static text, text field, button, button, button}
	get name of every text field of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> {"Save As:", "Tags:", missing value, missing value, missing value, missing value, missing value}
	set value of text field 1 of sheet 1 of sheet 1 of window 1 of process "Safari" to "TestResults.pdf"
	display dialog "TestResults.pdf"
		--> error number -1708
	«event ascrgdut»
		--> error number -1708
	display dialog "TestResults.pdf"
		--> {button returned:"OK"}
	get name of every button of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> {"Security Options...", "New Folder", "Cancel", "Save"}
	get name of last button of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> "Save"
	click last button of sheet 1 of sheet 1 of window 1 of process "Safari"
		--> button "Save" of sheet 1 of sheet 1 of window "MacScripter / Save Web Pages as PDF's" of application process "Safari"
end tell
Result:
button "Save" of sheet 1 of sheet 1 of window "MacScripter / Save Web Pages as PDF's" of application process "Safari" of application "System Events"


The script in message #7 allow you to edit one instruction defining the target folder to fit your needs.

set SaveFolder to (path to documents folder) as text-- EDIT to fit your needs.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 31 janvier 2020 09:41:12