Posts RSS Comments RSS 37 Posts and 56 Comments till now

Archive for the 'Applescript' Category

Getting user login data from Macs using AppleScript

A lab we work with recently upgraded to 10.5 and found that the program they were running to get the user login data for their billing wouldn’t work in 10.5 and wasn’t going to be upgrading. In looking at the data it was grabbing it was clear the same data could be gotten using the last command in Terminal. You can see more about last at my post here. However, these folks were not computer-savvy and needed the information in a comma-delimited file. So, I came up with this script.

When you run the script it asks you which month you want the data for in a list. Select the month and it will generate a file on the desktop with that months data. You can save it out as an application, run it from Script Editor or run it as a launchd/cron job. You can customize the output by changing the line that begins with “write”. For example, replace the “,” with tab and create a tab-delimited file instead.

If you run this script logged in as a regular user you will only get the login information for that user. If you run it as an administrator you’ll get the login information for every account on the machine.

Click here to download the script file:
Monthly Login accounting

set the_months to {}
set the_total to 0
set the_time to ""
set the_computer to (computer name of (system info))
--get the raw login log information
set the_login to (do shell script "last") as text
set the_count to the count of paragraphs of the_login
--get the list of all the months login data is available for
repeat with x from 1 to the_count
	if paragraph x of the_login contains "console" then
		set month_check to (word 4 of paragraph x of the_login) as text
		if the_months does not contain month_check then
			set end of the_months to month_check
		end if
	end if
end repeat
set selected_month to choose from list the_months with prompt "Select the month:"
--create the file to write data to
if selected_month is not false then
	tell application "Finder"
		set file_name to (the_computer & "-" & selected_month & "-" & (year of (current date)) & " login stats.txt")
		if not (exists file (((path to desktop from user domain as text) & file_name))) then
			set login_file to make file with properties {name:file_name, file type:"TEXT"} at (path to desktop from user domain)
		else
			display dialog "This stats file already exists.  Do you want to overwrite it?" buttons {"Yes", "No"} default button 2
			set the_answer to the button returned of the result
			if the_answer is "Yes" then
				delete file ((path to desktop from user domain as text) & file_name)
				set login_file to make file with properties {name:file_name, file type:"TEXT"} at (path to desktop from user domain)
			end if
		end if
	end tell
	set login_file to ((path to desktop from user domain) & file_name) as text
end if
--loop through the data and pull out the logins for the selected month
repeat with x from 1 to the_count
	if paragraph x of the_login contains selected_month then
		if paragraph x of the_login contains "console" then
			set the_user to (word 1 of paragraph x of the_login)
			set the_month to (word 4 of paragraph x of the_login) as text
			set the_logmonth to MonthNumber(the_month)
			set the_day to (word 5 of paragraph x of the_login) as text
			if (the (count of characters of the_day) is less than 2) then
				set the_day to ("0" & the_day)
			end if
			set the_year to yearCheck(selected_month, the_months)
			set log_date to (the_logmonth & "/" & the_day & "/" & the_year)
			set login_time to (word 6 of paragraph x of the_login & ":" & word 7 of paragraph x of the_login)
			set logout_time to (word 8 of paragraph x of the_login & ":" & word 9 of paragraph x of the_login)
			set old_delims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "("
			try
				set the_time1 to text item 2 of paragraph x of the_login
				set AppleScript's text item delimiters to old_delims
				set the_time to (characters 1 through 5 of the_time1) as text
			on error
				set AppleScript's text item delimiters to old_delims
			end try
			--write all the date to the file
			write (the_user & "," & the_computer & "," & log_date & "," & login_time & "," & log_date & "," & logout_time & "," & the_time & return) to file login_file starting at eof
		end if
	end if
end repeat
--if the list contains January see if the selected month comes before or after January.  Adjust the year accordingly.
on yearCheck(selected_month, the_months)
	if the_months contains "Jan" then
		repeat with x from 1 to count of the_months
			if item x of the_list contains selected_month then
				set the_num to x
			else
				if item x of the_list contains "Jan" then
					set jan_num to x
				end if
			end if
		end repeat
		set year_check to (the_num - jan_num)
		if year_check is greater than 0 then
			set the_year to ((year of (current date)) - 1)
		else
			set the_year to (year of (current date))
		end if
	else
		set the_year to (year of (current date))
	end if
	return the_year
