Hi to all,
Does anyone know how I could have an applescript keep a record of the time spent on a file. Meaning that each time I open the document, the time starts AGAIN (is added to the time already spent on the file and that when I close it, the time is added?
If this is too complicated, is there a way to simply have a file that will have a list of the time spent on the file, I’ll add the different times spent by hand. And if that’s too hard… is there a way to have a file that has the time at which I opened the file and a time at which I closed the file.
Thanks in advance.
Unless the application is attachable (which very few are), your basic approach is going to be something along the lines of an idle/stay open script that periodically checks which document(s) are open in your target application and logs accordingly.
The primary issue relates to one of granularity - if you check every n seconds/minutes you could miss any interval where the document is open for n-1 seconds (e.g. if you check every 5 minutes, it’s possible you could open the file, work on it and close it between checks so the script never sees it, and therefore doesn’t log the time.
The more frequently you check the lower the chance of a miss, but the more CPU time your script will suck. You need to decide what’s appropriate.
As for the script itself, the basic premise is that you check the time and current document when you start. At each interval you see if the document has changed. If not, just carry on, otherwise log the time delta.
Something like this should get you going:
global startTime, docName
set startTime to (current date)
tell application "YourApp"
set docName to name of document 1 -- adjust as appropriate for the app
tell application "YourApp"
if (docName is not "") and (name of document 1 is not docName) then
-- current document has changed, so log the time
set timeDelta to (current date) - startTime -- time in seconds
-- now open the log file - adjust the file name as appropriate
set logFile to open for access file "path:to:log.file" with write permission
-- read the previous value - will be 0 if this is a new file:
set currentTime to read file logFile
-- reset the file contents:
set eof logFile to 0
-- and write the new value:
write (currentTime + timeDelta) to logFile
-- close the log file when we're done
-- stop tracking
set docName to ""
return 60 -- come back and check again in one minute
You’ll probably want to make changes to deal with multiple documents/applications. The most obvious of which is the file names used to track the changes - presumably this will be based on the document name from the application.
You’ll also need to examine the target application’s dictionary to determine how to reference the active document. ‘document 1’ might work, but there may be other alternatives depending on the app in question.
You’ll also need to decide what to do when the document does change. Do you want to start tracking the new document? What about if the document is open, just not frontmost? The possibilities are endless - you just need to decide how to deal with them.
I’m a novice at Applescript, not to programming though.
In the command line
set docName to name of document 1 – adjust as appropriate for the app
Lets assume I’m tracking a TextEdit document. When I open the Dictionary, via applescript, of TextEdit, how can I know that Document 1 applies or not?
I set the log file in the root > “/TimeLog.txt”
TextEdit opens but the Log file isn’t incremented in anyway.
When you open the dictionary, expand the ‘Standard Suite’ and you should see the ‘document’ class. When you click on this you’ll see the properties of a document as far as TextEdit is concerned:
Here you can see that ‘name’ is a valid property for a document.
‘Open for Access’ calls for Mac-style (colon-delimited) path names, not unix-style (/-delimited). Try changing your code to use “Macintosh HD:TimeLog.txt” (adjusted for your hard drive name) instead of "/TimeLog.txt.