Author Topic: Printer set up via Shell  (Read 4270 times)

0 Members and 1 Guest are viewing this topic.

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Printer set up via Shell
« on: July 11, 2007, 11:03:30 AM »
Michel, Im currently working on my Printing load script and before I go any further do you know of a shell command that lets you set the system default printer? I've looked in the man pages of "lp" , "lpstat" and "lpadmin" and can't see anything this can't be right? I have a few options that use shell commands and mixing this with "Printer Setup Utility" applescript commands is a bit messy (fixing naming to UNIX no spaces/tabs) etc. Ideally I would like to drop "Printer Setup Utility" all together the only other thing I use this for is status.

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Re: Printer set up via Shell
« Reply #1 on: July 11, 2007, 05:57:08 PM »
Hi Mark,

I haven't done many scripts that needed printing.

I am wondering why you wish to do away with the Printer Setup Utility?
Did you mean that you no longer wish to use it manually or you do not want to include it in your scripts?
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: Printer set up via Shell
« Reply #2 on: July 12, 2007, 03:35:26 AM »
Michel, I will post what I have to date. I initally started this with some Printer Setup Utility stuff which was fairly simple to do, however several things that I would like to have control over are not available to PSU or are r/o. For these I have resorted to using a few shell commands and so far so good. Where I now have an issue that Im attempting to work my way around is what I have works fine with my Xerox DC_3535_DC3535_Print as this has UNIX friendly name. However my other printers do not. It is not safe to assume replace underscores for spaces as in some cases spaces are removed. Getting the UNIX names is not a problem either it is just how to set the default printer to a given name and check status.

The idea with one of the routines "OverRide" is that should the scripted printer have been put on hold by the user then the script will place the jobs on hold first then enable the printer allow the script prints to pass through it then return the queue back to its previous state. Its not exactly right but close enough for now.

I just thought I may have overlooked another man page but the releated pages usually are well linked. May be I will have to use a work around just wanted to get this as tidy as possible.