end yearCheck
on MonthNumber(the_month)
	if the_month = "Jan" then
		set the_month to "01"
	else
		if the_month = "Feb" then
			set the_month to "02"
		else
			if the_month = "Mar" then
				set the_month to "03"
			else
				if the_month = "Apr" then
					set the_month to "04"
				else
					if the_month = "May" then
						set the_month to "05"
					else
						if the_month = "Jun" then
							set the_month to "06"
						else
							if the_month = "Jul" then
								set the_month to "07"
							else
								if the_month = "Aug" then
									set the_month to "08"
								else
									if the_month = "Sep" then
										set the_month to "09"
									else
										if the_month = "Oct" then
											set the_month to "10"
										else
											if the_month = "Nov" then
												set the_month to "11"
											else
												if the_month = "Dec" then
													set the_month to "12"
												end if
											end if
										end if
									end if
								end if
							end if
						end if
					end if
				end if
			end if
		end if
	end if
	return the_month as string
end MonthNumber
Add a Link:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • del.icio.us
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • email

Enabling clear text passwords in Snow Leopard with AppleScript

Update: It appears that clear text passwords for AFP connections only work when booted into 32 bit mode. I’ve updated the script to check for which kernel the user is booted into. If they are running 64 bit it asks them if they want to switch to 32 bit. If they say “Yes” then it makes the switch and reboots the machine for them.

A nice article explaining how to see if you are running in 32 or 64 bit mode is here at MacObserver.

There is an Apple Knowledge base article dealing with servers but with good information on switching kernels here.

The procedure for enabling clear text passwords for AFP connections is the same in Snow Leopard as it is in Leopard with one very critical difference. The details about how and why are already in my post on Leopard. If you want the background information you should check out that page. This post will only deal with the Snow Leopard-specific changes.

The big change for enabling clear text passwords for Snow Leopard is that the .plist file is now a binary. This is something Apple has been moving towards since 10.4 and there is a built-in utility that allows you to change the format back and forth to allow for easy editing called “plutil”. The full path to it is “/usr/bin/plutil”

The flag we need to be aware of in “plutil” is the “-convert” flag. There are two formats that we’ll use for this flag, “xml1″ and “binary1″.

To convert the plist file to XML to allow editing we have to run the following command:
/usr/bin/plutil -convert xml1 /Users/joe/Library/Preferences/com.Apple.AppleShareClient.plist

This will convert the file to XML for editing. Now we will do the actual editing. This line is the same as in Leopard.
defaults write com.Apple.AppleShareClient afp_cleartext_allow -bool YES

Now that we have edited the file we have to convert it back to binary form. So we use the “plutil” tool again with a different format:
/usr/bin/plutil -convert binary1 /Users/joe/Library/Preferences/com.Apple.AppleShareClient.plist

Now the preference file is converted back to binary and can be used by the AFP client.

Here is an updated version of the Leopard AppleScript for changing this setting.

If you would prefer to download a pre-complied script file click below:
Snow Leopard Clear Text Script

set afp_pref_path to ((POSIX path of (path to preferences from user domain)) & "com.Apple.AppleShareClient.plist")
set OS_version to (do shell script "sw_vers -productVersion")
set kernel_answer to ""
--check if the user is running 32 or 64 bit kernel.
if OS_version contains "10.6" then
	set kernel_version to (do shell script "/usr/sbin/systemsetup -getkernelbootarchitecturesetting")
	if kernel_version contains "x86_64" then
		set kernel_answer to button returned of (display dialog "You are currently running in 64 bit mode.  Clear text passwords only work in 32 bit mode.  Would you like to change to 32 bit mode?  This will require a restart." buttons {"Yes, change it and restart", "No, just enable clear text"} default button 1)
	end if
