Applescript to switch printers?

I am trying to automate the printing of postage labels from Safari. My printing is set up with the default printer being my laser printer, but I need to switch to the label printer to print the labels.

What Applescript code do I use to do this? I am unable to find this info with a Google search. I thought this would be a pretty standard thing people would be doing.

I need AppleScript to invoke the Print sheet, then choose an alternate printer. The label printer’s default preset is the one I want to use to print. I just need to get AppleScript to choose a different printer since Safari default to the laser printer each time the Print sheet is invoked.

Thank you.

You may try to use this script :


(*
Grabs the name of the application as well as the name of the process. They aren't necessarily the same.
For instance, when I uses my old Numbers '09, the application is named Numbers 09 when the process is named Numbers.
*)
tell application "System Events"
	set frontmostProcess to first process where it is frontmost -- this will be the script process
	if name of frontmostProcess is in {"Script Editor", "AppleScript Editor"} then
		set visible of frontmostProcess to false -- hide the script process
		repeat while (frontmostProcess is frontmost) -- wait until the script is hiden
			delay 0.2
		end repeat
		set theProcess to (first process where it is frontmost)
		
		set frontmost of frontmostProcess to true -- unhide the script process
	else
		set theProcess to frontmostProcess
	end if
	set theapp to file of theProcess
	(*
Test added because during my tests, sometimes I left Finder at front and the script entered an infinite loop
*)
	if name of theapp is "Finder.app" then error number -128
	set theapp to path of theapp
end tell

activate application (theapp as text)
tell application "System Events" to tell theProcess
	keystroke "p" using command down
	(*
Wait for the availability of the sheet *)
	repeat
		if exists sheet 1 of window 1 then exit repeat
		delay 0.1
	end repeat
	-- value of every pop up button of sheet 1 of window 1
	--	(*HP Officejet Pro 8610, Papier ordinaire, supérieure, A4, Safari*)
	tell sheet 1 of window 1
		set printerPopUp to first pop up button
		click printerPopUp
		name of menu items of menu 1 of printerPopUp
		--> {"HP Officejet Pro 8610", missing value, "HP Officejet Pro 8610 (Fax)", "HP Officejet Pro 8610 FAX", missing value, "Ajouter une imprimante.", "Préférences Imprimantes et scanners."}
		
		click menu item 3 of menu 1 of printerPopUp # ADJUST TO FIT YOUR NEEDS
		
		keystroke return
	end tell # sheet 1 of window 1
end tell

#=====

Don’t forget to edit the instruction :
[format] click menu item 3 of menu 1 of printerPopUp # ADJUST TO FIT YOUR NEEDS[/format]

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) jeudi 4 aout 2016 11:01:25

Nominally, this should do it:

tell application "Safari"
	print document 1 with properties {target printer:"Your printer name here"} -- Edit as required.
end tell

Unfortunately, I only have one printer and can’t tell if printing does actually go to the specified non-default in Safari. However, this version .

tell application "Safari"
	print document 1 with properties {target printer:"Name of printer when directly connected"} with print dialog
end tell

. brings up the print dialog with the other driver selected, so it sounds hopeful.

The first solution worked perfectly. I saw that in the AppleScript library for Safari but I guess it wasn’t obvious to me that that was the command I was looking for.

Ultimately, this will be added into a Keyboard Maestro macro as I need to click various webpage buttons to bring up the PDFs to print, and I am pretty sure AppleScript can’t handle those.

I’d prefer to do it all within Applescript. The rest of the process is just bringing up the Print dialog, clicking Print, closing Safari tabs, and so on.

Thank you!