Folder Action script to log added items in 1 folder

I like Apple’s “add - new item alert.scpt”. But there are a few things I don’t like about it. For instance it seems to wholly ignore any file added to a folder from another Mac, whether by fileshare, SSH or FTP. So what I’d like to do is “ramp it up” a bit. First, though, I need a few questions answered.

One: Is it possible to write a script that watches for new items added to a folder, regardless of where they originate or how they’re added?
Two: Is it possible to create a list of files added to a folder in a given period of time (eg: six hours) via an AppleScript written primarily as a Folder Action script?
Three: Could such a list be written into a log file (in text format) with date and time, or date and some notation such as “0000-0600-hours”, the log file saved to the same location on the Mac in each instance, and with permissions set for the main user of that Mac to read and, if they so chose, delete each log file?

Now here’s the kick-in-the-pants. I have Panther 10.3.9 installed on my Mac: the Mac this script is being written for is still running Jaguar 10.2.8. So whatever script was cobbled together would likely have to work in both OS versions, “just like the Apple one that inspired it.”

Silversleeves

Model: PowerMac G4 QS 1.25 Dual MDD
AppleScript: 1.9.3
Browser: Firefox 1.5.0.8
Operating System: Mac OS X (10.3.9)

The “kick-in-the-pants” defeats me for one. I know how to do (and the bbs has considered before) the things you list in your post but I don’t have a 10.2.x system to check any of it on (setting up an old G3 with 10.2 and 10.3 has not happened yet). Believe it or not, AppleScript and OS X have changed substantially since then, given that we’re now at 10.4.8. :frowning:

I should correct myself on a few points from that alpha post in this thread.

I didn’t have Folder Actions enabled on the 10.2 Mac – that’s why it seemed “ignorant” of changes I’d assigned the “add” FA script to watch.

Still, and maybe the reason for this is what Adam mentioned in his reply, a modification of the Apple “add notify” script (adding a “beep 2” command before the dialog box comes up) works fine on my Panther G4, but does nothing at all - won’t even beep on the 10.2 Mac – when items are added to a “watched” folder. The original Apple-authored folder action script works as it should: the mod doesn’t. To confirm that “beep” wasn’t taken out of AppleScript in Jaguar, I opened ScriptEditor and wrote a junk script. Yep, it was still a valid command and not a “Panther add-back” as I’ve come to call the bells & whistles from legacy/Classic Mac that have found their way into the later OS X versions. Why it wouldn’t be working in the modified FA script, and why it would stall out or cause the remainder of the script to not work, is a mystery to me as I write this.

Which underscores the ultimate challenge of this whole project. In situations like this in legacy Mac days, I would at this point start thinking “scripting additions!” But again, how many of those with chops in this area were there in the days of the Spotted OS?

Silversleeves

The Finder has changed too; although still a bit lazy about updating folders to which files have been added remotely (by file exchange or FTP), it was really bad in earlier OS X es. My approach in those days (and still) was/is to run an ‘on idle’ stay-open script that checked/checks the contents of the folder periodically - that forces the Finder to “pay attention” and do whatever as required given the result. Even something as simple as getting the size of the folder periodically and acting when it changes.

Finding out how a file got there is beyond the Finder’s capabilities - you’d have to do that with an FTP log for example. It is certainly possible to find out what’s been added in 6 hours, though I’d use a idle script to do it. If you don’t want the idle script to show or be quittable on the desktop, you can use James Sentman’s Drop Script Backgrounder, but then you’ll have to quit it from the Activity Monitor or another script. Writing to an external file is easy, and the external file can be edited or deleted by any user with access to it. It can also be read back to the script and a dialog (for example) be constructed from its content.

I do intend to take this project further, so I’ll take up your suggestion on the Backgrounder utility, Adam, and move on from there.

It’s the un-beeped beep (and consequently the script around it rendered un-run-worthy) that puzzles me.

I can only guess that I introduced the “beep 2” command in a place in the original Apple script that was OK with Panther or Tiger, but was no-great-shakes for Jaguar’s AppleScript “engine” to ignore, so it did that and with it, the rest of the script’s commands as well. I’d like to get at least this much to work, so I’ll paste the modified code here (minus the AS guys’ comments at the top) and ask: “Is there a better place for the beep command to go?”

As it happens the Jaguar box (iMac G3/400 Indigo {though surprisingly it has an AirPort card slot – weird!}) is in the next room from the one my G4 is in, and earlier today I was able to, having attached the original “add new alert” script to a folder I often fileshare stuff into on that Mac, get a dialog to pop up after having added something to it. My idea for the beep was specifically so I would have a ‘signal’ that the script was working, rather than having to go from one room to another to see if a dialog box had appeared.

property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.

on adding folder items to this_folder after receiving added_items
	try
		tell application "Finder"
			--get the name of the folder
			set the folder_name to the name of this_folder
		end tell
		
		-- find out how many new items have been placed in the folder
		set the item_count to the number of items in the added_items
		--create the alert string
		set alert_message to ("Folder Actions Alert:" & return & return) as Unicode text
		if the item_count is greater than 1 then
			set alert_message to alert_message & (the item_count as text) & " new items have "
		else
			set alert_message to alert_message & "One new item has "
		end if
		set alert_message to alert_message & "been placed in folder " & «data utxt201C» & the folder_name & «data utxt201D» & "."
		set the alert_message to (the alert_message & return & return & "Would you like to view the added items?")
		beep 2
		display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout
		set the user_choice to the button returned of the result
		
		if user_choice is "Yes" then
			tell application "Finder"
				--go to the desktop 
				activate
				--open the folder
				open this_folder
				--select the items
				reveal the added_items
			end tell
		end if
	end try
end adding folder items to

Silversleeves