Can AppleScript display when a script was last run?

Is there a way for an AppleScript command to display the last time it was run? I have a few scripts where it would be useful to have a timestamp of the last time it was run for logging purposes. I am very green to scripting, any ideas are welcome.


Aside from saving info to a separate disk file, or shelling out to use a command line utility, it can also be saved as a value of a property within the script, preferably saved as an application. IMO Saving the value of a property within the script is the easiest from a coding aspect, however because the value is reset if the code is later modified or recompiled, this may present a drawback from using this method. It just depends on your real needs. That said, and to give you another option to choose from:

Example AppleScript code:

property lastRunTime : missing value
set lastRunTime to (current date) as string
display notification lastRunTime subtitle "This application was last run:"

I saved the three lines of code as an application named Last Run Time and upon running it, it showed the notification as:

enter image description here

You can also use the display dialog or display alert command if you don't want to use the display notification command.


Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways to accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.


Make sure if you change the value of set theFile to "/tmp/script_run_log.txt" -- value can be changed , then you must change the value of set theLogFile to "Macintosh HD:private:tmp:script_run_log.txt" in the second script also

-- PLACE THIS FOLLOWING CODE ANYWHERE IN A SCRIPT FILE
-- FOR NEATNESS, CONSIDER PLACING AT SCRIPT BOTTOM
-- THIS CODE WRITES TIME AND DATE OF LAST RUN OF THE SCRIPT OR APP
-- TO A SEPARATE FILE

writeToFile()
on writeToFile()
    set currentDate to (current date) as string
    set theFile to "/tmp/script_run_log.txt" -- value can be changed
    set myName to name of (info for (path to me))
    set theText to myName & " was last run on " & currentDate
    try
        set writeToFile to open for access theFile with write permission
        write theText & linefeed to writeToFile as text starting at eof
        close access theFile
    on error errMsg number errNum
        close access theFile
        set writeToFile to open for access theFile with write permission
        write theText & linefeed to writeToFile starting at eof
        close access theFile
    end try
end writeToFile

Save the following code as an application,. Any time you would like to check the log file for the last time a specific script was run, just launch this app

set theLogFile to "Macintosh HD:private:tmp:script_run_log.txt"
set lastParagraph to paragraphs of (read alias theLogFile) as list

display alert ¬
    theLogFile message item -2 of lastParagraph ¬
    buttons ¬
    "OK" default button ¬
    "OK" giving up after 15

enter image description here


You can retrieve the time that any file was last accessed using the command-line tool stat with the following arguments:

    stat -f "%Sa" -t '%Y-%m-%d %H:%M:%S' /path/to/file
  • -f "%Sa": This requests only the file access data, as stat can provide other data as well, such as modification times, creation times, device information, ownership and permission data, etc. See man stat if you want to know more about the other capabilities of stat.

  • -t "%Y-%m-%d %H:%M:%S": This tells stat what format to output the date and time in. You can change this to suit your needs, but for the purposes of this answer, I chose a standardised format that output this for one of my AppleScripts: 2018-03-31 16:55:42.

Bear in mind that the access time for an AppleScript file will be updated, not just if it is run, but also if it gets read or copied or moved.

To use this within an AppleScript, you can wrap it within a do shell script:

    do shell script "stat -f '%Sa' -t '%Y-%m-%d %H:%M:%S' " & ¬
        quoted form of POSIX path of theFileAlias