FolderAction: poll folder every 15 min and email info on new files

I have tried to configure following folderaction/AS so that it would look in to clients FTP-folder every 15 minutes and email a list of newly added files since last check. But there are still couple of problems bothering:

  • How do I get all the information to one email, in my script I get one email / file added
  • How do I configure FolderAction to poll a folder at specific times (e.g. every 15 min)

on adding folder items to this_folder after receiving added_items
	
	
	set date_ to (current date) as text
	
	repeat with file_ in added_items
		set info_ to info for file_
		set name_ to name of info_
		set sisalto to "File" & name_ & return & "was recieved at " & date_
		
		tell application "Mail" to tell (make new outgoing message with properties {subject:"Email from me", visible:true, content:sisalto})
			make new to recipient with properties {name:"MyName", address:"my@email.com"}
			
		end tell
	end repeat
end adding folder items to

Any suggestions would be appriciated:)

Hi, cuie.

Folder actions are only triggered by the events to which their handlers respond “ eg. adding items to the folder, opening it, etc. If you want to superimpose a timed action, one way is to use another, stay-open script that’s triggered by the operating system every 15 minutes. The folder action script would update a variable in the stay-open script every time a drop was made and the stay-open script would check its variable every 15 minutes.

The folder action script:

on adding folder items to this_folder after receiving added_items
	
	set date_ to (current date) as text
	
	repeat with file_ in added_items
		set info_ to info for file_
		set name_ to name of info_
		-- This assumes that the matching stay-open script has been saved
		-- as a stay open application with the name "Notifier.app".
		tell application "Notifier" to set its sisalto to its sisalto & "File " & name_ & return & "  was received at " & date_ & return
	end repeat
	
end adding folder items to

The stay-open script application:

global sisalto

set sisalto to ""

on idle
	if ((count sisalto) > 0) then
		tell application "Mail"
			tell (make new outgoing message with properties {subject:"Email from me", visible:true, content:sisalto})
				make new to recipient with properties {name:"MyName", address:"my@email.com"}
			end tell
		end tell
		-- After creating the Mail message, "clear" the sisalto variable.
		set sisalto to ""
	end if
	
	-- Wake me up again in 15 minutes.
	return 15 * minutes
end idle

Always a new trick Mr G. :lol:

Didn’t know this was possible with a running stay-open script:

tell application "Notifier" to set its sisalto to its sisalto & "File " & name_ & return & " was received at " & date_ & return

I can use this to reset the repeat time in a script of mine. Neat.

Hi Nigel, and thanks for the scripts!

I saved the Notifier.app to /Applications, should it be open all the time? I can´t get it to stay open, it just starts and quits every time the FolderAction-script call to it. Also, it won´t evoke Mail to send the e-mail.

Am I doing something wrong?

Janne

Hi, Janne.

Save “Notifier.app” (or whatever else you decide to call it) this way: In the dialog that comes up when you click “Save As.” in Script Editor’s “File” menu, there’s a pop-up menu called “File Format:”. When you select “application” or “application bundle” from that, three “Options:” check boxes are enabled just below it. Check the “Stay Open” box and leave the other two blank.

This should also solve the problem of Mail not being invoked, since “Notifier.app” won’t now quit immediately. When the “Notifier” applet is first launched, it runs through its start-up business and then just sits there. Every 15 minutes, its ‘idle’ handler is automatically run again and, if the value of ‘sisalto’ has changed from “”, a new message is created in Mail. If “Notifier” is launched by the folder action, ‘sisalto’ doesn’t get changed until immediately after the start-up process has finished, so the message about the first drop (+ any subsequent ones) won’t appear until 15 minutes later.

As in your original script, there’s nothing in “Notifier.app”'s code that actually sends the message. For that you need to add a ‘send’ line.

global sisalto

set sisalto to ""

on idle
	if ((count sisalto) > 0) then
		tell application "Mail"
			tell (make new outgoing message with properties {subject:"Email from me", visible:true, content:sisalto})
				make new to recipient with properties {name:"MyName", address:"my@email.com"}
				send
			end tell
		end tell
		-- After creating and sending the Mail message, "clear" the sisalto variable.
		set sisalto to ""
	end if
	
	-- Wake me up again in 15 minutes.
	return 15 * minutes
end idle

Trying to ‘send’ a message when there’s nothing else in Mail’s Outbox throws up annoying dialog for me (just one of the many reasons I don’t use Mail), but presumably that doesn’t happen if the machine’s permanently on-line.

Thanks Nigel, it solved out with checking the Stay-open option when saving.

Another problem came up:
When somebody uploads a folder, and after that uploads files to that subfolder my script doesn´t notice that. Apparently because FolderActions can´t look inside subfolders.
Is it possible to accoplish this with AppleScript or do I have figure out some other way to do it?

I´m trying to use this on FTP-server, so that our employees would get a notification if someone uploaded material to any of the customers folder. Sometimes customers upload files to subfolders that they have previously uploaded, and AppleScript/FolderActions can´t see that.

  • Jannne

Hi, Janne.

That’s right. Folder actions don’t work recursively, only with the specific folders to which they’re attached. What you could do is to get the folder action script to attach itself to any folders that appear in the watched folder. The following works in Tiger, but I haven’t tried it on my other machine. The obvious caveat is that once the new folders become watched folders themselves, the action will attach itself to any folders that subsequently appear in them. I don’t actually know if there’s a limit to the number of folders that can have the same action attached to them or what happens if they’re all triggered at once…

on adding folder items to this_folder after receiving added_items
	
	set date_ to (current date) as text
	
	repeat with file_ in added_items
		set info_ to info for file_
		set name_ to name of info_
		
		-- If this added item is a folder, attach this action to that too.
		if (folder of info_) and not (package folder of info_) then
			tell application "System Events"
				attach action to file_ using (path to me)
			end tell
		end if
		
		-- This assumes that the matching stay-open script has been saved
		-- as a stay open application with the name "Notifier.app".
		tell application "Notifier" to set its sisalto to its sisalto & "File " & name_ & return & "  was received at " & date_ & return
	end repeat
	
end adding folder items to

Wow, I didn´t even think that is possible. Great work and BIG thanks Nigel! :slight_smile:

  • Janne

This post is “exactly” what I’m looking for but I don’t know why it is not working so I need the experts help. I have a folder with jpgs. I have set up a folder action that would update a variable in the stay-open script every time a jpg was drop and the stay-open script would check its variable every 30 minutes.

This is the Folder Action

on adding folder items to this_folder after receiving added_items
	
	set date_ to (current date) as text
	repeat with file_ in added_items
		set info_ to info for file_
		set name_ to name of info_
		-- This assumes that the matching stay-open script has been saved
		-- as a stay open application with the name "test stay open.app".
		tell application "test stay open" to set its sisalto to its sisalto & "File " & name_ & return & "  was received at " & date_ & return
	end repeat
end adding folder items to

This Folder action is in the scripts folder in the main library & the stay open app. is in home folder

The stay open is:

global sisalto
set sisalto to ""
on idle
	if ((count sisalto) > 0) then
		tell application "Microsoft Entourage"
			set Email_Notify to make outgoing message with properties {subject:"New images added to IT-Web folder", recipient:"nellyann.rosario@intervalintl.com", content:"HI there, your files are waiting"}
			send Email_Notify
		end tell
		
		-- After creating and sending the Entourage message, "clear" the sisalto variable.
		set sisalto to ""
	end if
	
	-- Wake me up again in 15 minutes.
	return 30 * minutes
end idle

Any ideas why this is not working. Please help:|

Never mind. The Folderaction is working & the stay open script too but its not listing the file names dropped in the folder