3 amazing AppleScripts

Today's Best Tech Deals

Picked by Macworld's Editors

Top Deals On Great Products

Picked by Techconnect's Editors

For simple automation, it’s hard to beat Automator. But if you want to build truly powerful workflows that can do everything from coloring your iCal calendars to converting your e-mail messages for easy reading on your iPod, AppleScript is still the best tool in town. Armed only with Apple’s Script Editor (/Applications/AppleScript) and these three cool scripts, you’ll be amazed at what you can do.

After entering a script in the Script Editor window, choose File: Save, and save the script in the /Library/ Scripts folder. You can also download all these scripts.

1. Color your calendars

Using multiple calendars in iCal is a good idea—that way, you can easily figure out which events are urgent (because they’re in your Critical Projects calendar) and which are optional (because they’re in your New Movie Releases calendar). The problem is, iCal has only seven calendar colors—if you have more than seven calendars, iCal uses these colors multiple times, making it hard to differentiate your calendars at a glance.

Luckily, iCal allows you to script the colors of your calendars—so even if you have 100 calendars, no two will look alike (see “All the Colors of the Rainbow”). Here’s the script:

	—Part 1:
	tell application "iCal"
	set numberOfCalendars to (count every calendar)
	—Part 2:
	repeat with currentNumber from 1 to numberOfCalendars
	set calendarColors to (the color of every calendar)
	—Part 3:
	set redValue to random number 65535
	set greenValue to random number 65535
	set blueValue to random number 65535
	—Part 4:
	repeat until calendarColors does not contain {{redValue, greenValue, blueValue}}
	set redValue to random number 65535
	set greenValue to random number 65535
	set blueValue to random number 65535
	—Part 5:
	set the color of calendar currentNumber to {redValue, greenValue, blueValue}
	end repeat
	end tell

Part 1 of the script counts the number of calendars you have in iCal, and stores the result in the

variable. Part 2 tells AppleScript to go through your calendars, one at a time, and set
to the color of each calendar. Part 3 generates three random numbers that represent the red, green, and blue color channels. A value of 0 means “add none of this color,” and a value of 65535 means “add as much of this color into the mix as you can.” By generating a random value for each color channel, your script can produce trillions of colors.

Part 4 ensures that the randomly chosen color isn’t already taken by an existing calendar. If necessary, the script generates random colors until it finds one that isn’t taken already.

Part 5 sets the color of the current calendar to the randomly chosen color generated in Parts 3 and 4. Then the script jumps back to Part 2, restarting the process for the next calendar.

Once the script has finished running, look at the colors in iCal and see if they suit your fancy. If not, run the script again—it’ll generate a whole new set of colors.

2. See the source

When you’re studying HTML or JavaScript, it can be very helpful to learn by example—that is, by reading the source code of existing Web sites. This script saves the source code of the current Safari page as a separate file in your Documents folder, and then opens the file in the program you choose:

	—Part 1:
	tell application "Safari"
	set siteName to the name of document 1
	set siteSource to the source of document 1
	—Part 2:
	set theFile to open for access ("
	/Users/your user name/Documents/
	" & siteName & ".html") as POSIX file with write permission
	set eof of theFile to 0
	write siteSource to theFile
	close access theFile
	end tell
	—Part 3:
	set theApp to (choose application)
	tell application (theApp as string)
	—Part 4:
	open ("
	/Users/your user name/ Documents/
	" siteName & ".html") as POSIX file
	end tell

Here’s how it works. Part 1 asks for the name and the source code of the current Web page and then stores that information in the

variables, respectively. Part 2 tells AppleScript to create a new file in your Documents folder, naming the file after the current Web page. (Be sure to replace the two instances of your user name with your actual user name.) AppleScript then writes the source code of the Web page into the new file on your hard drive. Finally, the script closes access to the new file, telling OS X that the file is no longer being edited.

Part 3 opens a dialog box where you can choose a program to open the file. The script then sends all subsequent commands to that program. (If you intend to use TextEdit to open the file, you should first go to TextEdit’s Preferences, click on the Open And Save tab, and select the Ignore Rich Text Commands In HTML Files option. That way, TextEdit won’t try to format the file.)

Part 4 brings the program you selected to the foreground and instructs it to open the source-code file. From there, you’re free to learn from the HTML or edit it to your liking.

3. Read new e-mail on your iPod

Most people never use the iPod’s Notes feature (available in third-generation and newer iPods) for anything more sophisticated than shopping lists. But there’s no reason you shouldn’t be able to read new e-mail on your iPod—and that’s what this script does with Apple Mail messages. The script itself is too long to reproduce in print. ( Click here to download.) But a notation of the script follows here.

Part 1 of the script creates a Mail Notes folder on the desktop (if one doesn’t exist already), and deletes any existing items in the folder. Part 2 generates a list of all unread messages in your inbox (

), and puts aside a variable (
) to hold the subject line of each message. Part 3 goes through each unread message and generates a note for it—including the sender, the subject, the date, and the message’s content. (The
section indicates that the subject line of the e-mail message should be used as the title of the iPod note.)

Part 4 creates new files that contain the text generated in Part 3, and puts them in the Mail Notes folder. (Make sure to replace your user name with your actual user name, both here and in Part 6.) The first new file is named 1.txt, the second is named 2.txt, and so on. Part 5 updates the

variable to contain the subject line of the current message. In other words, by the time Part 5 is done running, the
variable will contain a list of every new message’s subject line.

Part 6 creates a new file—0.txt. This holds the list of all new mail messages. Now all you have to do is copy the notes from the Mail Notes folder on your desktop to your iPod’s Notes folder. Then use your iPod to navigate to the New Mail Messages note, and you’ll see a list of all your unread messages—complete with clickable links to each one.

[Adam Goldstein is the author of AppleScript: The Missing Manual and a coauthor of Switching to the Mac: The Missing Manual (both O’Reilly, 2005). He’s also a freshman at MIT.]

All the Colors of the Rainbow: Do you have more calendars than iCal has colors? Spice things up—and make the calendars distinguishable at a glance—with randomly generated colors.
1 2 Page 1
Page 1 of 2
Shop Tech Products at Amazon