Add logging count to the amout of times a script is run

140 views Asked by At

I'm really stuck. I want to keep a log of how many times a script is run. My script is the following

set actionlist to {"action1","action2}
--select an action
set actionlist to choose from list actionlist with prompt "hello" default items {"action1"} with title "Actions"
if actionlist is {"action1"} then
    set thefile to quoted form of POSIX path of (choose file of type {"mov"})
    tell application "Terminal"

        do script "DO SCRIPT HERE"
        activate
    end tell

if actionlist is {"action2"} then
    set thefile to quoted form of POSIX path of (choose file of type {"mp4"})
    tell application "Terminal"
        do script "DO SCRIPT HERE"
        activate
    end tell

I want a script-log.txt that counts any time and actions were executed. Something like this:

action1 = 14
action2 = 21

Is this possible? Thank you for your time!

1

There are 1 answers

22
Ted Wrigley On BEST ANSWER

This is doable in a few different ways, depending on what your intent is. Simplest would be to set up properties like so:

property action1 : 0
property action2 : 0

And then each time you do action 1 or action 2 increment the property:

set action1 to action1 + 1
--[...]
set action2 to action2 + 1

The interesting thing about AppleScript properties is that they are retained across each run of the script so that you can build a cumulative total over time. The caveats are that:

  • if you edit and recompile the script, the properties are reset to their initial values (0, in this case)
  • The property values can only be accessed within a script context, either by using Display Dialog within the script to show them in an alert, by loading the script from another script and reading them indirectly.

If you prefer to write data to a file, you can use handlers like the following:

...The following is edited significantly per requests in comments...

property tracking_file : POSIX path of (path to home folder from user domain) & "script-log.txt"
property boundary_text : "--- Log ---"
property colon_delim : " : "
property actions_list : {"action1", "action2", "action3"}

tell application "System Events"
    if not (exists file tracking_file) then my initialize()
end tell

set chosen_action to choose from list actions_list with prompt "hello" default items {"action1"} with title "Actions"
if chosen_action is not false then
    writeToFile(chosen_action as text)
end if

on writeToFile(chosen_action)
    set file_pointer to openFilePointer()
    set tally_text to read file_pointer until boundary_text

    set action_label to chosen_action & colon_delim
    set tally_offest to (offset of action_label in tally_text) + (length of action_label)
    set current_action_tally to (text tally_offest through (tally_offest + 3) of tally_text) as integer
    set new_action_tally to zeroPad(current_action_tally + 1)
    write new_action_tally to file_pointer starting at tally_offest for 3
    write action_label & short date string of (current date) & " " & time string of (current date) & return to file_pointer starting at (get eof file_pointer) + 1
    close access file_pointer
end writeToFile

on zeroPad(a_num)
    return text -3 through -1 of ("0000" & a_num as string)
end zeroPad

on initialize()
    set intital_text to ""
    repeat with this_item in actions_list
        set intital_text to intital_text & this_item & colon_delim & "000" & return
    end repeat
    set intital_text to intital_text & return & boundary_text & return
    set file_pointer to openFilePointer()
    write intital_text to file_pointer
    close access file_pointer
end initialize

on openFilePointer()
    try
        set file_pointer to open for access tracking_file with write permission
    on error err_str number err_num
        display alert "Error " & err_num & ": " & err_str
        close access tracking_file
        set file_pointer to open for access tracking_file with write permission
    end try
    return file_pointer
end openFilePointer