Folder Action for Auto-Moving Files Matching Certain Attributes

I’m hoping someone who’s a bit more script-savvy than I (read: most anyone) can assist me with an idea I’d like to implement on my own Desktop using Folder Actions.

On a daily basis, I usually download an export of my database from 37Signals’ Backpack service. It always takes the form of:

backpack-export-YYYY-MM-DD.xml

I would love for Folder Actions to “watch” my Desktop and, upon seeing this hit it, file it away in the proper backup folder on my system (in my case, “Macintosh HD:Users:mike:Documents:Backups:Backpack”).

I have a similar auto-move I’d love to enact using download originals from Flickr. Their shared characteristic would be that they end in “_o” (extension being JPG, of course). The only complication with that is that I would hope that a new folder could be opened on the Desktop if one didn’t already exist.

I know that this sounds very much like a “build my script for me” post, and, unfortunately, I suppose that really is what it is. If someone would prefer not to create it out of goodwill, which I suppose I could understand, could I offer perhaps a $5 donation to EPIC or EFF in your name?

Oh, I should clarify: I am on 10.3.9 – I found my iBook G3, even maxed out on 640 MB RAM, grew more and more unresponsive with 10.4.x, even with Dashboard disabled.

Mike

Model: iBook
AppleScript: AppleScript 1.9.3
Browser: Safari 312.3.1
Operating System: Mac OS X (10.3.9)

hi mike,
this folder action should work for your first request, although i havent tested it on 10.3.9,
its probably a bit messy as there was a lot of copy, paste going on.
just attach the folder action to your desktop folder.


set theDate to current date
set int to integer
--  get the day number with leading zero
set dd to text -2 thru -1 of ("0" & theDate's day)
--  note that it is faster to just add a zero than to check whether one is required
copy theDate to b
set month of b to January
set mm to text -2 thru -1 of ("0" & (1 + ((theDate - b + 1314864) div 2629728)))
--  get the year...
set yyyy to year of theDate
on adding folder items to pdfFolder after receiving added_items
	repeat with i in added_items
		if folder of (info for i) is false then
			set fileBusy to true
			repeat while fileBusy is true
				set fileBusy to busy status of (info for i)
				delay 1
			end repeat
			try
				set theDate to current date
				set int to integer
				--  get the day number with leading zero
				set dd to text -2 thru -1 of ("0" & theDate's day)
				--  note that it is faster to just add a zero than to check whether one is required
				--  ...or, if you prefer, a month number with leading zero
				copy theDate to b
				set month of b to January
				set mm to text -2 thru -1 of ("0" & (1 + ((theDate - b + 1314864) div 2629728)))
				--  get the year...
				set yyyy to year of theDate
				tell application "Finder"
					set mydate1 to {yyyy, "-", mm, "-", dd} as text
					set sourcefile to i
				end tell
				tell application "Finder"
					set dest to folder "Macintosh HD:Users:mike:Documents:Backups:Backpack"
				end tell
				tell application "Finder"
					if name of sourcefile contains {"backpack"} then
						if name of sourcefile contains {mydate1, ".xml"} then
							set dupe to (move sourcefile to dest)
							end if
						end if
					end tell
			end try
			end if
		end repeat
end adding folder items to

ps. feel free to let us know the result.
peace.
truth.

I appreciate what you’ve done — but it doesn’t appear to be working. Any thoughts? Anything I can do to assist the debugging process?

Model: iBook
AppleScript: AppleScript 1.9.3
Browser: Safari 312.3.1
Operating System: Mac OS X (10.3.9)

sorry about that mike, think it was probably a bit over complicated with the date bit.
try this




on adding folder items to theFolder after receiving added_items
	repeat with i in added_items
		if folder of (info for i) is false then
			set fileBusy to true
			repeat while fileBusy is true
				set fileBusy to busy status of (info for i)
				delay 1
			end repeat
			
			tell application "Finder"
				set sourcefile to i
				
				set dest to folder "Macintosh HD:Users:mike:Documents:Backups:Backpack"
			end tell
			tell application "Finder"
				if name of sourcefile ends with ".xml" then
					if name of sourcefile begins with "backpack-export" then
						set dupe to (move sourcefile to dest)
					end if
				end if
			end tell
			
		end if
	end repeat
end adding folder items to

might be worth checking i have the path right to the backup folder
peace,
truth

Well, the introductory part is “Maggie:”, not “Macintosh HD:”, but other than that, you have the path right.

Still, it’s not working, unfortunately. I’m not sure why.

The file arrives via download from Safari, plops itself on the Desktop, and then stays there. No auto-file into the correct folder.

(Heading to work now, so won’t be able to reply further until I get home.)