end if
try
	set clearStatus to (do shell script "defaults read com.Apple.AppleShareClient afp_cleartext_allow") as number
on error
	--the first command will throw an error if the afp_cleartext_allow setting does not exist
	--if there is an error we'll assume that the setting isn't there and set our variable to the disabled setting
	set clearStatus to 0
end try
--a status of "1" means it's enabled.  So ask if they want to disable it
if clearStatus is 1 then
	display dialog "Do you want to disable clear text passwords?" buttons {"Cancel", "Disable"} default button 2
	if the button returned of the result is "Disable" then
		do shell script "/usr/bin/plutil -convert xml1 " & afp_pref_path
		do shell script "defaults write com.Apple.AppleShareClient afp_cleartext_allow -bool NO"
		do shell script "/usr/bin/plutil -convert binary1 " & afp_pref_path
		set clearStatus to (do shell script "defaults read com.Apple.AppleShareClient afp_cleartext_allow") as number
		--check to make sure the change really took effect
		if clearStatus is 0 then
			display dialog "Clear text passwords have been disabled" buttons {"OK"}
		else
			display dialog "There was an error disabling clear text passwords!" buttons {"OK"}
		end if
	end if
else
	display dialog "Do you want to enable clear text passwords?" buttons {"Cancel", "Enable"} default button 2
	if the button returned of the result is "Enable" then
		do shell script "/usr/bin/plutil -convert xml1 " & afp_pref_path
		do shell script "defaults write com.Apple.AppleShareClient afp_cleartext_allow -bool YES"
		do shell script "/usr/bin/plutil -convert binary1 " & afp_pref_path
		set clearStatus to (do shell script "defaults read com.Apple.AppleShareClient afp_cleartext_allow") as number
		--check to make sure the change really took effect
		if clearStatus is 1 then
			display dialog "Clear text passwords have been enabled" buttons {"OK"}
		else
			display dialog "There was an error enabling clear text passwords!" buttons {"OK"}
		end if
	end if
end if
if kernel_answer contains "Yes" then
	do shell script "/usr/sbin/systemsetup -setkernelbootarchitecture i386" with administrator privileges
	do shell script "/sbin/shutdown -r now" with administrator privileges
end if
Add a Link:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • del.icio.us
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • email

Recommended AppleScript Books for Beginners

I’ve got several AppleScripts on this site and I get lots of questions about things relating to AppleScript. One of the questions I hear is “Are there any good books about AppleScript, especially for beginners”. Well, the answer is yes and no. I’ll give a few recommendations here that I think will help most beginning AppleScripters and possibly some more advanced ones. There are lots of sites out there as well such a Macscripter.net that also have a wealth of information. I encourage anyone looking into AppleScript to use these sites as well as printed materials.

Having said that I know that I personally benefit from having books around. In some cases it just an easy way to quickly look something up. Most books have examples on how to use either a scripting addition or a vanilla AppleScript command. Having those examples makes it a lot easier for me to figure things out.

Danny Goodman’s AppleScript Handbook

This is, in my opinion, the best book still out there on AppleScript. Danny Goodman has been around about as long as the Mac and his knowledge as well as his writing style are unsurpassed. He lays out the basics of AppleScript first and gives tons of examples each time. He steps you through each of the examples and explains what each line is about and why it does what it does. I learn languages best by doing lots of examples and see what things do. This book excels at that.

Many of the examples are short so you don’t get bogged down in code while trying to understand everything. It’s also a fantastic reference book. Whether you’re trying to find out what a list is or how to create a new folder all you have to do is look here. If you’re a beginning AppleScripter and you can only afford one book this is it. He hasn’t updated it in several years but the material is all still very relevant. Just watch out for example for things like “Scriptable Text Editor” since that was replaced long ago by Text Edit.

AppleScript: The Missing Manual

Applescript: The Missing Manual

Another in the “Missing Manual” series this is another great book for beginners. It’s full of great and useful scripting examples that you can use right out of the box. I started writing AppleScripts to do little things that I got tired of doing over and over. You’ll find lots of examples in this book of just that. And along the way you’ll find yourself thinking about how you can adjust those scripts to suit your needs. And THAT is the best way, in my opinion, to learn AppleScript.

