10 AppleScripts to make you love your Mac (even more)

Apple's highly useful scripting language is one of the Mac's hidden gems

Search for any term in your clipboard on Computerworld or elsewhere

This is one of my faves: Take any term you've copied into your clipboard (command-C), regardless of the app you're in, and Safari displays search results on that term from Computerworld.com.

To use:

1. Copy the code.

2. Open your script editor (you'll find it in your Applications/AppleScript folder).

3. Paste the code into the larger top portion of your script editor.

4. Save the file as a script in your Library/Scripts folder. (Note: If you open the AppleScript utility in the Applications/AppleScript folder, you can have your script menu display in the top menu bar.)

Code

#This script searches Computerworld.com for the contents of text in the clipboard.

set url1 to "http://www.computerworld.com/action/googleSearch.do?cx=014839440456418836424%3A-khvkt1lc-e&q="

set url2 to "&x=0&y=0&cof=FORID%3A9#1214"

tell application "System Events"

set myquery to the clipboard

end tell

-- changes space to plus using function at end of file

set thequery to SaR(myquery, " ", "+")

tell application "Safari"

activate

tell application "System Events"

tell process "Safari"

click menu item "New Tab" of menu "File" of menu bar 1

end tell

end tell

set theURL to url1 & thequery & url2

set URL of document 1 to theURL

end tell

-- handler (function) for search and replace

-- (posted by James Nierodzik at macscripter.net)

on SaR(sourceText, findText, replaceText)

set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, findText}

set tempText to text items of sourceText

set AppleScript's text item delimiters to replaceText

set sourceText to tempText as string

set AppleScript's text item delimiters to atid

return sourceText

end SaR

If you want scripts that will search for terms in your clipboard at other sites, MacScripter has AppleScript versions posted for searching highlighted text on Googleand Wikipedia (within Safari only). Download and install in your Scripts folder.

Targeted backups

Although Time Machine will do automated backups for those with modern versions of OS X, some readers might want an easy way to, say, keep all files related to a critical project together for an additional "in case of catastrophe" backup, or perhaps for something that could be easily downloaded to a USB drive.

It's pretty intuitive to select files to back up by name or date. For example, take this line of AppleScript:

tell application "Finder" to duplicate (every item in folder "Documents" of home whose modification date comes after (current date) - 7 * days) to folder "backup_docs" of disk "Backups" with replacing

It copies all items in your Documents folder that were modified in the last week to the backup folder on your "Backups" disk; modify the folder locations and dates as needed (note that yesterday would be current date - 1 * days [not day]). "With replacing" tells the script to overwrite any existing files with the same name.

The same "whose" construction can be used to find other things, such as these two statements that can be used for all types of script or image files:

whose name contains "script"

or

whose kind ends with "image".

Since I prefer to select files to back up based on "everything changed since I last ran this script," here's a quick modification created with Sal Soghoian's help:

Code

tell application "Finder"

activate

-- Configure source and destination folders

set source_folder to folder "Documents" of home

set destination_folder to folder "docs" of disk "BackupDisk"

-- Create a config file 1st time script runs to keep track of latest time run

if not (exists file "backup_script_config.txt" in source_folder) then

make new document file at source_folder with properties {name:"backup_script_config.txt"}

duplicate every item in source_folder to destination_folder with replacing

else

set last_run to modification date of file "backup_script_config.txt" in source_folder

duplicate (every item in source_folder whose modification date comes after last_run) to destination_folder with replacing

-- Update config file modification date

set the open_file to open for access file ((document file "backup_script_config.txt" of source_folder) as string) with write permission

set eof of the open_file to 0

write last_run to the open_file starting at eof

close access the open_file

end if

end tell

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

Tags Appleunixsoftware developmentWindowsMac OS Xsnow leopardapplescript

More about BilletworkGoogleMacsMicrosoftReillyWikipedia

Show Comments
[]