Help tracking file open, close and write time/date to text file.

Hello: I am new to this forum and also to writing AppleScript. I need an AppleScript that will assist me with project tracking.

  1. The script would activate at login.

  2. When a file is opened, the script would capture the time opened then write the date and time to a text file.

  3. When the file is closed, the script would capture the time closed then write the date and time to the same text file.

I need this to work regardless of the files native application.

Has anyone seen a script or OSX utility to accomplish this task? Or does anyone have suggestions on how I might accomplish this with AppleScript? Or would this task be best accomplished with UNIX and Terminal?

Any help, suggestions, or direction would be very much appreciated. Thank you.

There’s no way to attach a script to a document, but you could attach a folder action to a folder to record when a file is changed. You could write a script that logs a time and opens a document when one is dragged on top of it. Then you could have a folder action attached to the container of that document that logs the times when the file is modified.

Maybe someone else will have a better way, but this sounds kind of kludgy to me.

Check out http://versiontracker.com/dyn/moreinfo/macosx/24470

Thank you for the help. It sounds so simple. But apparently it is more complicated than I had thought. Thank you for replying.

Hi,

A script can respond to the opening of a file, but you need to prepare the file first. Use a script app with an open handler an save it as stay open. Something like this:

on open p
set f to item 1 of p
display dialog (f as string)
tell application “TextEdit” to open f
end open

Create a new plain text file with some text editor. Set it to open with to the script app you created above. Now when you open the text document, the Finder looks at what app to open with and sends a list of references ‘p’ of files it wants opened. Your stay open script can now do many things with the files’ references. The hard part about this is you can’t do the same when the document is closed. I’ll show an example of how to monitor the document.

Later,

To keep track of open documents, you might use an idle handler. Something like this:

global open_files

on run
set open_files to {}

end run

on idle
repeat with this_file in open_files
set file_info to (info for this_file)
set is_busy to busy status of file_info
– remove this file reference from the list if not busy or make a new list
if is_busy then
– do somehting
end if
end repeat
return 2
end idle

on open p
set r to item 1 of p
tell application “TextEdit” to open r
end open

I didn’t test this one, but from memory it should work.There was something about ‘busy status’ that might add a snag, but the main theory is monitoring the document.

You say that you want to monitor a project. First thing you need to do to set up your files so they all open with the stay open script. Also, you need to store the default application to open with. You can get the default app with the info for command, so you might run another script on all files within the project’s folder.

Tell if this is feasable up to this point, then I’ll show you how to use the default app property if necessary.

Later,

Oops. I almost forgot to store new document references:

global open_files

on run
set open_files to {}

end run

on idle
repeat with this_file in open_files
set file_info to (info for this_file)
set is_busy to busy status of file_info
– remove this file reference from the list if not busy or make a new list
if is_busy then
– do somehting
end if
end repeat
return 2
end idle

on open p
set r to item 1 of p
set end of open_files to r
tell application “TextEdit” to open r
end open

Later,

Hi,

This might help you out on the theory. You create a script runner something like this:

on open p
set r to item 1 of p
run script r
end open

You can then set any app to open a doc. If you set the above doc to open a file then the Finder sends an open event to your app. It sends parameters a list ot referencess to docs that need to be opened.
Do you get this concept?

Here you can make a double clickable compiled script

excuse me for getting excited but there’s a lot you can do with a double clickable compiled script.


note that a do uble clickable compiled script is not an app

anyway if you want to know what double clickable compiled scripts are good for then just asks.

Have a good day!

Kel: Thank you. The info is great. I’ll give it a go and see what happens. Thanks again.