Friday, October 22, 2021

#1 2008-05-05 02:00:00 am

Martin Michel
Administrator
From:: Berlin, Germany
Registered: 2008-03-03
Posts: 701
Website

Switch the printer preset in the print dialog

Lately a fellow Mac user contacted me and kindly asked, if it would be possible to automatically switch the currently used printer preset in the print dialog. Working in the graphics industry, he necessarily needs to use specific printer settings for his print jobs, that are all saved in various printer presets.

So I did some research on the topic and quickly found this helpful article on Mac OS X Hints, that describes how to easily switch printer presets in Mac OS X by manipulating a preferences file using the «do shell script» command.

After having a closer look at the «com.apple.print.custompresets.plist» file in my own preferences folder, I wrote a simple sample script which lets you choose a printer preset from a list and then sets this printer preset as the currently used printer preset:

The script contains two AppleScript functions:
1. One for getting a list of all available printer presets (getcustompresetnames)
2. for setting the currently used printer preset (setcurpresetname)

It was successfully tested on Mac OS X 10.5.2, but may also work on earlier incarnations of our beloved operating system.

Applescript:


-- created: 21.03.2008

property mytitle : "Set printer preset"

-- I let the user select a printer preset to be set as the current printer preset
on run
   -- getting a list of available printer presets
   set custompresetnames to my getcustompresetnames()
   -- no printer presets found :(
   if custompresetnames is {} then
       tell me
           activate
           display dialog "Sorry, we could not find any printer preset names." buttons {"OK"} default button 1 with icon stop with title mytitle
       end tell
       -- printer presets found
   else
       choose from list custompresetnames with prompt "Please choose a printer preset:" with title mytitle without multiple selections allowed and empty selection allowed
       set choice to result
       if choice is not false then
           set presetname to item 1 of choice
           -- settings the printer preset
           my setcurpresetname(presetname)
       end if
   end if
end run

-- I am returning all preset names available in the print dialog
-- (in case the corresponding preferences file does not yet exist
-- or no preset names are listed, I will return an empty list)
on getcustompresetnames()
   set custompresetnames to {}
   set prefsfolderpath to POSIX path of ((path to preferences folder from user domain) as Unicode text)
   set prefsfilepath to prefsfolderpath & "com.apple.print.custompresets.plist"
   try
       tell application "System Events"
           set fileobj to property list file prefsfilepath
           set custompresetnames to value of property list item "com.apple.print.customPresetNames" of contents of fileobj
       end tell
   end try
   return custompresetnames
end getcustompresetnames

-- I am switching the current printer preset to the given preset name
on setcurpresetname(presetname)
   set cmd to "defaults write com.apple.print.custompresets com.apple.print.lastPresetPref -string " & quoted form of presetname
   set cmd to cmd as «class utf8»
   do shell script cmd
end setcurpresetname


Plonum - Convert images to iWork Numbers documents

Offline

 

#2 2009-02-22 05:43:30 am

Simon Tännler
Member
From:: Switzerland
Registered: 2008-03-25
Posts: 23

Re: Switch the printer preset in the print dialog

Hey there

Martin, thanks for this script. I was trying to do something similar but didn't came up with a solution this cool.

But right now I have another problem:

I'm launching your script and change the preset. Then I print a file but it's not printed using the preset I've chosen in your script. If I check the plist file the settings are changed, and also if I manually want to print a file (using "apple + p") the preset is selected. I think that printing in applescript always use the "Standard" preset, and not the settings in the plist....but I'm just guessing. Any idea how I can use my presets in applescript?...or automator, or any other automated solution?

Applescript:

with timeout of 600 seconds
   tell application "PrinterPresetter"
       launch
       run
   end tell
end timeout

tell application "Finder"
   
   set theFile to "/Users/simon/Desktop/KomSys-UZH-Merkzettel.pdf"
   
   set theFile to ((POSIX file theFile) as alias)
   
end tell

with timeout of 10 seconds
   tell application "Printer Setup Utility"
       activate
       open theFile
   end tell
end timeout

thanks, Simon

Offline

 

#3 2009-02-25 02:14:39 am

Simon Tännler
Member
From:: Switzerland
Registered: 2008-03-25
Posts: 23

Re: Switch the printer preset in the print dialog

Offline

 

#4 2021-07-18 10:33:09 am

Scripted
Member
Registered: 2021-07-12
Posts: 9

Re: Switch the printer preset in the print dialog

Hi Martin, I googled and found your post.  I tried to run it and got the following error:

Expected class name but found unknown token

And the letter  is selected in the following line:

set cmd to cmd as «class utf8»

Can you help?

Offline

 

#5 2021-07-18 11:48:08 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5431

Re: Switch the printer preset in the print dialog

Hi.

Martin's script was posted thirteen years ago, since when a lot has changed — including MacScripter's text encoding. It would be simple to correct the characters, but in this case I'd recommend not simply running an ancient presets script and hoping it works.


NG

Offline

 

#6 2021-07-18 02:37:09 pm

Scripted
Member
Registered: 2021-07-12
Posts: 9

Re: Switch the printer preset in the print dialog

Hi.  Do you know of a more updated version of this script?

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)