001   property Version_Number : 1.0 -- 21/06/07 First build.
002   --
003   property Printing_Run_Count : 0
004   property Printing_Last_Call : ""
005   property Printing_Last_Error : ""
006   
007   property Hold_Jobs_List : {}
008   property Printer_Status : ""
009   
010   
011   (* set Choose_Printer to my ListPrinters()
012   if the result is false then error Printing_Last_Error as text
013   set Previous_Printer to my SetPrinter(Choose_Printer)
014   if the result is false then error Printing_Last_Error as text
015   set Previous_Paper to my PaperSizeOS()
016   if the result is false then error Printing_Last_Error as text
017   set Previous_Preset to my PrinterPreset()
018   if the result is false then error Printing_Last_Error as text
019   delay 3
020   tell application "Script Editor"
021        tell document 1
022             print
023        end tell
024   end tell
025   delay 15
026   my ResetPrinter(Previous_Printer)
027   if the result is false then error Printing_Last_Error as text
028   my ResetPaperSizeOS(Previous_Paper)
029   if the result is false then error Printing_Last_Error as text
030   my ResetPreset(Previous_Preset)
031   if the result is false then error Printing_Last_Error as text *)
032   
033   
034   -- Choose a printer from list
035   on ListPrinters()
036        try
037             tell application "Printer Setup Utility"
038                  set Printer_Names to name of every printer as list
039             end tell
040             tell the current application
041                  set Choose_Printer to choose from list ¬
042                       Printer_Names with prompt "Please choose a printer" without multiple selections allowed
043             end tell
044             my scriptInfo()
045             return Choose_Printer as text
046        on error errMSG number errNum
047             set Printing_Last_Error to errMSG
048             return false
049        end try
050   end ListPrinters
051   
052   -- Change the current printer. Get name & status of previous printer
053   on SetPrinter(Printer_Name)
054        try
055             tell application "Printer Setup Utility"
056                  set Previous_Printer to name of the current printer
057                  set the current printer to printer Printer_Name
058                  set Printer_Status to status of current printer -- R/O
059                  if Printer_Status is stopped then
060                       my OverRide(Printer_Name)
061                  else
062                       set Hold_Jobs_List to {}
063                  end if
064             end tell
065             my scriptInfo()
066             return Previous_Printer as text
067        on error errMSG number errNum
068             set Printing_Last_Error to errMSG
069             return false
070        end try
071   end SetPrinter
072   
073   -- Returns it back to previous printer with previous settings
074   on ResetPrinter(Previous_Printer)
075        try
076             tell application "Printer Setup Utility"
077                  if Printer_Status is stopped then
078                       set Current_Printer to name of the current printer
079                       do shell script "disable" & space & Current_Printer
080                  else
081                       set current printer to printer Previous_Printer
082                  end if
083                  if Hold_Jobs_List is not {} then
084                       repeat with Job_ID in Hold_Jobs_List
085                            do shell script "lp -i" & space & Job_ID & space & "-H resume"
086                       end repeat
087                  end if
088             end tell
089             my scriptInfo()
090        on error errMSG number errNum
091             set Printing_Last_Error to errMSG
092             return false
093        end try
094   end ResetPrinter
095   
096   -- Set the OS default paper size (print & fax of system preferences)
097   on PaperSizeOS()
098        try
099             set Previous_Paper to do shell script "defaults read com.apple.print.PrintingPrefs DefaultPaperID"
100             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"}
101             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"}
102             tell the current application
103                  choose from list ¬
104                       Paper_List with prompt "Please choose a paper size" without multiple selections allowed
105                  set Chosen_Paper to the result as string
106             end tell
107             repeat with i from 1 to length of Paper_List
108                  if item i of Paper_List is Chosen_Paper then
109                       set Paper_ID to item i of Paper_ID_List
110                       exit repeat
111                  end if
112             end repeat
113             do shell script "defaults write com.apple.print.PrintingPrefs DefaultPaperID" & space & Paper_ID
114             my scriptInfo()
115             return Previous_Paper as Unicode text
116        on error errMSG number errNum
117             set Printing_Last_Error to errMSG
118             return false
119        end try
120   end PaperSizeOS
121   
122   -- Returns OS default paper size (print & fax of system preferences)
123   on ResetPaperSizeOS(Previous_Paper)
124        try
125             do shell script "defaults write com.apple.print.PrintingPrefs DefaultPaperID" & space & Previous_Paper
126             my scriptInfo()
127        on error errMSG number errNum
128             set Printing_Last_Error to errMSG
129             return false
130        end try
131   end ResetPaperSizeOS
132   
133   -- Set printer preset (note see commented line for preset name handling)
134   on PrinterPreset()
135        try
136             set Previous_Preset to (do shell script "defaults read com.apple.print.custompresets com.apple.print.lastPresetPref")
137             -- The line below requires that preset names have no spaces or numbers in them.
138             set Preset_List to words of (do shell script "defaults read com.apple.print.custompresets com.apple.print.customPresetNames") as list
139             tell the current application
140                  set Choose_Preset to choose from list ¬
141                       Preset_List with prompt "Please choose a print preset" without multiple selections allowed
142             end tell
143             do shell script "defaults write com.apple.print.custompresets com.apple.print.lastPresetPref" & space & Choose_Preset
144             my scriptInfo()
145             return Previous_Preset as Unicode text
146        on error errMSG number errNum
147             set Printing_Last_Error to errMSG
148             return false
149        end try
150   end PrinterPreset
151   
152   -- Returns printer preset to previous setting
153   on ResetPreset(Previous_Preset)
154        try
155             do shell script "defaults write com.apple.print.custompresets com.apple.print.lastPresetPref" & space & Previous_Preset
156             my scriptInfo()
157        on error errMSG number errNum
158             set Printing_Last_Error to errMSG
159             return false
160        end try
161   end ResetPreset
162   
163   -- Called from SetPrinter(Printer_Name) requires UNIX printer names for lpstat
164   on OverRide(Printer_Name)
165        try
166             set Print_Job_List to do shell script "lpstat -o" & space & Printer_Name
167             set ASTID to AppleScript's text item delimiters
168             set AppleScript's text item delimiters to " "
169             repeat with i from 1 to count of paragraphs of Print_Job_List
170                  set Job_ID to text items 1 thru 2 of paragraph i of Print_Job_List as string
171                  copy Job_ID to end of Hold_Jobs_List
172                  do shell script "lp -i" & space & Job_ID & space & "-H hold"
173             end repeat
174             set AppleScript's text item delimiters to ASTID
175             do shell script "lpadmin -p" & space & Printer_Name & space & "-E"
176        on error errMSG number errNum
177             set Printing_Last_Error to errMSG
178             return false
179        end try
180   end OverRide
181   
182   -- keep a record of usage
183   on scriptInfo()
184        set Printing_Run_Count to Printing_Run_Count + 1
185        set Printing_Last_Call to (current date) as text
186   end scriptInfo

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: Printer set up via Shell
« Reply #3 on: July 12, 2007, 03:46:30 AM »
Now I have some numbers I can replace lines 037 thru 039 with a shell command but Im stuck with how to replace lines 057 thru 059 and 077 thru 082.

