Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'An AppleScript routines to quickly stop all print jobs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript routines to quickly stop all print jobs
Authored by: szedula on Feb 17, '06 08:09:04AM
This hint inspired me. I couldn't resist the urge to tinker together a few AppleScript routines to manipulate the printer. This is what I came up with:

-- examples using "PrinterTools"
on run
  
  -- get list of printers
  set oldDelims to AppleScript's text item delimiters
  set AppleScript's text item delimiters to {", "}
  set out to "Printers:" & return & " " & GetPrinterList() as text
  set AppleScript's text item delimiters to oldDelims
  
  -- get default printer
  set DefaultPrinter to GetDefaultPrinter()
  set out to out & return & return & "Default Printer: " & DefaultPrinter
  
  -- get name of job printing on default printer
  set JobPrinting to GetPrintJobName(DefaultPrinter)
  set out to out & return & return & "Printing: " & JobPrinting
  
  -- select a printer
  set myChoice to SelectPrinter()
  set out to out & return & return & "Selected Printer: " & myChoice
  
  -- get status of all the printers
  set out to out & return & return & FormatPrinterStatus("")
end run

-- cancel print jobs
on CancelExamples()
  
  -- solution #1: cancel everything
  -- (draconian solution)
  CancelPrintJob("")
  
  -- solution #2: cancel print job of default printer
  -- (assumes default printer is problem)
  CancelPrintJob(GetPrintJobName(GetDefaultPrinter()))
  
  -- solution #3: cancel job on selected printer
  -- (the printer with the problem?)
  CancelPrintJob(GetPrintJobName(SelectPrinter()))
end CancelExamples

-- select a printer
on SelectPrinter()
  set DefaultPrinter to GetDefaultPrinter()
  set thePrinters to GetPrinterList()
  
  set myChoice to (choose from list thePrinters with prompt 
    "Choose a Printer" default items DefaultPrinter 
    without multiple selections allowed) as text
  if myChoice = "false" then return "--no printer--" -- "cancel" clicked
  
  return myChoice
end SelectPrinter

-- cancel selected print job
-- if "theJob" is "" all jobs are canceled
on CancelPrintJob(theJob)
  if (theJob = "") then
    set CancelJob to "-a"
  else
    set CancelJob to theJob
  end if
  try
    set theResult to do shell script "cancel " & CancelJob
  on error
    set theResult to "--Failed to cancel job: " & CancelJob & "--"
  end try
  return theResult
end CancelPrintJob

-- format the printer status for display
on FormatPrinterStatus(thePrinter)
  set theList to GetPrinterStatus(thePrinter) as list
  set out to "Printer Status: "
  repeat with j from 1 to count theList
    set printer to item j of theList
    repeat with i from 1 to count of items in printer
      if (i = 1) then
        if not (out = "") then
          set out to out & return
        end if
        set out to out & " - " & item 1 of printer & " is: "
      else if (i = 2) then
        set out to out & item 2 of printer
      else
        set out to out & "/" & item i of printer
      end if
    end repeat
  end repeat
  return out
end FormatPrinterStatus

-- get printer(s) and whether or not they are enabled for printing
-- if "thePrinter" is "" all printers are returned
on GetPrinterStatus(thePrinter)
  set oldDelims to AppleScript's text item delimiters
  set PrinterStatusList to {}
  try
    set cmd to "lpstat -p " & thePrinter & " | grep ^printer"
    set PrinterStatus to do shell script cmd as text
    set AppleScript's text item delimiters to {" "}
    repeat with i from 1 to count of paragraphs of PrinterStatus
      set s to paragraph i of PrinterStatus
      set p to text item 2 of s as list
      if (s contains "idle") then
        copy "idle" to end of p
      end if
      if (s contains "enabled") then
        copy "enabled" to end of p
      end if
      if (s contains "disabled") then
        copy "disabled" to end of p
      end if
      if (s contains "now printing") then
        copy "printing" to end of p
      end if
      copy p to end of PrinterStatusList
    end repeat
  on error
    set PrinterStatus to ""
  end try
  set AppleScript's text item delimiters to oldDelims
  return PrinterStatusList
end GetPrinterStatus

-- get a list of the connected printers
on GetPrinterList()
  set oldDelims to AppleScript's text item delimiters
  set AppleScript's text item delimiters to {ASCII character 10}
  try
    set PrinterListText to do shell script "lpstat -a | awk '{print $1}'" as text
  on error
    set PrinterListText to ""
  end try
  set PrinterList to (paragraphs of PrinterListText)
  set AppleScript's text item delimiters to oldDelims
  return PrinterList as list
end GetPrinterList

-- get name of currently printing job of selected printer
on GetPrintJobName(thePrinter)
  set oldDelims to AppleScript's text item delimiters
  try
    set PrintJobName to do shell script "lpstat -t | grep " & thePrinter & " | grep printer" as text
  on error
    set PrintJobName to "--Can't get status of: '" & thePrinter & "'--"
  end try
  if (PrintJobName starts with (("printer " & thePrinter & " now printing") as text)) then
    set AppleScript's text item delimiters to {" "}
    set PrintJobName to text item 5 of PrintJobName
    set AppleScript's text item delimiters to {""}
    set PrintJobName to (characters 1 thru ((count of PrintJobName) - 1) of PrintJobName) as text
    set AppleScript's text item delimiters to oldDelims
  else
    set PrintJobName to "--" & thePrinter & ", not printing--"
  end if
  return PrintJobName
end GetPrintJobName

-- get name of default printer
on GetDefaultPrinter()
  set oldDelims to AppleScript's text item delimiters
  set AppleScript's text item delimiters to {": "}
  try
    set DefaultPrinter to do shell script "lpstat -d" as text
    set DefaultPrinter to last text item of DefaultPrinter as text
  on error
    set DefaultPrinter to "--no default printer--"
  end try
  set AppleScript's text item delimiters to oldDelims
  return DefaultPrinter
end GetDefaultPrinter


[ Reply to This | # ]
An AppleScript routines to quickly stop all print jobs
Authored by: vramin on Feb 21, '06 12:18:50PM

szedula-

This looks interesting, but didn't work as I expepcted. Are you using an Applescript add-on?



[ Reply to This | # ]
An AppleScript routines to quickly stop all print jobs
Authored by: szedula on Mar 11, '06 08:20:42AM

I don't know what you expected. The "on run" routine executes the routines in the library and saves the results to the variable "out." You can see the results when running the script fom within "Script Editor."

The library can be used to assemble your own scripts to do what you want. For example you could write scripts to:
- cancel print jobs (see routine "CancelExamples")
+ all
+ default printer
+ selected printer
- set a selected printer as the new default
+ using a list of available printers
+ specific printer based upon Mac network address (home or work)
- display printer status

This library is not a complete application. Some "assembly is required" to do what you want.

Hope this helps.



[ Reply to This | # ]