Set default printer via shell

My searches are yealding hundreds of results but finding this amongst them is proving difficult. Im sure it’s been covered before. I would like to replace lines 3-4 with shell commands if possible? so I can do away with PSU altogether. X.3

tell application "Printer Setup Utility"
	set Previous_Printer to name of the current printer -- I can do this with last word of (do shell script "lpstat -d")
	set the current printer to printer Printer_Name -- Can't find how to do this
	set Printer_Status to status of current printer -- r/o in PSU. Can't find how to do this
	if Printer_Status is stopped then
		my OverRide(Printer_Name) -- Uses "lpstat" & "lpadmin"
	else
		set Hold_Jobs_List to {}
	end if
end tell

Hi Mark,

AFAIK not directly, in many applications you can use in AppleScript

print document 1 with properties {target printer:thePrinter} without print dialog

or with the lpr shell command to print a file

do shell script "lpr " & "-P " & thePrinter & space & (quoted form of POSIX path of thepath)

Hi Mark

I use this to set my default printer.

set the_ppd to "Adobe_PDF_7_0"
do shell script "lpoptions -d" & the_ppd

use this command “lpstat -t” to get a list of printers on your network.
you can get the exact spelling for the printer then to place in the above script.

Thanks pidge, there’s something to learn every day :slight_smile:

Here is a little routine to set the printer from the list of available printers

set currPrinter to last word of (do shell script "lpstat -d")
set printer_list to {}
set availablePrinters to paragraphs of (do shell script "lpstat -a")
repeat with i in availablePrinters
	set end of printer_list to word 1 of i
end repeat
set choice to choose from list printer_list with prompt "select printer" default items {currPrinter}
if choice is false then return
do shell script "lpoptions -d" & item 1 of choice

I use a hot key for this chooser:

tell application "Printer Setup Utility"
	activate
	repeat
		set myPrinters to every printer
		set C to count myPrinters
		set DefaultPrinter to current printer
		repeat with k from 1 to C
			if item k of myPrinters is DefaultPrinter then
				set current printer to item ((k mod C) + 1) of myPrinters
				exit repeat
			end if
		end repeat
		if button returned of (display dialog (name of current printer) as text default button 1 buttons {"OK"} with title "Press Enter when Printer is Correct" giving up after 1.5) is "OK" then exit repeat
	end repeat
	quit
end tell

When StefanK said no go. I had given up hope on this he’s almost always right on the money. Thank you, Pidge1 I may now get a PSU free solution. Im already using most of whats in Stafan’s later post. I also think I have a solution for all the status options. I will get working on this again.

Here is what I ended up with couldn’t quite work an option or two that I would have perfered. Presets works well with several Apple apps for PPD specific options duplexing, paper trays, colour settings etc. Im sure there’ll be stuff I’ve missed but Im limited to test with only a few printers.

property Version_Number : 1.0 -- 12/07/07 First build.
--
property Printing_Run_Count : 0
property Printing_Last_Call : ""
property Printing_Last_Error : ""

property Hold_Jobs_List : {}
property Printer_Status : ""

-- Choose a printer from list
on ListPrinters()
	try
		set Printer_List to {}
		set UNIX_Printers to paragraphs of (do shell script "lpstat -a")
		repeat with i in UNIX_Printers
			set end of Printer_List to word 1 of i
		end repeat
		tell the current application
			choose from list ¬
				Printer_List with prompt "Please choose a printer" without multiple selections allowed
			set Chosen_Printer to the result as string
		end tell
		my scriptInfo()
		return Chosen_Printer as text
	on error errMSG number errNum
		set Printing_Last_Error to errMSG
		return false
	end try
end ListPrinters

-- Get name of previous printer. Change the current printer & get status
on SetPrinter(Printer_Name)
	try
		set Previous_Printer to last word of (do shell script "lpstat -d")
		do shell script "lpoptions -d" & space & Printer_Name
		set Status to do shell script "lpstat -p" & space & Printer_Name
		if last word of Status is "Paused" then
			set Printer_Status to last word of Status
			my OverRide(Printer_Name)
		else
			set Printer_Status to word 4 of Status
			set Hold_Jobs_List to {}
		end if
		my scriptInfo()
		return Previous_Printer as text
	on error errMSG number errNum
		set Printing_Last_Error to errMSG
		return false
	end try
end SetPrinter

-- Returns it back to previous printer with previous settings (well almost)
on ResetPrinter(Previous_Printer)
	try
		if Printer_Status is "Paused" then
			set Current_Printer to last word of (do shell script "lpstat -d")
			set Status to ""
			repeat until Status is "idle"
				set Status to word 4 of (do shell script "lpstat -p" & space & Current_Printer)
				delay 5
			end repeat
			delay 1
			do shell script "disable" & space & Current_Printer
			if Hold_Jobs_List is not {} then
				repeat with Job_ID in Hold_Jobs_List
					do shell script "lp -i" & space & Job_ID & space & "-H resume"
				end repeat
			end if
		end if
		do shell script "lpoptions -d" & space & Previous_Printer
		my scriptInfo()
	on error errMSG number errNum
		set Printing_Last_Error to errMSG
		return false
	end try
end ResetPrinter

