How to detect if a file has been saved inside an specific folder?

Hi everybody:
I’m a amateur in applescript programming( as an english writing too so excuse me).
I need to know if it´s possible with applescript or somenthing to detect if some file inside an specific folder has been saved at the moment it’s saved.
I know it is possible to detect,as an event, when some file(s) is dropped inside a folder and I would need something like that for developping some dificult task.

Thanks for advance anyway.

Bye.

An unsaved file is said to be “dirty”, and the red button on it’s window will have a black dot in it in most applications. After looking around a bit, I’m not sure how that information is stored, but since the window manager knows, there must be a way to find out.

In the meantime, why not just save the file? What are you really trying to find out? Typically, if not saved, a file has a window open, either visible or hidden and you can detect that. If that window has been closed without saving, then whatever changes it held are lost anyway.

[Edit] - It just occurred to me that you might mean “detect that a file has been moved into a specific folder by some means other than dropping it” if so, that’s easy.

My problem is how launching an applescript or whatever script when some system user modified any file located in an specific folder cause it wolud be much better than launching everytime and testing if any user has modified some of these files.
May be it is not possible with applescript but I have seen some event asociated with a folder but I haven’t been able to find a list of system folder or file events that can be captured from applescript.
I guest it is not an easy question.

Thank for your fast response anyway, Adam.

That’s fairly straight-forward. Play with this:

info for (choose file)

You will see that one of the entries is “modification date” so:

modification date of (info for (choose file))

will return a date. Compare that to current date and you know how
long ago the file was last modified and saved (in seconds).

To find the most recent file:

set theFolder to choose folder with prompt "Select anything for this example"
tell application "Finder"
	-- get the latest file (check to see whether the -1 should be a 1 - depends on which way it is sorted. On my machine it's a plain 1, no minus - it's the first file not the last.
	set theFile to (item -1 of (sort (get files of folder theFolder) by modification date))
	-- Convert the finder reference to it to a path
	set thePath to theFile as string
	-- get it's name
	set theName to name of theFile
end tell
display dialog "The complete path: " & return & thePath & return & return & "The Name of the file: " & theName

Something like that could be solve my problem.

But if I have understood I would have to be executing each minute(s) or something to know if a file has been modified and if it is, execute my task.
Is possible for the script to activate automatically when a file has been modified or keep waiting for a file modification and execute a task just when a file has been modified?

Thank you very much for the last solution.

Bye.

Since the file can be modified while it is in the folder (rather than added to the folder), an idle handler is a better way to go than a Folder Action. An idle handler is a script with the contents below saved as a stay open application:

on run
	-- do stuff - this runs when the application is started to set things up. 
	-- Here you could create a list of all the files in the folder and their modifation dates.
end run

on idle
	-- In this section, you periodically check names and dates and compare them in a repeat loop. If one has changed you do your thing.
	return 60 -- do this every 60 seconds
end idle

on quit
	-- do stuff - assumes something in the idle handler would cause the application to quit from within.
	continue quit
end quit

Hi Adam,
I’ve been testing but I cannot see when “on idle” event is launching or which event produce its activation or response.
Thanks.

I wasn’t clear then sicos. You have to write code for the run handler that gets the folder contents and modification dates for the contents when the stay-open application is started which forms the basis for comparisons, then

In the idle handler, you set a time to repeat, and above that insert code to see whether any modification times have changed by getting them again, and compariing them to the original. If any have, you do whatever you had in mind to them, update your list of names and times (for the next comparison), and wait for the next time around.

You don’t need a quit handler unless you want to do something before you quit or use the quit instruction in the other two handlers.

I’m afraid I don’t have time to write you an illustration today, so perhaps someone else will jump in.