script receives message when changes in file?

Is it possible to create stay open AppleScript that is idle all the time and when some file is modified or deleted, this AppleScript receives message from OS and then AppleScript does something?

Something like this may work:


global FileTime
global MainFile
set MainFile to "path:to:file"

on idle
tell application "Finder"
	set fileTime to modification date of file MainFile
	repeat
		set curTime to modification date of file MainFile
		if curTime > FileTime then
			--do something with file
			set FileTime to modification date of file MainFile
		end if
		delay 30 --amount of time to wait in seconds
	end repeat
end tell

Model: iMac Core Duo
AppleScript: 1.10.4
Browser: Safari 417.9.2
Operating System: Mac OS X (10.4)

Thanks for answer, but i want to monitor large amount of files or maybe even all files in harddrive and receive message from OS or Spotlight when file is modified/deleted. Spotlight can propably notify AppleScript or is it callt AppleEvent what i need? Thanks

There are unix commands to do that (that I can’t recall off hand), but they are incredibly verbose because system files, caches, etc. are changing all the time. You’ll have to confine your quest to something manageable.

Does anybody know what these commands are? Maybe caches etc can be excluded?

Sorry, it’s a Perl script called watchit written by Cameron Hayne, found here.