I found this code some where on this site and it used to work, but somehow I broke it and can’t see what went wrong. Any help would be great.
Is it possible to embed this code into a script and then keep track of how many times the script was run?
--Counter no longer works? What did I do to break?
--
--
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
property numberOfScriptRuns : 0
set numberOfScriptRuns to numberOfScriptRuns + 1
display dialog "Number of times this script has run to date: " & numberOfScriptRuns & ""
set the logFile to ((path to desktop) as text) & "FileRunCounter.txt"
set the logText to "Number of times this script has run to date: " & numberOfScriptRuns & " " & "Script was last ran on " & (current date)
try
open for access file the logFile with write permission
write ((logText) & return) to file the logFile starting at eof
close access file the logFile
on error
try
close access file the logFile
end try
end try
Have you upgraded to Big Sur? Properties in applets are no longer updated when the scripts are run. You have to store the values somewhere yourself each run.
aspaceintime. If the script does not work because you are running a recent version of macOS, there are a number of alternatives. One is to parse the last line of text in the log. This script needs some refinement but works in my testing.
set the logFile to (path to desktop as text) & "FileRunCounter.txt"
try
set logContent to paragraph -2 of (read file logFile)
set numberOfScriptRuns to text ((offset of "date: " in logContent) + 6) thru -1 of logContent
set numberOfScriptRuns to text 1 thru ((offset of " " in numberOfScriptRuns) - 1) in numberOfScriptRuns
on error
set numberOfScriptRuns to 0
end try
set numberOfScriptRuns to numberOfScriptRuns + 1
display dialog "Number of times this script has run to date: " & numberOfScriptRuns & ""
set the logText to "Number of times this script has run to date: " & numberOfScriptRuns & " " & "Script was last ran on " & (current date)
try
open for access file the logFile with write permission
write ((logText) & return) to file the logFile starting at eof
close access file the logFile
on error
try
close access file the logFile
end try
end try
The second alternative is to count the number of lines in the log, but this will only work if you don’t manually edit the log. The actual count part of the script might be something like:
set the logFile to (path to desktop as text) & "FileRunCounter.txt"
set logContents to paragraphs of (read file logFile)
set numberOfScriptRuns to ((count logContents) - 1)
I am not on the Big Sur, so I can’t test following script. On the Catalina it works.
script o
property numberOfRuns : 0
end script
set o's numberOfRuns to (o's numberOfRuns) + 1
or, better:
script o
property numberOfRuns : 0
on updateNumberOfRuns()
set numberOfRuns to numberOfRuns + 1
end updateNumberOfRuns
end script
o's updateNumberOfRuns()
The issue is that script persistence of properties works (outside an editor) by re-saving the script to disk each time a property (or other top-level value) is modified. Once you start code-signing applets, as is always done in Big Sur, that’s out of the question.
Another solution is my PrefsStorageLib script library, which uses the standard defaults mechanism:
https://latenightsw.com/freeware/
I have a late model iMac running Catalina version 10.15.1 and this code does the trick. Thanks for the help…
I already had a custom .scpt, .scptd, & .app - Logging system set up on my computer (which logs the time and date, to file, each time you run one of your custom script, script bundle, or applet files)… but I tweaked it a little bit to suit your needs.
This following AppleScript code is what I consider the “Setup Script”. Running this code will create a “Script Libraries” folder if it doesn’t already exist in your Users/YOUR_SHORT_NAME/Library folder. It then creates a “Script_Run_Log_Library.scpt” file in that folder with the code from the script scriptLogger
portion of the following script. It will also create a “Logs” folder on your Desktop if it does not already exist.
property scriptLibrariesFolder : ((path to library folder from user domain) as text) & "Script Libraries:"
property logsFolder : ((path to desktop) as text) & "Logs:"
try
alias scriptLibrariesFolder
on error errMsg number errNum
tell application "System Events"
set name of (make new folder at folder ¬
((path to library folder from user domain) as text)) ¬
to "Script Libraries"
end tell
end try
try
alias logsFolder
on error errMsg number errNum
tell application "System Events"
set name of (make new folder at folder ¬
((path to desktop) as text)) to "Logs"
end tell
end try
storeScript(scriptLogger, "Script_Run_Log_Library.scpt")
on storeScript(someScript, newScriptFileName)
set scriptObject to store script someScript in ¬
(scriptLibrariesFolder & newScriptFileName) replacing yes
end storeScript
script scriptLogger
on scriptRunLog(myName)
set currentDate to (current date) as string
set theFile to (path to desktop as text) & ¬
"Logs" & ":" & "Script_Run_Log.log"
set myName to myName
set theText to myName & " was last run on " & currentDate
do shell script "echo " & quoted form of theText & " >> " & ¬
quoted form of POSIX path of theFile
set scriptLaunches to (do shell script "cd ~/Desktop/Logs ;cat Script_Run_Log.log |sort -f |grep -Eo \".+.scpt|.+.scptd|.+.app\" |uniq |while read line ;do echo \"$line\" \" --> \" Launch Count : \"$(grep -c \"$line\" Script_Run_Log.log)\" ;done")
do shell script "cd ~/Desktop/Logs ;echo " & quoted form of scriptLaunches & ¬
" > Script_Run_Log_Tally.txt"
end scriptRunLog
end script
After running the above AppleScript code, you can now use your “Script_Run_Log_Library.scpt” as a Script Library to log to file… each time you run one of your custom script, script bundle, or applet files… to file ~/Desktop/Logs/Script_Run_Log.log and also a creates a second file which shows the name of each file and how many times each was run (to file “Script_Run_Log_Tally.txt”).
Now you’re set up to start logging any of your custom script, script bundle, or applet files by simply inserting this following AppleScript code at the top of any of those files.
use myLib : script "Script_Run_Log_Library"
use scripting additions
set myName to name of (info for (path to me))
myLib's scriptRunLog(myName)
These two following images show how your new log files will look.