Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How do I specify path in Applescript?

Trying to implement the following AppleScript:


tell application "Finder"

delete (every item of "Mac HD:Users:bbotticelli:Dropbox:Photos" whose modification date is less than ((get current date) - 7 * days))

end tell


My goal is to run this script to delete any files located in the "Photos" folder that are older than a week. However, AppleScript Editor returns this message when I attempt to run the script:


error "Can’t get {\"M\", \"a\", \"c\", \" \", \"H\", \"D\", \":\", \"U\", \"s\", \"e\", \"r\", \"s\", \":\", \"b\", \"b\", \"o\", \"t\", \"t\", \"i\", \"c\", \"e\", \"l\", \"l\", \"i\", \":\", \"D\", \"r\", \"o\", \"p\", \"b\", \"o\", \"x\", \":\", \"P\", \"h\", \"o\", \"t\", \"o\", \"s\"} whose modification date < date \"Thursday, March 8, 2012 4:30:12 PM\"." number -1728


I've also tried to specify the path as:


tell application "Finder"

delete (every item of folder "Photos" of folder "Dropbox" of folder "bbotticelli" of folder "Users" of startup disk whose modification date is less than ((get current date)) - 7 * days)

end tell


Thank you in advance, any help would be greatly appreciated!

Posted on Mar 15, 2012 1:53 PM

Reply
Question marked as Best reply

Posted on Mar 15, 2012 2:57 PM


tell application "Finder"

delete (every item of "Mac HD:Users:bbotticelli:Dropbox:Photos" whose modification date is less than ((get current date) - 7 * days))

end tell



Should be every item of folder "Mac HD: etc..."


But as you're only looking to delete the files change item to files.


You could also use something like:

get every file of folder "Photo" of folder "Dropbox" of folder (path to home folder) whose modification date is less than (get current date) - 7 * days

This way should the path to the Home folder change or a different users runs it the script still works.

8 replies
Question marked as Best reply

Mar 15, 2012 2:57 PM in response to brianbotticelli


tell application "Finder"

delete (every item of "Mac HD:Users:bbotticelli:Dropbox:Photos" whose modification date is less than ((get current date) - 7 * days))

end tell



Should be every item of folder "Mac HD: etc..."


But as you're only looking to delete the files change item to files.


You could also use something like:

get every file of folder "Photo" of folder "Dropbox" of folder (path to home folder) whose modification date is less than (get current date) - 7 * days

This way should the path to the Home folder change or a different users runs it the script still works.

Mar 15, 2012 3:30 PM in response to brianbotticelli

Just to clarify Frank's answer:


delete (everyitemof "Mac HD:Users:bbotticelli:Dropbox:Photos" ...


You're asking the Finder to get the items of a string... By definition, the items of a string ARE the individual characters.


By adding the keyword 'folder' before the string, AppleScript will coerce this to a folder reference and now 'items of' will retrieve the files/folders within that folder.

Apr 23, 2012 10:38 AM in response to brianbotticelli

The script was working fine for awhile, and now I regularly receive an Apple Event Timed Out error message. The script is:


tell application "Finder"

delete (every item of folder "Mac HD:Users:bbotticelli:Dropbox:Photos" whose modification date is less than ((get current date) - 7 * days))

end tell


Do you see any obvious problems with this the way that it's written? Will this script time out if there are no files older than 7 days?


Apr 23, 2012 11:22 AM in response to brianbotticelli

The obvious question would be how many files are in the folder...


There are two ways around timeout problems. I'd recommend pushing the search off to System Events as follows:


set targetDate to (get current date) - 7 * days

tell application "System Events"

set filesToGo to path of (every disk item of folder "Mac HD:Users:bbotticelli:Dropbox:Photos" whose modification date is less than targetDate and visible is true)

end tell

tell application "Finder" to delete filesToGo

System Events more efficient at tasks like that. Alternately, you could use a 'with timeout' block, which gives the Finder more time to execute:


tell application "Finder"

with timeout of 300 seconds

delete (every item of "Mac HD:Users:bbotticelli:Dropbox:Photos" whose modification date is less than ((get current date) - 7 * days))

end timeout

end tell


but I never like scripting the Finder unless I have to.

Apr 23, 2012 12:01 PM in response to twtwtw

The number of files are anywhere from 0 to approximately 1000. I wasn't sure it had to do with the number of files, because when I ran the script from the AppleScript Editor there would be no activity in the log (it would just say "running"). And, even after receiving the time out error Finder would need a force reload. I appreciate your script for running a System Event, I will try it and let you know how it goes. Thanks!

How do I specify path in Applescript?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.