The first three chapters of the book are all about getting your feet wet. It starts with showing you where things like Script Editor lives and the wide variety of scripts that already come with your Mac. You’ll open several of those existing scripts and edit them to change what they do. A great way to introduce scripting in my opinion. Change something, see what it does and go from there. The last chapter in that section shows you how to start creating scripts from scratch in Script Editor. You’ll cover things like creating dialog boxes and opening the AppleScript dictionaries of various applications to see what you can do with them.

The second section gets into more of the everyday aspects of AppleScript and what you can do with it. It covers topics such as manipulating text in Text Edit and Microsoft Word and how to move, select and delete files. People who work with graphics on a regular basis will like the sections on controlling Photoshop and iPhoto with AppleScript. I particularly liked the fact that he covers using the built-in Image Events to manipulate graphics. Image Events comes with your OS and lets you do very cool things with graphics without ever opening a program, such as determine color space, rotate the image or even convert it from one format to another.

This section also covers scripting iTunes and Quicktime but only briefly. With AppleScript you can access the features of Quicktime Pro without paying for the Pro license. It would have been nice to see more of that but the Quicktime section gets you started.

The Internet and Networking section has some useful scripts for Internet Connect and Airport but I think most people will find the Mail and iChat scripting section the most useful. The final chapter in this section covers database scripting, in particular making a small AppleScript database and scripting Filemaker Pro.

The third section of the book is labeled as the “Power Users” section but there are lots of things in there that the beginning scripter will find incredibly useful. Things like assigning scripts to folder actions so they run automatically will be immediately useful to many. And the section on running UNIX commands using AppleScript will open up a whole new world to many. There is a very brief chapter on using AppleScript Studio to create applications. It’s enough to get your started if you’re interested but otherwise you would be better off getting an actual AppleScript Studio book.

The most useful part of this section for any scripter has to be the Debugging section. Nothing is more frustrating when writing scripts, especially for new scripters, then getting error messages and having no idea what they mean. Script Editor does a great job of pointing out where the problem is but not in telling you how to fix it. This section covers in detail what many of those error messages mean and how to fix them. It also covers how to trap for errors that may occur and what to do in your script when they do.

AppleScript: A Comprehensive Guide to Scripting and Automation on Mac OS X

This book is one of the more recent and takes things to the next level from the “Missing Manual” series. This book takes you into the meat of AppleScript and shows you how to do really great things like scripting all those command line tools you know are there but don’t want to open Terminal to use. You’ll even learn how to write scripts that can be executed on a remote Mac. This can be really handy when you need to re-start the Filemaker Pro database from across a campus or business. After you’ve gotten an understanding of the basics of AppleScript from the first two books this one will take you to the next level and get you writing more involved and exciting scripts.

These books are just a small sample of the things that are out there but I think these books will give anyone who is starting in AppleScript or wants a more complete understand the foundation they need. As I said, I like books that give lots of real world examples and all of these do exactly that and more.

Add a Link:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • del.icio.us
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • email

Turning your Airport Card on and off

This is a simple tip I stumbled across a month or so ago. There were some bugs in Leopard wireless that wouldn’t let me connect to the encrypted wireless network at work after a machine was restarted. I found that turning the airport card on and off let me connect again. I hated having to remember this every time I restarted so I dug around and found that the command line tool networksetup can do it for me. Great!

This tool exists on Tiger machines in the Apple Remote Desktop client bundle. The path to it is:

/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/networksetup

Fortunately they very kindly included it in the build for Leopard. The path in Leopard is:

/usr/sbin/networksetup

So, in Leopard, to turn the Airport card off enter the following in Terminal:

/usr/sbin/networksetup -setairportpower off

To turn the card back on change “off” to “on”. If you’re running Tiger make sure to change the path so it points to the app correctly.

Put both of these commands in an AppleScript, save it as an application and add it to your Login items. Then, when the machine is rebooted the card gets turned off and then on and in my case makes my wireless connection.

