Act on updated file

Is there a way to initiate an applescript from a file that exists, but has just been updated. I’d rather not have to check the file every X minutes, and was wondering if anyone knows of any “system trick” that could trigger an applescript, kind of like folder actions does when a file is dropped into a folder.

Model: G5 Dual 2Ghz + Mac OSX 10.4.3
Browser: Safari) OmniWeb/v563.42
Operating System: Mac OS X (10.4)

You could use a stay open Idle handler app.

property fileToWatch : {}
property modDate : {}
global fileToWatch
on run
	if fileToWatch is {} then
		set fileToWatch to choose file with prompt "Choose file to watch."
	end if
end run

on idle
	tell application "Finder"
		set modTime to modification date of fileToWatch
		if modDate is not {} then
			if modTime is not modDate then
				display dialog "Do actions here."
				set modDate to modTime
			end if
		else
			set modDate to modTime
		end if
	end tell
	return 30
end idle

You might want to add a dialog and if statement just incase you want to ever change the file you’re watching without editing the code.

PreTech

P.S. Once you run this once, the file is stored in the property and will be remembered unless you edit the code and recompile it. So the next time you start the app it will run but not ask you to choose a file. Also save this as a stay open application and double click it to run.

If you come up with something, I’d love to know what it is.

At least as far as I know, OSX has no archive bit like DOS file systems do, so that’s out. You may have no choice but to use a file system that explicitly supports this, like XInet, or keep a database of files and last mod datetimes and scan periodically to create your own log of files to process. (Check out the man on mtree.)

Sorry, I read your post but it didn’t quite register as to what you wanted. :confused:

PreTech

That’s OK I’m not 100% sure what your code does (bit of a newbie!).

Maybe it’s clearer if I outline exactly what I’m trying to do.

I have an IRC program, that write’s URLs to a log file when it sees them in a channel. When this happens I want to have applescript do a few things with the data.

The problem is the file already exists, so I can’t get it to trigger an applescript from a folder action. So I’m trying to run an applescript on “updated file” in a folder rather than “new file” in a folder.

Well then, the script I wrote will allow you to choose the file that you want watched, then the program sits in “idle” and checks that file’s modification date (which includes time) every thirty seconds. If it is different than the last time then it does something. Right now it just displays a dialog that says “Do actions here.” Basically it automates checking the file every so often so you don’t have to do it. If you need it to check more often or less often, then change the return 30 to return X. X is in seconds.

What the script does is set a property (declaring a variable a property allows that variable information to be kept in “permanent” storage unless you recompile the script): fileToWatch and modDate.

If fileToWatch is empty then it asks you to choose the file you want checked. After that the program gets the modification date of the file and sets modDate to that modification date. It waits for thirty seconds and then gets the modification date of fileToWatch. If it has changed then it displays the dialog which is where you would put the actions you want to perform such as: opening the file, starting a program, just displaying the dialog that lets you know it has been modified, or any of a number of things.

PreTech