Monday, November 19, 2018

#1 2010-11-10 06:25:36 am

JayBird
Member
Registered: 2009-12-28
Posts: 132

Timer AppleScript??

Hey Guys,

I've done a little bit of digging but cannot find what i am after. I want to create a timer that starts when i lanch one of my applescripts.

I've seen a few examples but they are to complex for what i need.

I either need it to write the time it starts, till the time it finsihes to a text file - Note i am pushing a command through terminal

or

pop up in a dialog box with an OK button, counting up until i press ok.

Are these possiable?

I am also looking into taking the variable and pushing it out as a Growl Notfication e.g

Total Time:
Started At:
Finshed At:

Last edited by JayBird (2010-11-10 06:53:58 am)


Happy Days

Offline

 

#2 2010-11-10 07:02:23 am

JayBird
Member
Registered: 2009-12-28
Posts: 132

Re: Timer AppleScript??

I've found something, that kind of does what i want, if anyone can think of a quicker / cleaner method, any help would be appreaciated


Applescript:


on run -- example
set startTime to (current date)
display dialog "Wait for a few moments before clicking \"OK\" for elapsed time..." with title (startTime as text)
set EndTime to (current date)
display dialog "Start: " & (startTime as text) & return & "End: " & (EndTime as text) & return & return & "Elapsed: " & TimeToText(EndTime - startTime) with title "Elapsed Time"
end run


on TimeToText(TheTime)
(*
converts a number to a time string
parameters - TheTime [integer]: the time in seconds
returns [text]: the time in the format hh:mm:ss
*)

if (class of TheTime) as text is "integer" then
set TimeString to 1000000 + 10000 * (TheTime mod days div hours) -- hours
set TimeString to TimeString + 100 * (TheTime mod hours div minutes) -- minutes
set TimeString to (TimeString + (TheTime mod minutes)) as text -- seconds
tell TimeString to set TheTime to (text -6 thru -5) & ":" & (text -4 thru -3) & ":" & (text -2 thru -1)
end if
return TheTime
end TimeToText


Happy Days

Offline

 

#3 2010-11-10 09:12:58 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4720

Re: Timer AppleScript??

JayBird wrote:

… if anyone can think of a quicker / cleaner method …


That handler is quick, but it contains a messy version of my original time string code. (It also gives the wrong result if 'TheTime' is more than 86399. It was probably written for some specific situation.) The original code's the 'tell' block below:

Applescript:

-- Uncomment the commented code if t's likely to be a day or more.
on secondsToTimeString(t)
   -- set d to t div days
   -- set t to t mod days
   tell (1000000 + (t div hours) * 10000 + (t mod hours div minutes) * 100 + t mod minutes) as text
       set timeString to (text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7)
   end tell
   -- set TimeString to text 2 thru 4 of ((1000 + d) as text) & ":" & TimeString
   
   return timeString
end secondsToTimeString


NG

Offline

 

#4 2010-11-10 09:18:58 am

JayBird
Member
Registered: 2009-12-28
Posts: 132

Re: Timer AppleScript??

Many Thanks Nigel,

No offence ment on the Cleaner / quicker method comment,

Jay


Happy Days

Offline

 

#5 2010-11-10 02:32:42 pm

JayBird
Member
Registered: 2009-12-28
Posts: 132

Re: Timer AppleScript??

Hi Nigel,


Is there anyway to get this to run through terminal?

At present i run a backup script via terminal and i want it to record the beginning and end times, followed my time taken.

For example at present i have the following:

Applescript:


   tell application "Terminal"
       activate

do script "cp -Rv $HOME/Pictures /Volumes/Drive/" in window 1

End Tell

I have written a little extra to the code as a shell script rather than a script and am a little stuck....

Applescript:



on run -- example
   
   set desktopPath to "$HOME/Documents/" -- set dir to create files in
   set BookFolders to "mkdir -p " & desktopPath & "'AFolder'/" -- specify name of doc and commands
   set myLog1 to "$HOME/Documents/AFolder/Time.txt" -- creating a log
   
   do shell script BookFolders -- --- create a folder note: if not created
   
   
   set startTime to (current date)
   display dialog "Wait for a few moments before clicking \"OK\" for elapsed time..." with title (startTime as text)
   set EndTime to (current date)
   display dialog "Start: " & (startTime as text) & return & "End: " & (EndTime as text) & return & return & "Elapsed: " & TimeToText(EndTime - startTime) with title "Elapsed Time"
   
   
   do shell script "echo " & return & "Test Time " & startTime & return & EndTime & return & TimeToText(EndTime - startTime) & ">>" & myLog1

   
   close
   
   
end run


on TimeToText(TheTime)
   (*
converts a number to a time string
parameters - TheTime [integer]: the time in seconds
returns [text]: the time in the format hh:mm:ss
*)

   if (class of TheTime) as text is "integer" then
       set TimeString to 1000000 + 10000 * (TheTime mod days div hours) -- hours
       set TimeString to TimeString + 100 * (TheTime mod hours div minutes) -- minutes
       set TimeString to (TimeString + (TheTime mod minutes)) as text -- seconds
       tell TimeString to set TheTime to (text -6 thru -5) & ":" & (text -4 thru -3) & ":" & (text -2 thru -1)
   end if
   return TheTime
end TimeToText

Last edited by JayBird (2010-11-10 02:33:36 pm)


Happy Days

Offline

 

#6 2010-11-11 03:04:03 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4720

Re: Timer AppleScript??

You probably want the 'osascript' shell command, which can run lines of AppleScript code or entire scripts — provided they don't contain any user interaction, such as dialogs. You seem to be more at home with shell commands than I am, so you'd probably find it easier than I would to implement. But of course ask again if you're still stuck. smile


NG

Offline

 

#7 2010-11-11 06:19:51 am

JayBird
Member
Registered: 2009-12-28
Posts: 132

Re: Timer AppleScript??

Still Stuck sad

Although Nearly There:

I need something to make the do shell script wait until the terminal has finished / closed

Applescript:



on run -- example
   
   set desktopPath to "$HOME/Documents/" -- set dir to create files in
   set BookFolders to "mkdir -p " & desktopPath & "'AFolder'/" -- specify name of doc and commands
   set myLog1 to "$HOME/Documents/AFolder/Time.txt" -- creating a log
   
   
   do shell script BookFolders -- --- create a folder note: if not created
   
   
   set startTime to (current date)
   set EndTime to (current date)
   
   
   tell application "Terminal"
       activate
       
       
       
       do script "cp -Rv $HOME/Pictures /$HOME/Drive/" in window 1
       
       
       
   end tell
   
   do shell script "echo " & return & "Test Time " & TimeToText(EndTime - startTime) & ">>" & myLog1
   
   
   
end run


on TimeToText(TheTime)
   (*
converts a number to a time string
parameters - TheTime [integer]: the time in seconds
returns [text]: the time in the format hh:mm:ss
*)

   if (class of TheTime) as text is "integer" then
       set TimeString to 1000000 + 10000 * (TheTime mod days div hours) -- hours
       set TimeString to TimeString + 100 * (TheTime mod hours div minutes) -- minutes
       set TimeString to (TimeString + (TheTime mod minutes)) as text -- seconds
       tell TimeString to set TheTime to (text -6 thru -5) & ":" & (text -4 thru -3) & ":" & (text -2 thru -1)
   end if
   return TheTime
end TimeToText


Have Been Playing with this with no luck:

Applescript:




on run -- example
   
   set desktopPath to "$HOME/Documents/" -- set dir to create files in
   set BookFolders to "mkdir -p " & desktopPath & "'AFolder'/" -- specify name of doc and commands
   set myLog1 to "$HOME/Documents/AFolder/Time.txt" -- creating a log
   
   
   do shell script BookFolders -- --- create a folder note: if not created
   
   
   set startTime to (current date)
   set EndTime to (current date)
   
   
   tell application "Terminal"
       activate
       
       
       
       do script "cp -Rv $HOME/Pictures /$HOME/Drive/" in window 1
       do script "exit" in window 1
       
       
   end tell
   
   if application "Terminal" is running then
       
       
   else
       
       do shell script "echo " & return & "Test Time " & TimeToText(EndTime - startTime) & ">>" & myLog1
       
   end if
   
   
end run


on TimeToText(TheTime)
   (*
converts a number to a time string
parameters - TheTime [integer]: the time in seconds
returns [text]: the time in the format hh:mm:ss
*)

   if (class of TheTime) as text is "integer" then
       set TimeString to 1000000 + 10000 * (TheTime mod days div hours) -- hours
       set TimeString to TimeString + 100 * (TheTime mod hours div minutes) -- minutes
       set TimeString to (TimeString + (TheTime mod minutes)) as text -- seconds
       tell TimeString to set TheTime to (text -6 thru -5) & ":" & (text -4 thru -3) & ":" & (text -2 thru -1)
   end if
   return TheTime
end TimeToText

Last edited by JayBird (2010-11-11 07:05:07 am)


Happy Days

Offline

 

#8 2010-11-11 01:50:49 pm

JayBird
Member
Registered: 2009-12-28
Posts: 132

Re: Timer AppleScript??

Really Really Stuck Now, Is anybody able to help me here, i cant go any further with this...


Applescript:




set startTime to (current date)
set desktopPath to "$HOME/Documents/" -- set dir to create files in
set BookFolders to "mkdir -p " & desktopPath & "'AFolder'/" -- specify name of doc and commands
set myLog1 to "$HOME/Documents/AFolder/Time.txt" -- creating a log


do shell script BookFolders -- --- create a folder note: if not created


tell application "Terminal"
   activate
   
   do script "cp -Rv $HOME/Downloads /$HOME/Desktop/Drive/" in window 1
   
   
   -- This makes the script wait for Terminal
   set a to 0
   repeat until (a = 1)
       if (window 1 is not busy) then
           
           set a to 1
           
       end if
   end repeat
   
   close window 1
   
   
   tell application "Terminal"
       quit
       
       
       set EndTime to (current date)
       do shell script "echo " & return & "Test Time " & TimeToText(EndTime - startTime) & ">>" & myLog1
       
   end tell
end tell




on TimeToText(TheTime)
   (*
converts a number to a time string
parameters - TheTime [integer]: the time in seconds
returns [text]: the time in the format hh:mm:ss
*)

   if (class of TheTime) as text is "integer" then
       set TimeString to 1000000 + 10000 * (TheTime mod days div hours) -- hours
       set TimeString to TimeString + 100 * (TheTime mod hours div minutes) -- minutes
       set TimeString to (TimeString + (TheTime mod minutes)) as text -- seconds
       tell TimeString to set TheTime to (text -6 thru -5) & ":" & (text -4 thru -3) & ":" & (text -2 thru -1)
   end if
   return TheTime
end TimeToText




Happy Days

Offline

 

#9 2010-11-12 02:37:09 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4720

Re: Timer AppleScript??

The works for me. Any good to you?

Applescript:

on main()
   set docsPath to (path to documents folder as text)
   do shell script "mkdir -p " & (quoted form of POSIX path of docsPath) & "'AFolder'/"
   set myLog1 to docsPath & "AFolder:Time.txt"
   
   set startTime to (current date)
   tell application "Terminal"
       activate
       do script "cp -Rv $HOME/Downloads /$HOME/Desktop/Drive/" in window 1
       repeat until (window 1's busy)
           delay 0.2
       end repeat
       repeat while (window 1's busy)
           delay 0.2
       end repeat
   end tell
   set EndTime to (current date)
   
   tell application "Terminal" to quit saving no
   
   set logText to "Start time: " & startTime & return & ¬
       "End time: " & EndTime & return & ¬
       "Time taken: " & secondsToTimeString(EndTime - startTime) & return & return
   
   set fref to (open for access file myLog1 with write permission)
   try
       if ((get eof fref) is 0) then write «data rdatEFBBBF» to fref -- UTF-8 BOM
       write logText as «class utf8» to fref starting at eof
   end try
   close access fref
   
   display dialog logText buttons {"OK"} default button 1
end main

on secondsToTimeString(t)
   tell (1000000 + (t div hours) * 10000 + (t mod hours div minutes) * 100 + t mod minutes) as text
       return (text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7)
   end tell
end secondsToTimeString

main()


NG

Offline

 

#10 2010-11-13 12:10:08 pm

JayBird
Member
Registered: 2009-12-28
Posts: 132

Re: Timer AppleScript??

AWESOME!! Thanks Nigel!!!!!


Happy Days

Offline

 

#11 2018-09-12 04:15:39 pm

Adak47
Member
Registered: 2018-08-18
Posts: 7

Re: Timer AppleScript??

I hope this thread is still alive, as I've found Jaybird's timer script very useful. I've modified it for my own use, and post the code below for anyone interested in testing it. My AppleScript experience is limited, and I'm sure the script shows that. Still, my Timer Countup script is handy if you want to start timing something, get a spit time update without stopping the timer, or be able to pause the timer while you answer the phone (say) and then resume execution afterwards. These are the additions I've provided.

Applescript:

-- Source: JayBird in https://macscripter.net/viewtopic.php?id=34653
-- Nigel Garvey points out that "TheTime" is only valid up to 86399 seconds (23.99 hrs) and offers a patch in a comment to the above post
-- Adak47 modifications: changed button definitions, titles, and provided for a split timer and a paused timer. The default mode of operation is
-- split timer mode, starting at 00:00:00, and updating whenever the Split Time button is pressed. Pressing the Pause button pauses the timer
-- and a Resume button starts the timer again. Finally, the Stop Timer button displays the final elapsed and pause adjusted elapsed times.
-- Current limitations: for timing events up to (but not including) 24 hrs duration.
-- TO DO: put in Garvey's patch

try
   -- initialize text and numeric variables
   set timerMode to "Split Time" -- set the default timer mode
   set PauseTotal to 0
   set Time1 to 0
   set Time2 to 0
   -- record the startTime and start the timer
   set startTime to (current date)
   repeat
       set EndTime to (current date)
       set AdjustedTime to EndTime - PauseTotal -- adjust EndTime time for the total pause delays
       if timerMode = "Split Time" then
           set timerMode to the button returned of ¬
               (display dialog ¬
                   "Start: " & (startTime as text) & return & "End: " & (EndTime as text) & return & return & ¬
                   "Split Time: " & TimeToText(AdjustedTime - startTime) ¬
                   buttons {"Split Time", "Pause Timer", "Stop Timer"} ¬
                   default button "Stop Timer" with title "Timer is Running")
       else if timerMode = "Pause Timer" then
           set Time1 to (current date) -- time at the beginning of the pause
           set timerMode to the button returned of (display dialog ¬
               "Start: " & (startTime as text) & return & "End: " & (EndTime as text) & return & return & ¬
               "Paused Time: " & TimeToText(AdjustedTime - startTime) ¬
               buttons {"Exit", "Resume Timer"} ¬
               default button "Resume Timer" with title "Timer is Paused")
           if timerMode = "Exit" then return -- leave in Pause mode without showing Stop Timer dialog
           set Time2 to (current date) -- time at the end of the pause
           set PauseTotal to PauseTotal + (Time2 - Time1) -- update the total pause time
           set timerMode to "Split Time" -- resume timing from the end of pause
       else if timerMode = "Stop Timer" then
           set PauseAdjusted to return & "Pause Adjusted Elapsed Time: " & TimeToText(AdjustedTime - startTime) -- adjusted time
           if PauseTotal = 0 then set PauseAdjusted to "" -- don't display adjusted time if it is the same as the unadjusted
           display dialog ¬
               "Start: " & (startTime as text) & return & "End: " & (EndTime as text) & return & return & ¬
               "Elapsed Time: " & TimeToText(EndTime - startTime) & PauseAdjusted ¬
               buttons {"OK"} default button "OK" with title "Timer is Stopped"
           return TimeToText(AdjustedTime - startTime)
       end if
   end repeat
on error errorMessage number errorNum
   display dialog "Error in script Timer Countup: " & errorMessage & return & errorNum
end try

on TimeToText(TheTime)
   -- converts a number to a time string
   -- parameters - TheTime [integer]: the time in seconds
   -- returns [text]: the time in the format hh:mm:ss
   if (class of TheTime) as text is "integer" then
       set TimeString to 1000000 + 10000 * (TheTime mod days div hours) -- hours
       set TimeString to TimeString + 100 * (TheTime mod hours div minutes) -- minutes
       set TimeString to (TimeString + (TheTime mod minutes)) as text -- seconds
       tell TimeString to set TheTime to (text -6 thru -5) & ":" & (text -4 thru -3) & ":" & (text -2 thru -1)
   end if
   return TheTime
end TimeToText


Filed under: timer, pause, count up, split time

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)