This is what I've used to check the real printer names

001   property UNIX_Name_List : {}
002   
003   set Printer_List to do shell script "lpstat -a"
004   set ASTID to AppleScript's text item delimiters
005   set AppleScript's text item delimiters to " "
006   repeat with i from 1 to count of paragraphs of Printer_List
007        set UNIX_Name to text item 1 of paragraph i of Printer_List as string
008        copy UNIX_Name to end of UNIX_Name_List
009   end repeat
010   set AppleScript's text item delimiters to ASTID
011   
012   return UNIX_Name_List
« Last Edit: July 12, 2007, 03:58:45 AM by larsen67 »

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: Printer set up via Shell
« Reply #4 on: July 12, 2007, 11:44:31 AM »
It appears that a shell to change the default printer is not something I have foolishly overlooked. I now have used my work around solution for the time being anyhow. I really was expecting between 4 & 12 characters off the top of someone's head. oh well…

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: Printer set up via Shell
« Reply #5 on: July 13, 2007, 05:25:05 AM »
I need to run some tests yet with all the status options but looking good so far. What I required looks like it may be posssible using this:

001   do shell script "lpoptions -d" & space & Printer_Name
002   
003   set Status to do shell script "lpstat -p" & space & Printer_Name
004   if last word of Status is "Paused" then
005        set Printer_Status to last word of Status
006   else
007        set Printer_Status to word 4 of Status
008   end if


Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: Printer set up via Shell
« Reply #6 on: July 16, 2007, 08:01:29 AM »
Well here is what I have ended up with for now.

