Count how many times a script is run

What is the best solution to count how many times I execute a spesific script?
A script-counter of some kind…

property runcount : 0
set runcount to runcount + 1

This should count the number of times that the script is run since compiled and saved. The counter would be reset if the script is open and recompiled.

A more permanent way to save a result whether it’s run from a compiled script or run from the script editor is to use a preference file created this way:

	set runs to (do shell script "defaults read ScriptRunCount theCount")
	set runs to 1 + runs
	do shell script ("defaults write ScriptRunCount theCount " & runs as text)
on error --> no such file yet
	set runs to 1
	do shell script ("defaults write ScriptRunCount theCount " & runs as text)
end try

You can find out the current value at any time with:

display dialog "This script has run " & (do shell script "defaults read ScriptRunCount theCount") & " times."

The preference file will be created in your user’s Library/Preferences folder with the name: “ScriptRunCount.plist”, so to set it back to one, just delete that file.

Thank you Adam!
This is just what I’m looking for.
My next question is how to save this count file on an server.
The script is placed on an server, and is run from different Macs in the network, and I want to gather to counting in one file, instead of one for each computer…

Is there a way to do this?


it’s also possible to use a script object, which will be saved into the main script.
Therefor the main script must be an application bundle

set PrefPath to (path to me as Unicode text) & "Contents:Resources:ScriptRunCount.scpt"

script ScriptRunCount
	property RunCount : 1
end script
	set CountPrefs to load script file PrefPath
	set CountPrefs's RunCount to (CountPrefs's RunCount) + 1
	display dialog CountPrefs's RunCount as string
	store script CountPrefs in file PrefPath replacing yes
on error
	store script ScriptRunCount in file PrefPath replacing yes
end try

If a user on Network runs a script on the server that writes to a plist, whose preferences is it written to? The server or the user?

I would have to test this, but I am almost positive it would write to a local plist since the script is running in the local application space. Which is backed by the fact that running a script with a shell commad would manipulate your local environment not the servers… mounted volumes are, for most purposes, considered local by the system.

I’ve tried Stefans suggestion, but something goes wrong, so the script count never passes 1…

Adams way worked great, so if somebody could help me on adjusting the code so that the file is saved on an monted volume, and not on my local disk, it would be great!

Did you start the script from the application bundle or from Script Editor?
In Script Editor path to me doesn’t work properly

Most of the scripts will be started from the Scripts pallette in InDesign.

Hm, I’ve tested the script successfully by starting the application bundle in the Finder
and with Script Debugger (which handles path to me correctly).
I don’t know whether InDesign’s behavior is different