Skip to content

Instantly share code, notes, and snippets.

@jrothwell
Last active December 9, 2015 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrothwell/4343791 to your computer and use it in GitHub Desktop.
Save jrothwell/4343791 to your computer and use it in GitHub Desktop.
A simple AppleScript to sweep out the contents of a 'junk' folder and a downloads folder to some external storage (usually a USB/FW/Thunderbolt hard drive or a NAS.) As long as AppleScript can see it as a disk, it should work just fine.
(* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment