set theFolder to POSIX path of (path to documents folder as text)
set theDays to 30 --set to desired value
set theFolders to getFoldersR(theFolder, theDays)
on getFoldersR(theFolder, theDays) -- Recursive
script F
property oldDate : ((current date) - theDays * days)
on getFolders(aFolder)
local i, myFolders, modDates, foundPaths
tell application "System Events"
set myFolders to properties of folders of folder aFolder whose visible is true
tell myFolders
repeat with i from 1 to count it
set item i to {POSIX path of item i, modification date of item i}
end repeat
end tell
end tell
set foundPaths to {}
repeat with i from 1 to count myFolders
if (item 2 of item i of myFolders) < oldDate then
set end of foundPaths to item 1 of item i of myFolders
set foundPaths to foundPaths & getFolders(item 1 of item i of myFolders, theDays)
end if
end repeat
return foundPaths
end getFolders
end script
if class of theDays is not in {integer, number} then return false
return F's getFolders(theFolder)
end getFoldersR
Thank you, working perfectly. And, although several of the other posted scripts worked as well, for some unknown reason, I can always follow your scripts (with my limited AppleScript experience) a bit easier, so I tend to more easily pick up any changes I’d like to make. Thanks for always helping me along.
Just need a push in the correct direction here. I’ve modified your script to select a specific folder:
set theFolder to POSIX path of "Volumes:Mac Mini Local Backups:Sync Folders Local Backups:Local Documents Archive:"
set theDays to 60 --set to desired value
set theFolders to getFolders(theFolder, theDays)
writeFile(theFolders)
Is it now possible to somehow string together five additional folders?
Example: Local Dropbox Folders Archive, Local Installs Archive, Local Movies Archive, Local Music Archive & Local Pictures Archive
Your path is wrong.
Since it has colons as the folder separator, it is an HFS path.
It should be “ Mac Mini Local Backups:Sync Folders Local Backups:Local Documents Archive:"
Only POSIX paths need to start zwith “/Volumes/“
Is it now possible to somehow string together five additional folders?
Yes, by using a repeat loop. I’ll make an example in a little while.
Can you give the full paths of these folders?
Mac Mini Local Backups:Sync Folders Local Backups:Local Documents Archive:
Mac Mini Local Backups:Sync Folders Local Backups:Local Dropbox Folders Archive:
Mac Mini Local Backups:Sync Folders Local Backups:Local Installs Archive:
Mac Mini Local Backups:Sync Folders Local Backups:Local Movies Archive:
Mac Mini Local Backups:Sync Folders Local Backups:Local Music Archive:
Mac Mini Local Backups:Sync Folders Local Backups:Local Pictures Archive:
set theDays to 30 --set to desired value
set masterFolder to "Mac Mini Local Backups:Sync Folders Local Backups:"
repeat with aFolder in {"Local Documents Archive:", "Local Dropbox Folders Archive:", "Local Installs Archive:", "Local Movies Archive:", "Local Music Archive:", "Local Pictures Archive:"}
set theFolders to getFoldersR(masterFolder & aFolder, theDays)
-- do what you need to do to the folders found here
end repeat
What needs to go there is this script, but no matter what I change/modify I keep getting complie errors.
use framework "Foundation"
use scripting additions
set theFolder to POSIX path of (choose folder)
set theDays to 60 --set to desired value
set theFolders to getFolders(theFolder, theDays)
writeFile(theFolders)
on getFolders(theFolder, theDays)
set fileManager to current application's NSFileManager's defaultManager()
set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
set dateKey to current application's NSURLCreationDateKey --or use NSURLContentModificationDateKey
set directoryKey to current application's NSURLIsDirectoryKey
set packageKey to current application's NSURLIsPackageKey
set folderContents to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:6 errorHandler:(missing value))'s allObjects() --option 6 skips hidden files and package contents
set filterDate to current application's NSDate's dateWithTimeIntervalSinceNow:(-86400 * theDays)
set theFolders to current application's NSMutableArray's new()
repeat with anItem in folderContents
set {theResult, aDirectory} to (anItem's getResourceValue:(reference) forKey:directoryKey |error|:(missing value))
if (aDirectory as boolean) is true then
set {theResult, aPackage} to (anItem's getResourceValue:(reference) forKey:packageKey |error|:(missing value))
if (aPackage as boolean) is false then
set {theResult, aDate} to (anItem's getResourceValue:(reference) forKey:dateKey |error|:(missing value))
if (aDate's compare:filterDate) = -1 then (theFolders's addObject:anItem)
end if
end if
end repeat
return (theFolders's valueForKey:"path")'s sortedArrayUsingSelector:"localizedStandardCompare:"
end getFolders
on writeFile(theArray)
set theFile to POSIX path of ((path to desktop as text) & "Old Folders.txt") --set to desired value
set theString to theArray's componentsJoinedByString:linefeed
(current application's NSString's stringWithString:theString)'s writeToFile:theFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
end writeFile
That’s not my script. That the AppleScript-ObjC version. Hopefully Peavine can help.
I’ll put up the complete version of mine later
Here it is…
set theDays to 30 --set to desired value
set masterFolder to "Mac Mini Local Backups:Sync Folders Local Backups:"
repeat with aFolder in {"Local Documents Archive:", "Local Dropbox Folders Archive:", "Local Installs Archive:", "Local Movies Archive:", "Local Music Archive:", "Local Pictures Archive:"}
set theFolders to getFoldersR(masterFolder & aFolder, theDays)
-- do what you need to do to the folders found here
if (count theFolders) > 0 then writeFile(theFolders)
end repeat
on getFoldersR(theFolder, theDays) -- Recursive with 'Whose' clause - FASTEST
script F
property oldDate : ((current date) - theDays * days)
on getFolders(aFolder)
local i, folderPaths, modDates, foundPaths
tell application "System Events"
set folderPaths to properties of folders of folder aFolder whose visible is true
tell folderPaths
repeat with i from 1 to count it
set item i to {POSIX path of item i, modification date of item i}
end repeat
end tell
end tell
set foundPaths to {}
repeat with i from 1 to count folderPaths
if (item 2 of item i of folderPaths) < oldDate then
set end of foundPaths to item 1 of item i of folderPaths
set foundPaths to foundPaths & getFolders(item 1 of item i of folderPaths, theDays)
end if
end repeat
return foundPaths
end getFolders
end script
if class of theDays is not in {integer, number} then return false
return F's getFolders(theFolder)
end getFoldersR
on writeFile(theFolders)
local tid, outfile, fileEnd
set {tid, text item delimiters} to {text item delimiters, linefeed}
set outfile to (path to desktop as text) & "Old Folders.txt"
try
set outfile to open for access outfile with write permission
on error
return false
end try
set fileEnd to get eof outfile + 1
write (theFolders as text) & linefeed to outfile starting at fileEnd
close access outfile
set text item delimiters to tid
end writeFile
Homer712. I’m not entirely sure I understand your request. The following allows you to specify multiple source folders with HFS paths in a list.
use framework "Foundation"
use scripting additions
set theFolders to {"Macintosh HD:Users:robert:Documents:", "Macintosh HD:Users:robert:Downloads:"} --set to desired values
set theDays to 30 --set to desired value
set theOldFolders to current application's NSMutableArray's new()
repeat with aFolder in theFolders
set aFolder to POSIX path of aFolder
set someOldFolders to getFolders(aFolder, theDays)
(theOldFolders's addObjectsFromArray:someOldFolders)
end repeat
theOldFolders's sortUsingSelector:"localizedStandardCompare:" --delete if desired
writeFile(theOldFolders)
on getFolders(theFolder, theDays)
set fileManager to current application's NSFileManager's defaultManager()
set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
set dateKey to current application's NSURLCreationDateKey --or use NSURLContentModificationDateKey
set directoryKey to current application's NSURLIsDirectoryKey
set packageKey to current application's NSURLIsPackageKey
set folderContents to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:6 errorHandler:(missing value))'s allObjects() --option 6 skips hidden files and package contents
set filterDate to current application's NSDate's dateWithTimeIntervalSinceNow:(-86400 * theDays)
set theFolders to current application's NSMutableArray's new()
repeat with anItem in folderContents
set {theResult, aDirectory} to (anItem's getResourceValue:(reference) forKey:directoryKey |error|:(missing value))
if (aDirectory as boolean) is true then
set {theResult, aPackage} to (anItem's getResourceValue:(reference) forKey:packageKey |error|:(missing value))
if (aPackage as boolean) is false then
set {theResult, aDate} to (anItem's getResourceValue:(reference) forKey:dateKey |error|:(missing value))
if (aDate's compare:filterDate) = -1 then (theFolders's addObject:anItem)
end if
end if
end repeat
return theFolders's valueForKey:"path"
end getFolders
on writeFile(theArray)
set theFile to POSIX path of ((path to desktop as text) & "Old Folders.txt") --set to desired value
set theString to theArray's componentsJoinedByString:linefeed
(current application's NSString's stringWithString:theString)'s writeToFile:theFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
end writeFile
I looked at Robert’s script and I think I understand now. My revised suggestion:
use framework "Foundation"
use scripting additions
set theDays to 30 --set to desired value
set theMasterFolder to "Mac Mini Local Backups:Sync Folders Local Backups:" --set to desired value
set theSubfolders to {"Local Documents Archive:", "Local Dropbox Folders Archive:", "Local Installs Archive:", "Local Movies Archive:", "Local Music Archive:", "Local Pictures Archive:"} --set to desired values
set theOldFolders to current application's NSMutableArray's new()
repeat with aSubfolder in theSubfolders
set aFolder to POSIX path of (theMasterFolder & aSubfolder)
set theseFolders to getFolders(aFolder, theDays)
(theOldFolders's addObjectsFromArray:theseFolders)
end repeat
theOldFolders's sortUsingSelector:"localizedStandardCompare:" --delete if desired
writeFile(theOldFolders)
on getFolders(theFolder, theDays)
set fileManager to current application's NSFileManager's defaultManager()
set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
set dateKey to current application's NSURLCreationDateKey --or use NSURLContentModificationDateKey
set directoryKey to current application's NSURLIsDirectoryKey
set packageKey to current application's NSURLIsPackageKey
set folderContents to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:6 errorHandler:(missing value))'s allObjects() --option 6 skips hidden files and package contents
set filterDate to current application's NSDate's dateWithTimeIntervalSinceNow:(-86400 * theDays)
set theFolders to current application's NSMutableArray's new()
repeat with anItem in folderContents
set {theResult, aDirectory} to (anItem's getResourceValue:(reference) forKey:directoryKey |error|:(missing value))
if (aDirectory as boolean) is true then
set {theResult, aPackage} to (anItem's getResourceValue:(reference) forKey:packageKey |error|:(missing value))
if (aPackage as boolean) is false then
set {theResult, aDate} to (anItem's getResourceValue:(reference) forKey:dateKey |error|:(missing value))
if (aDate's compare:filterDate) = -1 then (theFolders's addObject:anItem) --use 1 for newer than
end if
end if
end repeat
return theFolders's valueForKey:"path"
end getFolders
on writeFile(theArray)
set theFile to POSIX path of ((path to desktop as text) & "Old Folders.txt") --set to desired value
set theString to theArray's componentsJoinedByString:linefeed
(current application's NSString's stringWithString:theString)'s writeToFile:theFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
end writeFile
Thank you. I also got your first script (post #30) working, but all that typing left room for error (see below). This latest one minimizes typing errors and produces the same exact list of files older than the 60 days. I’m good, again, thanks.
use framework "Foundation"
use scripting additions
set theFolders to {"MacBook Pro Local Backups:Sync Folders Local Backups:Local Documents Archive:", ¬
"MacBook Pro Local Backups:Sync Folders Local Backups:Local Dropbox Folders Archive:", ¬
"MacBook Pro Local Backups:Sync Folders Local Backups:Local Installs Archive:", ¬
"MacBook Pro Local Backups:Sync Folders Local Backups:Local Movies Archive:", ¬
"MacBook Pro Local Backups:Sync Folders Local Backups:Local Music Archive:", ¬
"MacBook Pro Local Backups:Sync Folders Local Backups:Local Photos Archive Archive:", ¬
"MacBook Pro Local Backups:Sync Folders Local Backups:Local Pictures Archive:"} --set to desired values
set theDays to 60 --set to desired value
set theOldFolders to current application's NSMutableArray's new()
repeat with aFolder in theFolders
set aFolder to POSIX path of aFolder
set someOldFolders to getFolders(aFolder, theDays)
(theOldFolders's addObjectsFromArray:someOldFolders)
end repeat
I have 1 question for you Homer. If a folder is not old, do you still want to go thru the subfolders that are in it? If you do, then in my script one line has to be moved.
Robert, in the case of the “Archive” folders, those were first created a few years ago when I first started fooling around with rsync (and rsync does not recreate them at any point), so I think not.
Also, you never answered if you want created dates or modified dates. Or did I miss that post?
BTW I erred a bit. My post 19 is actually faster. I’ve found that using whose or where clauses slow down ‘System Events’ and ‘Finder’. It’s best (i.e. faster) to get everything, then parse out what you don’t need in AppleScript. Especially on returned large lists
@Homer712, out of curiosity, why are you doing this in the first place?
If it’s for backup, why not just Time Machine backup to an external drive, or use SuperDuper similarly?
If it’s for removing backup logs, store them in various /var/tmp folders and use aliases in your home account to them for easy access.
If something else, wouldn’t @Mockman’s solution be your fastest solution? No AS solution is going to (speed-wise) match up with than the BSD app (though I find @StefanK’s SpotLight post very intriguing as a dB solution).
There are seven additional routines that backup additional files, but the folder structure is always “Local Documents” and “Local Documents Archive”. The rsync routine backs up to the main folder “Local Documents” and any changed/deleted files are then backed up to the “Local Documents Archive” folder in a “dated” folder. The routines in this post was an attempt to make sure that any files in the “Local Documents Archive” folder that are older than 90 days, get deleted.
As for the “why”, and I think I may have posted something similar in another thread, I purchased my first Macintosh in 1985, after seeing Steve Jobs give his presentation at the Boston Computer Society. And back then, HD failure was not an if, but when. Over those first 10 to 15 years, I like many others, lost incredibly high value files to HD failures. Those losses left me somewhat paranoid. I now use CCC, ChronoSync, Time Machine, SuperDuper!, as well as rsync routines that I have developed for my own use, just in case an update to any of the commercial software I use hits a bump in the road.