In the developer documentation there is the file file:///Developer/ADC%20Reference%20Library/documentation/Carbon/Reference/Memory_Manag_nt_Utilities/Reference/reference.html
In this file a function TickCount is described:
How can I use this function from within an ASS application?
You can also use Jon’s Commands for ticks or unix. Here’s an example of python:
set t to "import time
print time.time()
"
set new_t to (ReplaceText(return, ASCII character 10, t))
set t_py to quoted form of new_t
set x to do shell script "python -c " & t_py
delay 5
set y to do shell script "python -c " & t_py
set d to (y as number) - (x as number)
set c to (round (d * 100) rounding down) / 100
{x, y, d, c}
--
-- search, replace, the text t
on ReplaceText(s, r, t)
set dtid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {s}
set t_list to text items of t
set AppleScript's text item delimiters to {r}
set new_t to t_list as string
set AppleScript's text item delimiters to dtid
return new_t
end ReplaceText
On my computer, the time module returns down to 100ths of a second. The results are usually off by 6 to 9 hundredths of a second due to the shell call.
Others have already provided a solution, but I wanted to answer this question specifically.
C functions can’t be called directly from Studio. You need to wrap them in a Cocoa method and call that instead. Off the top of my head, you could add this to a new class in your project:
That will return a text string that is the number of ticks since system startup.
And then call that from your script with “call method”:
set t to (call method "getTickCount" of class "YourClass")
Note: This hasn’t been tested. I just jotted it down off the top of my head to give you an idea of what’s involved with using C functions in AppleScript Studio.
UNIX experts like Mikey-San wrote that you should add the directory that the unix command resides in. Here’s the correction:
set t to "import time
print time.time()
"
set new_t to (ReplaceText(return, ASCII character 10, t))
set t_py to quoted form of new_t
set x to do shell script "/usr/bin/python -c " & t_py
delay 5
set y to do shell script "/usr/bin/python -c " & t_py
set d to (y as number) - (x as number)
set c to (round (d * 100) rounding down) / 100
{x, y, d, c}
--
-- search, replace, the text t
on ReplaceText(s, r, t)
set dtid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {s}
set t_list to text items of t
set AppleScript's text item delimiters to {r}
set new_t to t_list as string
set AppleScript's text item delimiters to dtid
return new_t
end ReplaceText
If you’re interested only in measuring time between different actions, I think NSDate methods are accurate enough. For example:
set t1 to (call method "timeIntervalSinceReferenceDate" of class "NSDate")
delay 1
set t2 to (call method "timeIntervalSinceReferenceDate" of class "NSDate")
t2 - t1 --> around 1. Ie: 0.996235013008, 1.002915978432, ...
If you’re interested really in OS uptime, you can use the various methods exposed before
From Yosemite and up this should work ” tested on El Capitan: Important: the GetTick script should be last part of your code or script won’t compile. You can also save it and use it in a script library.
set t1 to GetTick's Now()
delay 1
set t2 to GetTick's Now()
t2 - t1
script GetTick
use framework "Foundation" --> for more precise timing calculations
on Now()
return (current application's NSDate's timeIntervalSinceReferenceDate) as real
end Now
end script
Does ASObjC work in AppleScript-Studio environment in Yosemite? Running some old AppleScript-Studio applications in Sierra but wasn’t able to run ASObjC code in it.