I’m writing a script that needs to detect when the contents of a file has changed. As such I’m detecting the file modification date of the file. What I have found is that if I run a script from the script menu, then the file modification date of that script is changed to the current time. It happens every time you run a script. This is causing me problems because the although the file modification date has changed, the actual contents of the script has not changed. Thus I can’t accurately detect when the contents of my script files have changed.
For example, suppose I’m writing a script to backup my files from my internal hard drive to an external drive. Logic tells me that I can compare the file modification date of the backedup file on the external drive, and match it to the file on the internal drive, then if the file modification dates are different then I know to rebackup that internal file again.
But this doesn’t work for scripts in my script menu. Any script that I run has its modification date changed so it gets backuped up every time even though the actual contents haven’t changed.
Here, check the file mod date on a script from your script menu. Then run that script and check the mod date again. Notice how the file modification date changes?
set aFile to (choose file)
set fileModDate to modification date of (info for aFile)
Did you know about this? Why do you think this happens? Can it be stopped? Does anybody have a solution to acurately detect when the contents of a script have changed?
Little things like this really bug me! 
I know for certain that Script Debugger does that – it saves metadata every time you run a script. It’s also true that if your scripts have properties that are reset by running the script, then the mod date will change as well.
It doesn’t seem to happen for “applescript-created” applications run from the script menu… just compiled scripts.
But AppleScript apps are “frozen solid” so you don’t mod them when you run them, .scpt files are not, stuff happens. I’m not into the details, however, so more research is required.
Hi,
Except with some early versions of Tiger, it happens with both scripts saved as applications and with compiled scripts, if they contain properties or globals. (Top level variables count as globals here, unless they’re explicitly declared local.) It makes no difference whether the values of the properties or globals are changed or not. They’re always updated.
With scripts saved as application bundles, the modification dates of their contained script resource files (which are compiled scripts) also change “ for the same reason “ but those of the package folders that front them don’t.
Thanks for the clarification. It makes more sense now why the modification date is updated.
Does anybody know another way other than “modification date” to determine when the actual contents of scripts have changed?
This may be more work than you want, but I have a property called VersionNumber at the top of all of my scripts that I start at “1.00” and update anytime I make a change. I then drop my scripts on the below applescript saved as an app whenever I need to check that a script is up to date on someone’s computer (I am distributing scripts to an art department of about 30 people). It works for me but it does mean adding that property to all your scripts and remembering to update it whenever you make a change.
property VersionNumber : "1.00"
on run
with timeout of 9999 seconds
display dialog "This script works by dragging and dropping a single AppleScript file onto the script icon." buttons "OK" default button "OK" with icon 2 giving up after 9990
end timeout
end run
on open these_items
tell application "System Events"
set NumberOfFiles to the count of these_items
if NumberOfFiles is not 1 then
display dialog "One file at a time please." buttons "Cancel"
end if
set this_item to item 1 of these_items
set the item_info1 to info for this_item
if ((kind of item_info1 is "script") or (kind of item_info1 starts with "Application")) then
else
tell me
activate
display dialog "This is not an AppleScript." buttons "Cancel"
end tell
end if
end tell
try
set VersionCheck to load script this_item
display dialog ("Version: " & (VersionNumber of VersionCheck)) buttons "OK" default button "OK"
on error
display dialog "Information not found????" buttons "Cancel" default button "Cancel"
end try
end open
Model: G5
Browser: Safari 419.3
Operating System: Mac OS X (10.4)