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
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
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:
-- 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
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:
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…
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
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.
I need something to make the do shell script wait until the terminal has finished / closed
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:
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
Really Really Stuck Now, Is anybody able to help me here, i cant go any further with this…
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
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()
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.
-- 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