-- Set the OS default paper size (print & fax of system preferences)
on PaperSizeOS()
	try
		set Previous_Paper to do shell script "defaults read com.apple.print.PrintingPrefs DefaultPaperID"
		set Paper_List to {"US Letter", "US Legal", "A4", "A5", "ROC 16K", "JB5", "B5", "#10 Envelope", "DL Envelope", "Choukei 3 Envelope", "Tabloid", "A3", "Tabloid Extra", "Super B"}
		set Paper_ID_List to {"na-letter", "na-legal", "iso-a4", "iso-a5", "roc16K", "jis-b5", "iso-b5", "na-number-10-envelope", "iso-designated-long-envelope", "cho-3-envelope", "tabloid", "iso-a3", "arch-b", "arch-b-extra"}
		tell the current application
			choose from list ¬
				Paper_List with prompt "Please choose a paper size" without multiple selections allowed
			set Chosen_Paper to the result as string
		end tell
		repeat with i from 1 to length of Paper_List
			if item i of Paper_List is Chosen_Paper then
				set Paper_ID to item i of Paper_ID_List
				exit repeat
			end if
		end repeat
		do shell script "defaults write com.apple.print.PrintingPrefs DefaultPaperID" & space & Paper_ID
		my scriptInfo()
		return Previous_Paper as text
	on error errMSG number errNum
		set Printing_Last_Error to errMSG
		return false
	end try
end PaperSizeOS

-- Returns OS default paper size (print & fax of system preferences)
on ResetPaperSizeOS(Previous_Paper)
	try
		do shell script "defaults write com.apple.print.PrintingPrefs DefaultPaperID" & space & Previous_Paper
		my scriptInfo()
	on error errMSG number errNum
		set Printing_Last_Error to errMSG
		return false
	end try
end ResetPaperSizeOS

-- Set printer preset (note see commented line for preset name handling)
-- Can be used for all PPD specific options, colour settings, layout, finishing etc.
on PrinterPreset()
	try
		set Previous_Preset to (do shell script "defaults read com.apple.print.custompresets com.apple.print.lastPresetPref")
		-- The line below requires that preset names have no spaces in them.
		set Preset_List to words of (do shell script "defaults read com.apple.print.custompresets com.apple.print.customPresetNames") as list
		tell the current application
			choose from list ¬
				Preset_List with prompt "Please choose a print preset" without multiple selections allowed
			set Chosen_Preset to the result as string
		end tell
		do shell script "defaults write com.apple.print.custompresets com.apple.print.lastPresetPref" & space & Chosen_Preset
		my scriptInfo()
		return Previous_Preset as text
	on error errMSG number errNum
		set Printing_Last_Error to errMSG
		return false
	end try
end PrinterPreset

-- Returns printer preset to previous setting
on ResetPreset(Previous_Preset)
	try
		do shell script "defaults write com.apple.print.custompresets com.apple.print.lastPresetPref" & space & Previous_Preset
		my scriptInfo()
	on error errMSG number errNum
		set Printing_Last_Error to errMSG
		return false
	end try
end ResetPreset

-- Called from SetPrinter(Printer_Name) if printer is paused (Jobs Stopped by user)
on OverRide(Printer_Name)
	try
		set Hold_Jobs_List to {}
		set Print_JobIDs to do shell script "lpstat -o" & space & Printer_Name
		set ASTID to AppleScript's text item delimiters
		set AppleScript's text item delimiters to " "
		repeat with i from 1 to count of paragraphs of Print_JobIDs
			set Job_ID to text items 1 thru 2 of paragraph i of Print_JobIDs as string
			set end of Hold_Jobs_List to Job_ID
			do shell script "lp -i" & space & Job_ID & space & "-H hold"
		end repeat
		set AppleScript's text item delimiters to ASTID
		do shell script "lpadmin -p" & space & Printer_Name & space & "-E"
	on error errMSG number errNum
		set Printing_Last_Error to errMSG
		return false
	end try
end OverRide

-- keep a record of usage
on scriptInfo()
	set Printing_Run_Count to Printing_Run_Count + 1
	set Printing_Last_Call to (current date) as text
end scriptInfo

oop’s forgot the applescript tags

Here is how I got Quark to respond to my preset changes. Requires a bit of UI intervention in the print dialog box I used “Extra Suites” for the mouse stuff may be there is a better way?

property PrintingHandlers : load script ((path to library folder as Unicode text) & "Scripts:Handlers:Printing Handlers.scpt" as text) as alias

if my PrintingHandlers's Version_Number ≠ 1.0 then error "Incorrect version of Printing Handlers.scpt"

set Previous_Preset to my PrintingHandlers's PrinterPreset()
if the result is false then error my PrintingHandlers's Printing_Last_Error as text

tell application "QuarkXPress"
	activate
	tell project 1
		set LayoutName to name of active layout space
		my CaptureSettings(LayoutName)
		tell layout space LayoutName
			print -- with your settings
		end tell
	end tell
end tell

my PrintingHandlers's ResetPreset(Previous_Preset)
if the result is false then error my PrintingHandlers's Printing_Last_Error as text


-- Requires Extra Suites for mouse move & click
on CaptureSettings(LayoutName)
	tell application "System Events"
		tell process "QuarkXPress"
			keystroke "p" using command down
			delay 0.5
			if exists window ("Print " & LayoutName) then
				set Click1 to get position of button "Printer..." of window ("Print " & LayoutName)
				tell application "Extra Suites"
					ES move mouse Click1
					ES click mouse
				end tell
				delay 0.5
				keystroke return
				delay 0.5
				set Click2 to get position of button "Capture Settings" of window ("Print " & LayoutName)
				tell application "Extra Suites"
					ES move mouse Click2
					ES click mouse
				end tell
				delay 3 -- Allow dialog to disappear
			end if
		end tell
	end tell
end CaptureSettings