Snow Leopard changes
In Snow Leopard the command remains, however now you need to run it as sudo. You also need to include the actual network device name AirPort is running on. You can get that by running the following command:

/usr/sbin/networksetup -listallhardwareports

You’ll see Airport listed and below it the device. If the machine does not have two Ethernet ports AirPort is commonly listed as “en1″

After you have that you include it in your command:

sudo /usr/sbin/networksetup -setairportpower en1 on

I’ve included these changes and a routine that will find the airport device in the Snow Leopard version of the script.

Click here to download a copy of the script for Tiger/Leopard:
Airport off and on

Click here to download a copy of the script for Snow Leopard:
Snow Leopard Airport off and on

networksetup is a great tool for administrators and even just people who want a little more control over their machines.

Add a Link:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • del.icio.us
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • email

Enabling clear text passwords in Leopard with AppleScript

Note: The method for doing this in Snow Leopard is almost the same but has one slight change to it. Check out the post on doing enabling this in Snow Leopard for the changes.

Leopard, by default, has clear text passwords disabled for AFP connections. This is of course a very good thing to do. No one should be using clear text password connections anymore. However, there are still some older implementations of AFP out there on servers that require a clear text password. So, how do you enable them? By editing a property list or .plist file.

The file in question here is named “com.Apple.AppleShareClient.plist”. It’s located in the Library/Preferences folder in each users home folder. Now, there are a couple of ways to edit this file. If you have the Developer’s Tools installed you can use Property List editor to change that setting from “NO” to “YES”.

Or, if you prefer a command line approach you can use the defaults command to write your settings to the file.

defaults write com.Apple.AppleShareClient afp_cleartext_allow -bool YES

If you’re not sure if clear text passwords are enabled you can use the “read” function in defaults to read the value

defaults read com.Apple.AppleShareClient afp_cleartext_allow

A returned value of “0″ means it is disabled. A value of “1″ means enabled.

If you have a lot of users that need to have this enabled or even checked that’s a lot of typing. So, once again AppleScript to the rescue.

This script will check the status of clear text passwords on launch. If it’s already enabled it will ask if the user wants to disable it. If it’s not enabled it will ask to enable it.

So, just launching the script will let you see if you need to do anything or not. Saving this an application and emailing it to users is a quick way to have them enable it if they need it and then disable it when the need is over without you having to walk over there and type everything a bunch of times.

If you would prefer to download a pre-complied script file click below:

Leopard Clear Text script

try
	set clearStatus to (do shell script "defaults read com.Apple.AppleShareClient afp_cleartext_allow") as number
on error
	-the first command will throw an error if the afp_cleartext_allow setting does not exist
	-if there is an error we'll assume that the setting isn't there and set our variable to the disabled setting
	set clearStatus to 0
end try
-a status of "1" means it's enabled.  So ask if they want to disable it
if clearStatus is 1 then
	display dialog "Do you want to disable clear text passwords?" buttons {"Cancel", "Disable"} default button 2
	if the button returned of the result is "Disable" then
		do shell script "defaults write com.Apple.AppleShareClient afp_cleartext_allow -bool NO"
		set clearStatus to (do shell script "defaults read com.Apple.AppleShareClient afp_cleartext_allow") as number
		-check to make sure the change really took effect
		if clearStatus is 0 then
			display dialog "Clear text passwords have been disabled" buttons {"OK"}
		else
			display dialog "There was an error disabling clear text passwords!" buttons {"OK"}
		end if
	end if
else
	display dialog "Do you want to enable clear text passwords?" buttons {"Cancel", "Enable"} default button 2
	if the button returned of the result is "Enable" then
		do shell script "defaults write com.Apple.AppleShareClient afp_cleartext_allow -bool YES"
		set clearStatus to (do shell script "defaults read com.Apple.AppleShareClient afp_cleartext_allow") as number
		-check to make sure the change really took effect
		if clearStatus is 1 then
			display dialog "Clear text passwords have been enabled" buttons {"OK"}
		else
			display dialog "There was an error enabling clear text passwords!" buttons {"OK"}
		end if
	end if
