rothwell.im

by Jonathan Rothwell

Adventures in AppleScript

Possibly to my peril, I decided I’d spend a little of the Christmas vacation (and the resulting absence of lectures and coursework) to learn AppleScript.

For the uninitiated, AppleScript is an archaic, frankly bizarre scripting language which has existed in the Macintosh OS since System 7, and survived through the later generations of the classic MacOS through to OS X today. Put simply, it’s a ‘natural language’ automation tool: it’s designed to help you automate the exchange of information between applications, and repetitive tasks involving similar data sets.

For my experiment in learning AppleScript, I decided to automate a task I perform quite frequently on my own machine. On any PC I own, I generally have two “junk” directories: there’ll be one Downloads directory, and another called Room 101, where I drag stuff from the desktop that I’m finished with and don’t feel comfortable with Trashing.

Every month or so, for the last two years, I’ve copied the contents of these directories to my external hard disk, freeing up the space on my machine’s main drive. It’s a simple, repetitive task, and it’s therefore ideal for automation with AppleScript.

You can see the results of my efforts here. There’s a lot of copy-and-pasted code here, and the date_format() function is extremely clunky. Furthermore, it doesn’t handle errors very well: this particular script will be unable to deal with folder-merging conflicts.

However, it works, and it works well enough for me to now find regular use for it. I even built in a refinement that means that items labelled grey in Room 101 won’t have their originals trashed: this allows me to keep the original of something I know I’m likely to need, but I still don’t want cluttering up my desktop.

It took some trial-and-error—but it works, and it’s been an interesting experiment in misguided English-like syntax. You’re free to use the code as you please: if you manage to improve on it, then please don’t hesitate to release it and let me know!

sweep.applescriptGist page
(* SWEEP
Moves the contents of your 'junk' and 'downloads' folders to an
external disk, trashing the original copy.
Written 2012/12/20 by Jonathan Rothwell <j@rothwell.im>
Amended 2013/01/02 to fix time-out issue
*)
# Declarations
set diskName to "Catharsis" -- change to your destination volume's label
set junkIn to "Room 101" -- relative path from ~ to your junk/storage directory
set junkOut to "Rooms 101" -- path on your destination disk
set downIn to "Downloads" -- path from ~ to your downloads directory
set downOut to "Old Downloads" -- path to downloads archive on destination disk
set markOfCain to 7 -- the 'mark of Cain'---any file in the junk folder with this label will not be Trashed
tell application "Finder"
if not (exists disk diskName) then
display alert "The disk is not present. Please attach the disk and try again."
error number 1
end if
end tell
set folderDate to date_format()
with timeout of (30 * 60) seconds
tell application "Finder"
set originFolder to folder downIn of home
if not (exists folder folderDate of folder downOut of disk diskName) then
make new folder at folder downOut of disk diskName with properties {name:folderDate}
end if
set targetFolder to folder folderDate of folder downOut of disk diskName
duplicate files of originFolder to targetFolder
duplicate folders of originFolder to targetFolder
# trash everything in Downloads
move files of originFolder to trash
move folders of originFolder to trash
set originFolder to folder junkIn of home
if not (exists folder folderDate of folder junkOut of disk diskName) then
make new folder at folder junkOut of disk diskName with properties {name:folderDate}
end if
set targetFolder to folder folderDate of folder junkOut of disk diskName
duplicate files of originFolder to targetFolder
duplicate folders of originFolder to targetFolder
# trash every file in the junk folder EXCEPT those marked with a label
# note: this is NOT recursive! Anything within another folder will get trashed!
set junkItems to every file of originFolder
repeat with theFile in junkItems
if label index of theFile is not 7 then
move theFile to trash
end if
end repeat
move folders of originFolder to trash
end tell
end timeout
beep
display alert "Sweep complete. You may empty the Trash now."
(* This section uses script samples from the following thread:
http://macscripter.net/viewtopic.php?id=12991
with thanks to contributors cannedbrain and kai.
*)
on date_format()
set {year:y, month:m, day:d} to current date
tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
end date_format