How to limit items in a folder to only those less than 30 days old?

A few years ago, I had run across a folder action that would check items in a backup folder (backups saved there once a day, which triggered the action) and delete those older than 30 days, or at least the oldest one if the count was over 30.

I’ve tried learning scripting backwards, by trying to create a solution to this need, only to find I don’t get anywhere without more foundation in scripting. (It was simpler to learn 8080 op-codes than applescript!). I really do need this solution, and could really use a reference that taught by example. My brain just wants to close down when the initial discussions revolve around value classes constants operators and so on, so one that sneaks that in while showing scripting by example would suit me well.

If you could help me with either or both items you’d be doing me a service.

Many Thanks!

Steve J

Steve:

Welcome to the boards! I actually believe (hell, I’ve made a career out of it :P) that your “backward” way is the easiest way to learn to script. All the abstract concepts are confusing for a beginner… you just know what you need to get done. Scripting examples that have no real world use (or are so simple that a script seems overkill) are of little practical value as well. To people who ask, “How do you know how to do this stuff?” i say, “I just get an idea of what I want the computer to do for me then I write, research and write some more until it does what I want it to do.” Bear in mind there’s some brute force programming where you try every possibility until it (or you) break.

Ignoring the object-oriented stuff for now, think procedurally. The easiest way to begin a script is to think of what you do to accomplish a task (actually writing it down helps because it allows you to see the flow and adjust on the fly.) Then look at the program you want to accomplish the task with. Keep its dictionary open along side the Script Editor. Show the Event Log History (Window>Event Log History and Log Events and Results in the dropdown). Try recording a script in the Finder (I usually don’t suggest this since not many programs ARE recordable but you may find it useful.)

And lastly, if there was one key word in Applescript I say it would be: properties. Get the properties of an item (and nicely, most program support the reserved word “properties” you just have to figure out what item to ask about.) Here’s a little ditty:


tell application "Finder"
	properties of window 1
end tell
--Returns: {class:Finder window, id:3, name:"Applescript", position:{445, 166}, bounds:{445, 166, 1306, 888}, index:1, zoomed:false, closeable:true, titled:true, floating:false, modal:false, resizable:true, zoomable:true, visible:true, collapsed:true, target:folder "Applescript" of folder "Programming" of folder "YYYYYYY" of folder "ZZZZZZZ" of folder "XXXXXXXX" of folder "Users" of startup disk of application "Finder", current view:list view, icon view options:icon view options of Finder window id 3 of application "Finder", list view options:list view options of Finder window id 3 of application "Finder", column view options:column view options of Finder window id 3 of application "Finder", toolbar visible:true, statusbar visible:true, sidebar width:139}

Looking at those properties you can see all kinds of neat stuff you can do with just a window :smiley: Sidebar Width:139 ?? You check the dictionary and find out the sidebar width property of the item “Window” is not read-only (R/O) which means you can mess with it! (On an aside: Dictionaries are not infallible. There are errors occasionally as well as omissions.) So you play this next tune with the window showing:


tell application "Finder"
	set sidebar width of window 1 to 42
end tell

Nifty, eh? And now to bring in some pseudoAnimation!


tell application "Finder"
	set originalWidth to sidebar width of window 1
	repeat with a from 1 to 108 -- Or increment it by something like: repeat with a from 1 to 100 by 5
		set sidebar width of window 1 to (originalWidth + a)
	end repeat
end tell

Well enough dumb window tricks. Hopefully this will encourage you a bit. Writing scripts is not that hard. (The hardest part is knowing the syntax of many different programs but that’s what dictionaries, research, and MacScripter are for :P)

Have fun with it!
Jim Neumann
BLUEFROG