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

An AppleScript to quickly stop all print jobs Apps
One thing I have always missed in OS X is the quick way to stop a print job. So this is my "Hello World" AppleScript to stop print jobs. It will delete all print jobs for all users on the local computer:
do shell script "cancel -a -"
Save it as an Apple Script application. I put it on the right edge of my dock. I also made it a login item. This is because my computer is portable and I don't want A's interrupted print job spitting out of B's computer. Works in Panther and Jaguar, untested in Tiger. Works with Appletalk and USB. IP printing often sends too fast to interrupt, but the application will kill a job if the printer is not on the network or turned off at the time you send the job.
    •    
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[10,770 views]  

An AppleScript to quickly stop all print jobs | 16 comments | Create New Account
Click here to return to the 'An AppleScript 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 to quickly stop all print jobs
Authored by: Egrefen on Feb 16, '06 09:45:10AM

Call me naive but can't you simply pull the cable? It's faster than anything, and works every time. Except maybe if you have a wireless printer, or a network printer which you're not directly connected to.

Anywhooo for the vast majority of us using USB printers, nothing works better than a good ol' tug on the USB cable.



[ Reply to This | # ]
An AppleScript to quickly stop all print jobs
Authored by: vindu on Feb 16, '06 12:51:06PM

But does it cancel the jobs cleanly after you pull the cable? What happens when you plug the cable back in?

In general it's better to have a solution that doesn't involve breaking a physical connection between two hardware devices, because in those situations you are not guaranteed a clean break in software terms.



[ Reply to This | # ]
Use UNIX command from terminal
Authored by: asher on Feb 16, '06 03:16:21PM

lpstat -t is a display only command you can run anytime. Try it from terminal. Shows you all printers and printer jobs.
Then you can cancel all or a single job by
cancel PrinterName-JobNumber
where PrinterName and JobNumber come from the output of the lpstat command.
If your connection to the printer is fast or the printer has a large buffer you may find the printer continues to print from its buffer. So I just turn the printer off to clear the buffer.



[ Reply to This | # ]
An AppleScript to quickly stop all print jobs
Authored by: ocdinsomniac on Feb 16, '06 09:54:07AM

This is an awesome hint for me. Managing a lab full of Macs and a network printer, I sometimes get one bad job that jams up printing for the whole network. I used to have to hunt down the computer with the offending job, which was a drag. Now, though, I can just send this command to all our machines and the job will be stopped.

I never even thought to do this. Thanks!

-systemsboy



[ Reply to This | # ]
An AppleScript to quickly stop all print jobs
Authored by: vramin on Feb 16, '06 12:04:44PM
I'd consider making this interactive.
display dialog "Are you sure you want to cancel all of the print jobs in the print queue?" buttons ["Yes", "No"] default button 2
if button returned of result = "Yes" then
	do shell script "cancel -a -"
	display dialog "Poof - no more print jobs" buttons ["OK"] default button 1
end if
end


[ Reply to This | # ]
An AppleScript to quickly stop all print jobs
Authored by: vramin on Feb 16, '06 12:43:36PM
oops... The end at the end of the script in the above post is unnecessary.

[ Reply to This | # ]
An AppleScript to quickly stop all print jobs
Authored by: Lizardmap on Feb 16, '06 08:25:36PM

how do i go about making such an interactive command to stop print jobs? I tried using terminal and it wont allow me to make the command interactive



[ Reply to This | # ]
An AppleScript to quickly stop all print jobs
Authored by: pub3abn on Feb 20, '06 06:56:07AM

That was AppleScript code, not a terminal command.



[ Reply to This | # ]
An AppleScript to quickly stop all print jobs
Authored by: RobertBen on Feb 21, '06 02:51:05PM
I really appreciated this AppleScript version, but didn't care for the first dialog (if I launch this script I want to kill all print jobs a.s.a.p.), so I trimmed it a bit which made it look like this:

do shell script "cancel -a -"
display dialog "No more printing jobs" buttons ["OK"] default button 1


Works like a charm and really fast!

P.S. for AppleScript novices:
I also took away the 'Poof', but the displayed dialog can be any text of course.

[ Reply to This | # ]
An AppleScript to quickly stop all print jobs
Authored by: garythemacguy on Feb 16, '06 01:09:50PM

Hey! Are you psychic? :-)

I had to attend our remaining Mac lab earlier this week to resolve this exact problem - bad, orphaned jobs. I had not got around to researching the solution yet - you have saved me some work there.

Many thanks!



[ Reply to This | # ]
Remaining?
Authored by: jscotta on Feb 16, '06 01:21:37PM

You need more than a psychic! :-)

You need a serious visit by Dr. Steve to save you!

---
Windows because I have to. OS X because I want to.



[ Reply to This | # ]
An AppleScript to quickly stop all print jobs
Authored by: vramin on Feb 17, '06 02:28:11AM

Use the Script Editor, paste in the code above, and save the script as an application. Remember to leave off the last "End".



[ Reply to This | # ]
An AppleScript to quickly stop all print jobs
Authored by: vramin on Feb 17, '06 02:30:31AM

Sorry, my previous response should have been directed to Lizardmap. I'll get it eventually.



[ Reply to This | # ]
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 | # ]