Posts RSS Comments RSS 48 Posts and 155 Comments till now

Archive for July, 2007

Using AppleScript to set the default printer

Here’s a simple little script that you can use to reset which printer is listed as default. You can set it up as a Login Item and have it run every time you log in to reset the default printer.

Get the name of the printer you want from Printer Setup Utility. You want the actual name displayed in the Printer Setup Window.

In AppleScript the term “current printer” refers to the currently selected printer, which is always the current default printer.

After you have the name place in this script, replacing the words “Epson Inkjet Printer”.

Download the compiled script here:
Default Printer Script

[codesyntax lang=”applescript” lines=”no”]
tell application “Printer Setup Utility”
set the_printer to the current printer
set the_name to the name of the_printer
if the_name is not “Epson Inkjet Printer” then
set the_count to the count of printers
repeat with x from 1 to the_count
if the name of printer x is “Epson Inkjet Printer” then
set the current printer to printer x
end if
end repeat
end if
quit
end tell
[/codesyntax]

The script gets the name of the current default printer. Then it loops through all the printers in the list looking for the name of the one you want. When the script comes to that name it sets it to be the current printer. Calling Printer Setup Utility in a script always launches it so I put a “quit” statement at the end so we don’t have Printer Setup Utility running afterwards.

Using Applescript to kill off a process

I use AppleScript to clean up some temp spaces on my lab machines. It’s easy to use and gives me most of what I need without a lot of shell programming. However, to do some of the cleaning I have to call up the Finder. That’s fine if the machine is already logged in but what if it isn’t? My cleanup script runs around 2 AM each morning when no ones around and no one’s logged in.

The issue with that is since the script runs as root it launchs the Finder as root. And leaves it running as root. So if you were to go to one of those machines in the morning you’d see the Finder running underneath the Login window. If you log in as a normal user it doesn’t restart the Finder, just leaves it running as Root. So, anybody could come along, log in and have complete root privileges to the entire system until they log out. Can you say security issue?

So, I have a small script that I keep around for just such an occasion. It does a search and destroy for the Finder process ID and kills it at the end of the script so we aren’t leaving root hanging around.

[codesyntax lang=”applescript” lines=”no”]
set app_name to “Finder”
set the_pid to (do shell script “ps ax | grep ” & (quoted form of app_name) & ” | grep -v grep | awk ‘{print $1}'”)
if the_pid is not “” then do shell script (“kill -9 ” & the_pid)
[/codesyntax]

First we set the variable “app_name” to the name of the process we want to kill.

Next we set the variable “the_pid” to the process ID of that process. It takes a little fancy shell scripting and text manipulation to get that.

Finally, if we did find it then we send the “kill -9” command with the process ID to kill it off. Using “kill -9” is basically saying “Die and no back talk”.

Now you’ve successfully killed off the Finder running as root and closed the hole. You can use this script to kill off other processes as well.