Archive for the 'Applescript' Category

Don’t Forget Your Chores: Empty the Trash!

If you find your self forgetting to empty the trash on a regular basis, why not automate it? The following applescript can be used to do just that.


tell application "Finder"
empty the trash
end tell


Alternatively, if you wanted to execute the cleanup from a shell script, or a one liner in your crontab, the following one liner could be utilized:


osascript -e ‘tell application “Finder” to empty the trash’


NOTE: In contrast to the command driven instructions represented in the Wikipedia article above, a nice GUI alternative is CronniX.

Folder Action: Moving Files and Keeping Organized

Folder actions are a nice way to save yourself mouse clicks and keystrokes. The applescript below, when assigned to your “Desktop” folder, will move all new torrent files to a subfolder named “torrents”. Now when you download .torrent files from your tracker of choice, you can let Safari, or Firefox, save the file to it’s default location, which is usually the “Desktop” folder. When the file is written to disk, it will be automatically moved for you, keeping your workspace nice and organized.



property fdest : “torrents”

on adding folder items to thefolder after receiving theAddedItems
repeat with eachitem in theAddedItems
tell application “Finder”
if (name of eachitem ends with “.torrent”) then
move eachitem to folder fdest
end if
end tell
end repeat
end adding folder items to


The above applescript can easily be tailored to organize other types of files, such as pictures or music. Remember, a cluttered environment leads to a cluttered mind.

Volume Control

Adjusting the audio volume via applescript can be very useful. Here’s how to raise the volume on your system 10 notches (out of 100):

 -- volume up 10
 set current_volume to output volume of (get volume settings)

 if current_volume is less than 100 then
 	set current_volume to current_volume + 10
 end if

 set volume output volume current_volume

If you are using a using a keyboard that isn’t designed for the Mac and doesn’t have functioning multimedia controls, variations of this script can come in handy when assigned to keyboard shortcuts.

Mount an iDisk via Applescript

Using this snippet, you can easily mount an iDisk from a script. Replace every instance of “USER” and “PASS” with the appropriate values.


tell application "Finder"
mount volume "http://idisk.mac.com/USER/" ¬
as user name "USER" with password "PASS"
end tell

Wednesday One Liner : Screen Saver

Activate your screen saver from a shell script via osascript:


osascript -e ‘tell application “ScreenSaverEngine” to activate’

Alternatively, if you want to quit the screen saver:


osascript -e ‘tell application “ScreenSaverEngine” to quit’

Safari Snippet

Using the code below, you can open a new Safari window for a given url (www.google.com).


tell application "Safari"
make new document at beginning of documents
set URL of document 1 to "http://www.google.com"
end tell

Vienna Article Filtering

Vienna is an excellent rss reader, but it’s not perfect. One of my biggest complaints is it’s lack of a hot key to toggle the display filtering of the articles. There are a variety of methods to assign key combinations to various application and desktop functions, but my preferred method is Quicksilver triggers.

Assign this applescript to a trigger, and you will then be able to use a key combo to cycle thru the 4 filter modes: All Articles, Unread Articles, Last Refresh, and Today. The script cycles thru the 4 modes in order.


tell application "Vienna"
set x to (get filter by)
— set to Unread Articles, if showing All
if (x = 0) then
set filter by to 1
— set to Last Refresh, if showing Unread Articles
else if (x = 1) then
set filter by to 2
— set to Today, if showing Last Refresh
else if (x = 2) then
set filter by to 3
— set to All Articles if showing Today
else if (x = 3) then
set filter by to 0
end if
end tell