001   property Version_Number : 1.0 -- 12/07/07 First build.
002   --
003   property Printing_Run_Count : 0
004   property Printing_Last_Call : ""
005   property Printing_Last_Error : ""
006   
007   property Hold_Jobs_List : {}
008   property Printer_Status : ""
009   
010   -- Choose a printer from list
011   on ListPrinters()
012        try
013             set Printer_List to {}
014             set UNIX_Printers to paragraphs of (do shell script "lpstat -a")
015             repeat with i in UNIX_Printers
016                  set end of Printer_List to word 1 of i
017             end repeat
018             tell the current application
019                  choose from list ¬
020                       Printer_List with prompt "Please choose a printer" without multiple selections allowed
021                  set Chosen_Printer to the result as string
022             end tell
023             my scriptInfo()
024             return Chosen_Printer as text
025        on error errMSG number errNum
026             set Printing_Last_Error to errMSG
027             return false
028        end try
029   end ListPrinters
030   
031   -- Get name of previous printer. Change the current printer & get status
032   on SetPrinter(Printer_Name)
033        try
034             set Previous_Printer to last word of (do shell script "lpstat -d")
035             do shell script "lpoptions -d" & space & Printer_Name
036             set Status to do shell script "lpstat -p" & space & Printer_Name
037             if last word of Status is "Paused" then
038                  set Printer_Status to last word of Status
039                  my OverRide(Printer_Name)
040             else
041                  set Printer_Status to word 4 of Status
042                  set Hold_Jobs_List to {}
043             end if
044             my scriptInfo()
045             return Previous_Printer as text
046        on error errMSG number errNum
047             set Printing_Last_Error to errMSG
048             return false
049        end try
050   end SetPrinter
051   
052   -- Returns it back to previous printer with previous settings (well almost)
053   on ResetPrinter(Previous_Printer)
054        try
055             if Printer_Status is "Paused" then
056                  set Current_Printer to last word of (do shell script "lpstat -d")
057                  set Status to ""
058                  repeat until Status is "idle"
059                       set Status to word 4 of (do shell script "lpstat -p" & space & Current_Printer)
060                       delay 5
061                  end repeat
062                  delay 1
063                  do shell script "disable" & space & Current_Printer
064                  if Hold_Jobs_List is not {} then
065                       repeat with Job_ID in Hold_Jobs_List
066                            do shell script "lp -i" & space & Job_ID & space & "-H resume"
067                       end repeat
068                  end if
069             end if
070             do shell script "lpoptions -d" & space & Previous_Printer
071             my scriptInfo()
072        on error errMSG number errNum
073             set Printing_Last_Error to errMSG
074             return false
075        end try
076   end ResetPrinter
077   
078   -- Set the OS default paper size (print & fax of system preferences)
079   on PaperSizeOS()
080        try
081             set Previous_Paper to do shell script "defaults read com.apple.print.PrintingPrefs DefaultPaperID"
082             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"}
083             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"}
084             tell the current application
085                  choose from list ¬
086                       Paper_List with prompt "Please choose a paper size" without multiple selections allowed
087                  set Chosen_Paper to the result as string
088             end tell
089             repeat with i from 1 to length of Paper_List
090                  if item i of Paper_List is Chosen_Paper then
091                       set Paper_ID to item i of Paper_ID_List
092                       exit repeat
093                  end if
094             end repeat
095             do shell script "defaults write com.apple.print.PrintingPrefs DefaultPaperID" & space & Paper_ID
096             my scriptInfo()
097             return Previous_Paper as text
098        on error errMSG number errNum
099             set Printing_Last_Error to errMSG
100             return false
101        end try
102   end PaperSizeOS
103   
104   -- Returns OS default paper size (print & fax of system preferences)
105   on ResetPaperSizeOS(Previous_Paper)
106        try
107             do shell script "defaults write com.apple.print.PrintingPrefs DefaultPaperID" & space & Previous_Paper
108             my scriptInfo()
109        on error errMSG number errNum
110             set Printing_Last_Error to errMSG
111             return false
112        end try
113   end ResetPaperSizeOS
114   
115   -- Set printer preset (note see commented line for preset name handling)
116   -- Can be used for all PPD specific options, colour settings, layout, finishing etc.
117   on PrinterPreset()
118        try
119             set Previous_Preset to (do shell script "defaults read com.apple.print.custompresets com.apple.print.lastPresetPref")
120             -- The line below requires that preset names have no spaces in them.
121             set Preset_List to words of (do shell script "defaults read com.apple.print.custompresets com.apple.print.customPresetNames") as list
122             tell the current application
123                  choose from list ¬
124                       Preset_List with prompt "Please choose a print preset" without multiple selections allowed
125                  set Chosen_Preset to the result as string
126             end tell
127             do shell script "defaults write com.apple.print.custompresets com.apple.print.lastPresetPref" & space & Chosen_Preset
128             my scriptInfo()
129             return Previous_Preset as text
130        on error errMSG number errNum
131             set Printing_Last_Error to errMSG
132             return false
133        end try
134   end PrinterPreset
135   
136   -- Returns printer preset to previous setting
137   on ResetPreset(Previous_Preset)
138        try
139             do shell script "defaults write com.apple.print.custompresets com.apple.print.lastPresetPref" & space & Previous_Preset
140             my scriptInfo()
141        on error errMSG number errNum
142             set Printing_Last_Error to errMSG
143             return false
144        end try
145   end ResetPreset
146   
147   -- Called from SetPrinter(Printer_Name) if printer is paused (Jobs Stopped by user)
148   on OverRide(Printer_Name)
149        try
150             set Hold_Jobs_List to {}
151             set Print_JobIDs to do shell script "lpstat -o" & space & Printer_Name
152             set ASTID to AppleScript's text item delimiters
153             set AppleScript's text item delimiters to " "
154             repeat with i from 1 to count of paragraphs of Print_JobIDs
155                  set Job_ID to text items 1 thru 2 of paragraph i of Print_JobIDs as string
156                  set end of Hold_Jobs_List to Job_ID
157                  do shell script "lp -i" & space & Job_ID & space & "-H hold"
158             end repeat
159             set AppleScript's text item delimiters to ASTID
160             do shell script "lpadmin -p" & space & Printer_Name & space & "-E"
161        on error errMSG number errNum
162             set Printing_Last_Error to errMSG
163             return false
164        end try
165   end OverRide
166   
167   -- keep a record of usage
168   on scriptInfo()
169        set Printing_Run_Count to Printing_Run_Count + 1
170        set Printing_Last_Call to (current date) as text
171   end scriptInfo

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: Printer set up via Shell
« Reply #7 on: July 16, 2007, 08:10:03 AM »
Too have Quark respond to the preset changes I had to make a little handler to do a bit of UI stuff. I used Extra Suites for this. Maybe there is a better way without using this? I can have control over all the PPD specific options available for me to test with my printers anyhow (Xerox3535, HPdeskjet, HP5000) including colour options paper tray duplexing etc.

