Storing data between executions, Growl and Folder Actions

I have a folder action that notifies me through Growl when someone places a file in a certain network folder. It works great, except that if I don’t retrieve the files right away, I get repeat notifications whenever new files are added. For instance, I will get a notification for file1, then file1 & file2, then file1, file2 & file3 all piling up the side of my screen. So I thought “A-Ha! I’ll define the files that have already sent notifications in a global property list! Then it will remember between executions what files have produced notifications.” And that’s great. I get one notification per file… unless it happens to have the same name as a file that’s already had a notification. Then I get nothing. And unfortunately, there are a lot of duplicate file names, which is something I can do nothing to change.

So really I’m asking for ideas from the pros. How would you go about making sure each file gets exactly 1 notification? Here is my code:

property whiteList : {}
on adding folder items to this_folder after receiving added_items
    set icon_file to "2074:Users:admin:Desktop:location-ftp-icon.png" as alias
    set FilePath to "FTP:Incoming:OsseoPressNews:NEWS:Finished News Pages" as alias
    set curTime to time string of (current date)
    
    tell application "Finder" to set fileList to name of every file of folder FilePath
    tell application "GrowlHelperApp"
        set the allNotificationsList to {"Pages_Done"}
        
        set the enabledNotificationsList to {"Pages_Done"}
        
        register as application ¬
            "News Pages Finished" all notifications allNotificationsList ¬
            default notifications enabledNotificationsList ¬
            icon of application icon_file
    end tell
    
    repeat with namedFile in fileList
        if namedFile is not in whiteList then
            tell application "GrowlHelperApp"
                --    Send a Notification...
                notify with name ¬
                    "Pages_Done" title ¬
                    curTime description ¬
                    namedFile & " has been placed on the FTP site" application name ¬
                    "News Pages Finished" image from location icon_file
            end tell
            set namedFile to namedFile as text
            set end of whiteList to namedFile
        end if
    end repeat
end adding folder items to

Model: PM G5 Dual 2.0
AppleScript: 1.10.7
Browser: Firefox 3.6.9
Operating System: Mac OS X (10.4)

There’s nothing magical about it. The folder action itself gives you the folder that the files were added to and a list of only the files that were added. See this line here…

 on adding folder items to this_folder after receiving added_items

this_folder = the folder so you don’t need this line of code…

set FilePath to "FTP:Incoming:OsseoPressNews:NEWS:Finished News Pages" as alias

added_items = a list of only the added items, so this is what you would want growl to display instead of this…

tell application "Finder" to set fileList to name of every file of folder FilePath

Of course you’ll have to repeat though that list and collect only the file names because they’re paths, but that’s easy enough.

Hi,

I already wondered that you retrieve always a list of every file in the folder.
To get notified only for the new items just process added_items instead


on adding folder items to this_folder after receiving added_items
	set icon_file to "2074:Users:admin:Desktop:location-ftp-icon.png" as alias
	set curTime to time string of (current date)
	
	tell application "GrowlHelperApp"
		set the allNotificationsList to {"Pages_Done"}
		set the enabledNotificationsList to {"Pages_Done"}
		
		register as application ¬
			"News Pages Finished" all notifications allNotificationsList ¬
			default notifications enabledNotificationsList ¬
			icon of application icon_file
	end tell
	
	repeat with oneFile in added_items
		set fileName to name of (info for oneFile)
		tell application "GrowlHelperApp"
			--    Send a Notification...
			notify with name ¬
				"Pages_Done" title ¬
				curTime description ¬
				fileName & " has been placed on the FTP site" application name ¬
				"News Pages Finished" image from location icon_file
		end tell
	end repeat
end adding folder items to

That makes sense. I wrote this script quite a while ago. I’m not sure why I did it that way. I had it in my head that I only wanted to use the added_items, so you (both) are right, using a list of all files doesn’t make sense. I wonder if I was having trouble extracting the name. I don’t think I would have thought of the (info for oneFile) part.

Thanks Regulus and Stefan