Last active
December 3, 2024 10:18
-
-
Save JMichaelTX/f26967ae82b865bb7f4c to your computer and use it in GitHub Desktop.
AppleScript Script #Timer using ASObjC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###—————————————————————————————————————————————— | |
# timer(pAction) Calculate and Log Execution Time | |
# | |
# Ver 1.1 2016-02-21 | |
# | |
# REF: The base ASObjC code was provided by Shane Stanley | |
# | |
# HOW TO USE: | |
# • You may want to run the script at least 3 times | |
# • The first run may be high | |
# * For more extensive/exhaustive testing, see: | |
# Script Geek app by Shane Stanley | |
# http://www.macosxautomation.com/applescript/apps/Script_Geek.html | |
# | |
# REQUIRES: | |
# • These two statements at top of main script: | |
# use scripting additions | |
# use framework "Foundation" | |
# • Yosemite+ | |
# • function formatSeconds(totalSeconds) | |
# (provided below) | |
###—————————————————————————————————————————————— | |
on timer(pAction) | |
global gTimerStartDate | |
if (pAction = "start") then -- START CASE | |
set gTimerStartDate to current application's NSDate's |date|() | |
log "START: " & ((current date) as text) | |
else -- HANDLE CASES OTHER THAN START | |
set durationNum to -(gTimerStartDate's timeIntervalSinceNow()) | |
--- IF ≥ 60 SEC, FORMAT AS HR MIN SEC --- | |
if durationNum ≥ 60 then | |
set durationStr to formatSeconds(durationNum) | |
else | |
set durationStr to (round (durationNum) * 1000) / 1000.0 & " sec" | |
end if -- durationNum ≥ 60 | |
log pAction & ": | |
• " & ((current date) as text) & " | |
• EXECUTION TIME: " & durationStr | |
end if -- (pAction = "start") | |
end timer | |
###—————————————————————————————————————————————— | |
###—————————————————————————————————————————————— | |
# formatSeconds(totalSeconds) Convert Seconds to HR MIN SEC format | |
# | |
# Ver 1.1 2016-02-21 | |
# | |
# REF: http://www.jesseweb.com/coding/applescript/format-seconds-hhmmss/ | |
###—————————————————————————————————————————————— | |
on formatSeconds(totalSeconds) | |
set theHours to (totalSeconds div hours) | |
set theRemainderSeconds to (totalSeconds mod hours) | |
set theMinutes to (theRemainderSeconds div minutes) | |
set theRemainderSeconds to (theRemainderSeconds mod minutes) | |
set theRemainderSeconds to (round (theRemainderSeconds * 100)) / 100.0 | |
# set theTimeString to theHours & ":" & theMinutes & ":" & theRemainderSeconds as text | |
set theTimeString to theHours & " hr " & theMinutes & " min " & theRemainderSeconds & " sec" as text | |
return theTimeString | |
end formatSeconds | |
###—————————————————————————————————————————————— |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use AppleScript version "2.4" | |
use scripting additions | |
use framework "Foundation" | |
timer("Start") | |
--- YOUR CODE GOES HERE --- | |
--- NOTE: The below shell scripts have nothing to do with the timer() handler --- | |
set cmdStr to "perl -e 'use Time::HiRes qw(time); print time'" | |
set timeStart to do shell script cmdStr | |
timer("After First Shell Script") | |
set timeStop to do shell script cmdStr | |
--delay 5 | |
timer("STOP") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Log Output
(START: Sun, Feb 21, 2016 at 8:59 PM)
(*After First Shell Script:
• Sun, Feb 21, 2016 at 8:59 PM
• EXECUTION TIME: 0.034*)
(*STOP:
• Sun, Feb 21, 2016 at 8:59 PM
• EXECUTION TIME: 0.065*)