Heres the Quark handler it may be of use?

001   property PrintingHandlers : load script ((path to library folder as Unicode text) & "Scripts:Handlers:Printing Handlers.scpt" as text) as alias
002   
003   if my PrintingHandlers's Version_Number is not 1.0 then error "Incorrect version of Printing Handlers.scpt"
004   
005   set Previous_Preset to my PrintingHandlers's PrinterPreset()
006   if the result is false then error my PrintingHandlers's Printing_Last_Error as text
007   
008   tell application "QuarkXPress"
009        activate
010        tell project 1
011             set LayoutName to name of active layout space
012             my CaptureSettings(LayoutName)
013             tell layout space LayoutName
014                  print
015             end tell
016        end tell
017   end tell
018   
019   my PrintingHandlers's ResetPreset(Previous_Preset)
020   if the result is false then error my PrintingHandlers's Printing_Last_Error as text
021   
022   
023   -- Requires Extra Suites for mouse move & click
024   on CaptureSettings(LayoutName)
025        tell application "System Events"
026             tell process "QuarkXPress"
027                  keystroke "p" using command down
028                  delay 0.5
029                  if exists window ("Print " & LayoutName) then
030                       set Click1 to get position of button "Printer..." of window ("Print " & LayoutName)
031                       tell application "Extra Suites"
032                            ES move mouse Click1
033                            ES click mouse
034                       end tell
035                       delay 0.5
036                       keystroke return
037                       delay 0.5
038                       set Click2 to get position of button "Capture Settings" of window ("Print " & LayoutName)
039                       tell application "Extra Suites"
040                            ES move mouse Click2
041                            ES click mouse
042                       end tell
043                       delay 3 -- Allow dialog to disappear
044                  end if
045             end tell
046        end tell
047   end CaptureSettings