3

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.

  • You could write to a log file - macscripter.net/viewtopic.php?id=24745 – Tetsujin Mar 31 '18 at 9:00
  • @Tetsujin That is an excellent idea. I was hoping for some kind of pop up but I will start adding that in right away. – Ubermaan Mar 31 '18 at 9:16
  • 1
    All the answers below are actually great ideas. I am going to use a little bit of each of them but marked the one with the system pop up as the answer because it's the closest to what I was hoping was possible. Thanks for all the help. – Ubermaan Apr 2 '18 at 20:38
3

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.

  • 1
    I have to admit, I like this solution and I’m annoyed I didn’t think of it. +1 – CJK Apr 1 '18 at 2:10
  • I actually did consider this as a solution. However I chose my solution because it covers all bases... Displaying the last time run... whether it was launched as stand alone application or from within script editor (even after recompiling). Also, with your solution, the script itself needs to run just to get the value of the last time run. What if someone else ran the script on a shared computer, and the intention is to find out when the script was actually run the last time? With your approach, one needs to open the script file in script editor to try and retrieve that value (if it can be) – wch1zpink Apr 1 '18 at 3:48
  • @user3439894 If you make the "display notification" line of your script as the third line rather than the second line, it fixes the problem of "as the first time it displays msng for missing value" – wch1zpink Apr 1 '18 at 4:26
  • @wch1zpink, You said. "However I chose my solution because it covers all bases... Displaying the last time run... whether it was launched as stand alone application or from within script editor (even after recompiling).", and this is exactly why I stated It just depends on your real needs. (emphasis added) as we really do not know what the OP's true or real needs are in this particular case. All the methods mentioned thus far have their practical applications and uses and under any particular given circumstance may or may not be an acceptable solution. It just depends on one's real needs. – user3439894 Apr 1 '18 at 10:28
  • @wch1zpink, Before I posted, and just writing in Script Editor, I did have it the way I've edited now. I can remember why I decided to switch the last two lines of code when first posted, but it's a moot point now as it's been edited. – user3439894 Apr 1 '18 at 10:28
3

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

3

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
  • That is perfect! One question: I got the command to run in terminal by first cd'ing to the directory but I can't run with a direct path because one folder has a space in it. I tried putting a \ before the space in the path but that doesn't seem to work. What is the POSIX way to handle a space? – Ubermaan Mar 31 '18 at 21:55
  • That’s what quoted form of handles: it surrounds the path in quotes, but you have to use the full path (i.e. /Users/%you%/path/to/file and not ~/path/to/file). But if you’ve retrieved the POSIX path of property, that ought to be fine. – CJK Mar 31 '18 at 22:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .