JMichaelTX
2/22/2016 - 5:42 AM

AppleScript Script #Timer using ASObjC

AppleScript Script #Timer using ASObjC

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")
###——————————————————————————————————————————————
#      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
###——————————————————————————————————————————————