将日志记录计数添加到脚本的次数 运行

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

我真的卡住了。我想记录一个脚本 运行 的次数。我的脚本如下

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

我想要一个脚本-log.txt 可以计算任何时间和执行的操作。像这样:

action1 = 14
action2 = 21

这可能吗?感谢您的宝贵时间!

这可以通过几种不同的方式实现,具体取决于您的意图。最简单的方法是像这样设置 properties

property action1 : 0
property action2 : 0

然后每次执行操作 1 或操作 2 时递增 属性:

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

关于 AppleScript 属性的有趣之处在于,它们会在脚本的每个 运行 中保留,以便您可以随着时间的推移建立一个累积总数。注意事项是:

  • 如果您编辑并重新编译脚本,属性将重置为其初始值(在本例中为 0)
  • 属性 值只能在脚本上下文中访问,方法是在脚本中使用 Display Dialog 以在警报中显示它们,或者通过从另一个脚本加载脚本并间接读取它们.

如果您更喜欢将数据写入文件,可以使用如下处理程序:

...以下根据评论中的请求进行了大量编辑...

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