end if
Add a Link:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • del.icio.us
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • email

Making an alias with AppleScript

This is a simple one but can be very useful.

To make an alias (shortcut) that points to another file you just need the path to file itself and the place you want the alias.

For this example we’ll make an alias of the Apple Mail program on the desktop the current user.

tell application "Finder" to make new alias at (path to desktop folder) to file ((path to applications folder as text) & "Mail")

That’s all there is to it. Remember to take advantage of the built in “path to” calls in the Standard Additions dictionary. In Script Editor go to “Open Dictionary” and choose “Standard Additions” from the list. Type “path” in the search box to find it quickly.

Add a Link:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • del.icio.us
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • email

Check your current IP address with AppleScript

This script will get your current “real world” IP address. It’s very useful when you’re using a router giving out internal addresses and you need to know the address the world is seeing for you. It will also store that IP address so you can have this script run on a schedule and only report if your IP address has changed.

Click here to download the script

property theIP : ""
set TheResult to (do shell script "curl -f http://checkip.dyndns.org")
set Olddelim to AppleScript's text item delimiters
try
	set AppleScript's text item delimiters to "Current IP Address: "
	set LongIP to item 2 of every text item of TheResult as text
	set AppleScript's text item delimiters to ""
	set stopPoint to (offset of "< " in LongIP)
	set myip to characters 1 through (stopPoint - 1) of LongIP as text
        if theIP is "" then
		set theIP to myip
		display dialog "Your IP is : " & myip
	end if
	if myip is not equal to theIP then
		display dialog "Your IP is : " & myip
		set theIP to myip
	end if
	set AppleScript's text item delimiters to Olddelim
on error
	set AppleScript's text item delimiters to Olddelim
end try
Add a Link:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • del.icio.us
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • email

Delete printers using AppleScript

I recently had a request for a script that would delete printers from a lab build. The user wanted to make one system build for all of his labs with all of the printers installed. Then, just run a script to select the correct printer for that lab and delete the others. Here’s the script I came up with. With this script you can choose more then one printer if that is more appropriate.


--get the name of every printer
tell application "Printer Setup Utility"
set theList to the name of every printer as list
end tell

--present the list to choose the printer from
choose from list theList with title "Lab Printer" with prompt "Pick the correct printer for this lab" with multiple selections allowed
set theItem to the result as list

--loop through the list and delete any printer not in the list theItem
repeat with x from 1 to the count of theList
if theItem does not contain (item x of theList) then
do shell script "lpadmin -x " & (item x of theList)
end if
end repeat

Add a Link:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • del.icio.us
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • email

Pinging a server using AppleScript

I’ve recently had a machine that suddenly seems to lose it’s network connection. I can’t figure out why but it always comes back after a reboot. The big problem with this is it’s my backup server and having that off the network is not good. So, I came up with an AppleScript that runs each hour to see if we’re still talking to the world. If not then I reboot the machine.

Here’s the basic script:


try
set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"
on error
delay 60
try
set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"
on error
do shell script "/sbin/shutdown -r now"
end try
end try

Breaking down the script

set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"

This executes the command line version of ping to see if we’re on the network. The “-o” means quit pinging as soon as we get a reply. Don’t want to flood Google with pings! The “-c 5″ says only send 5 pings at the most.

If there is no response for what ever reason it throws an error. So the “on error” part of the script catches that and tells the script to delay itself for 60 seconds and then continue.

After 60 seconds we try again. If we get an error again the we tell the machine to restart itself using “shutdown -r”.

I run this script as a System cron job, running once an hour. By running as system I don’t have to include any passwords and if it needs to shutdown most likely no active processes can get in the way. I use Cronnix to set up my cron jobs. Saving the job as “ping.scpt” the cron job would look like this:

/usr/bin/osascript /path/to/ping.scpt

Add a Link:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • del.icio.us
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • email

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

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

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.

Add a Link:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • del.icio.us
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • email

Next »