Watch Folder Then Open List Dialogue

Hi, I’m only a basic scripter, but here’s what I’m trying to do…

I want a script that will watch a folder (trying to stay away from folder actions if possible) which is on a remote server. When something goes in there, i need it to alert me with a list dialogue, let me choose a file and then have it open in Photoshop.

How hard would something like that be?

Thanks in advance,

  • Langley

My opinion of the difficulty is based on my rudimentary understanding of launchd, which is a part of the Unix core and is designed to launch scripts whenever a specified folder changes. A big question is whether or not your machine is connected to the remote server at all times. If the connection is stable and operating whenever your machine is running, that is at least a start, as your launchd agent can sit there and wait for changes. The problems begin when you only log into the server occasionally, then, every time you log on, it is considered a change, and therefore the launchd agent is called at every login.

The other potential issue involves the concept of user feedback. Under most circumstances, anything launched with launchd has to operate without any feedback at all from a user, including a simple display dialog command. I am sure there are ways to overcome this, although I have not experimented very much with that sort of thing.

For a plain vanilla solution, save this as a stay-open application. I don’t have any way to test it with a remote folder. Let me know if it works.


-- assuming the "watched" folder is mounted

property tWatchFiles : missing value -- properties "keep" from run to run
property tProjects : missing value

on run -- this runs when the application is started
	set WatchedFolder to (choose folder) as alias
	checkFiles(WatchedFolder)
end run

on idle -- idle scripts are saved as stay-open AppleScript applications and cycle periodically.
	if exists WatchedFolder then
		checkFiles(WatchedFolder)
	else
		beep 3 -- or display dialog, or whatever - it's not mounted
	end if
	return 30 -- these are seconds before the next 'look'
end idle

to checkFiles(WatchedFolder)
	set newFiles to {}
	try
		tell application "Finder" to set tFiles to files of entire contents of WatchedFolder as alias list
	on error -- only one, so as alias list doesn't work (a bug)
		tell application "Finder" to set tFiles to files of entire contents of WatchedFolder as alias as list
	end try
	repeat with F in tFiles
		if contents of F is not in tWatchFiles then set end of newFiles to contents of F
	end repeat
	try
		-- do stuff with the new files
	on error e
		display dialog e
	end try
	set tWatchFiles to tFiles -- update the file list
end checkFiles

Langley: Here’s my version (quickly hacked together)


property serverName : "Graphics Works"
property watchedFolder : "¢¢ Resources"
property delayTime : 5 -- How long to wait befor checking the folder
global defaultFileCount

tell application "Finder"
	-- Do Inits... Checking for server and folders...
	if serverName is in (list disks) then -- Check for the mounted volume
		if (watchedFolder as Unicode text) is in (name of every folder of disk serverName) then -- Check for the folder to watch
			set watchedFolder to (folder watchedFolder of disk serverName)
			set defaultFileCount to (count files of watchedFolder) -- Count the number of files present when the script first runs.
		else
			display dialog "Watched folder  \"" & watchedFolder & "\" not found."
		end if
	else
		display dialog "Server volume \"" & serverName & "\" not found."
	end if
end tell

on idle {}
	tell application "Finder"
		set fileCount to (count files of watchedFolder) -- Count the files again to see if it's changed
		if fileCount ≠ defaultFileCount and fileCount ≠ 0 then -- If there's a mismatch AND there ARE files in the folder...
			set chosenFile to (item 1 of (choose from list ((name of every file of watchedFolder) as list) without multiple selections allowed and empty selection allowed)) -- Pick your file.
			if chosenFile ≠ {} then
				try -- This is optional to force users not to open files directly from the server.
					set dupedFile to duplicate (first file of watchedFolder whose name is chosenFile) to desktop
				on error
					if button returned of (display dialog "File exists on Desktop." buttons {"Replace", "Cancel"} default button "Cancel") = "Replace" then
						set dupedFile to duplicate (first file of watchedFolder whose name is chosenFile) to desktop with replacing
						open dupedFile -- Or do whatever to it.
					end if
				end try
			end if
			set defaultFileCount to fileCount -- Cache the new count
		end if
	end tell
	return delayTime
end idle

As Adam said, save it as a stay-open application. You can change the properties to match your server/folders.

Have fun,
Jim Neumann
BLUEFROG