Q: Applescript to move files that are > 30 days old
Hi, I'm looking for a folder action script to move files that are more than 30 days old from my "NEW" file folder at: volumes/files/NEW to my "OLD" file folder: volumes/files/OLD folder. I've searched and have only found scripts to delete old files using the "date modified" info for the files. Any help is appreciated.
Mac mini, OS X Mavericks (10.9.5)
Posted on Aug 3, 2016 12:41 PM
Hi again, thanks for your help Jacques in crafting an applescript. I was not able to open your 2 scripts in my editor without getting an error, however I also had help from Yvan Koenig and Shane Stanley of www.Macscripter.net
and was testing their script when you sent your versions.
They crafted the following script that worked just as I needed it to.
Also, if someone else needs a script to move a file after a certain number of days you can modify the last 3 lines of this code. Instead of "volumes/files/NEW" , input your folder paths; and instead of 30 on the last line, put in any number of days you want, 10, 18, 27 etc. I am using this as an Automator Application to be run as a startup item.
Thanks again for all the help. This one is SOLVED.
The script:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
on moveFilesFrom:posixFolderPath toFolder:destPosixPath numberOfDaysOld:numDays
-- get date limit
set dateLimit to current application's NSDate's dateWithTimeIntervalSinceNow:-(numDays * days)
set dateLimit to current application's NSCalendar's currentCalendar()'s startOfDayForDate:dateLimit
-- make URLs of POSIX paths
set destFolderURL to current application's |NSURL|'s fileURLWithPath:destPosixPath
set sourceFolderURL to current application's |NSURL|'s fileURLWithPath:posixFolderPath
-- get file manager
set theNSFileManager to current application's NSFileManager's defaultManager()
-- get contents of directory, ignoring invisible items
set theURLs to theNSFileManager's contentsOfDirectoryAtURL:sourceFolderURL includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
-- loop through URLs
repeat with aURL in theURLs
-- get date added
set {theResult, theDate} to (aURL's getResourceValue:(reference) forKey:(current application's NSURLAddedToDirectoryDateKey) |error|:(missing value))
-- test date
if theResult as boolean and (theDate's compare:dateLimit) as integer < 0 then
-- get name of file
set theName to aURL's lastPathComponent()
-- make new URL
set destURL to (destFolderURL's URLByAppendingPathComponent:theName)
-- move file
(theNSFileManager's moveItemAtURL:aURL toURL:destURL |error|:(missing value))
end if
end repeat
end moveFilesFrom:toFolder:numberOfDaysOld:
#=====
# Define your own paths here
set posixFolderPath to "volumes/files/NEW"
set destPosixPath to "volumes/files/OLD"
my moveFilesFrom:posixFolderPath toFolder:destPosixPath numberOfDaysOld:30
Posted on Aug 10, 2016 7:40 AM