Sorry, I can’t get my head around the post that describes how to convert seconds to Hours:Minutes:Seconds
Help doing time based calculations would someone mind helping me with a ‘simpler’ solution to convert seconds into Minutes:Seconds? I’m timing a trigger.
I tried to create the macro that’s listed in that post and was not successful. -
See this AppleScript:
A routine to convert seconds to hh:mm:ss (hours:minutes:seconds).
Thanx J- for the AppleScript, I should have looked for that! ~ I’m trying to force myself to learn the KBM way , do you understand the desciption of the macro in that post I referred to in my initial post? I almost get it, but can’t build it correctly. Would you be able to make that into a macro that works? It would help me learn- thank you for your time and expertise-
I'm not sure I understand your question or objective.
AppleScript and Keyboard Maestro are both great tools. Sometimes it is easier to do a process in one or the other. In this case, I think AppleScript is the best choice.
In one KM Action, Execute AppleScript, you can convert seconds to formatted time.
Whereas it will take many KM Actions to achieve this.
This is a very common function. So I would just add it to my AppleScript library, and use in KM as needed.
The only thing you need to add to the AppleScript is to get the value of seconds from a KM variable.
Just add this code to the top:
tell application "Keyboard Maestro Engine" to set totalSeconds to getvariable "KM_Seconds"
return FormatSeconds(totalSeconds)
You will need to set a KM Variable "KM_Seconds" to the total seconds you want converted/formatted, then do the Execute AppleScript Action.
Understood-
I was only trying to better learn & understand KBM and the macro that Peter was explaining in that post. That would be my objective - no worries, thanx
Use the Set Variable to Text %CurrentClipboard% action to copy the value into a variable.
Use the Set Variable Time to Calculation Value MOD 60 to get a time Seconds component. Format as 00 to ensure it uses two digits.
Use the Set Variable Time to Calculation ((Value - Seconds) / 60) MOD 60 to get a time Minutes component. Format as 00 to ensure it uses two digits.
Use the Set Variable Time to Calculation (Value - Minutes * 60 - Seconds)/3600 to get a time Hours component.
Then Set Clipboard to Text %Variable%Hours%:%Variable%Minutes%:%Variable% Seconds%
Something like that should do the trick.
@peternlewis’ proposal translates to this macro:
_Time Conversion.kmmacros (2.9 KB)
Thanks to the With Format option, I find – in this case – the KM solution is more elegant than the aforementioned AppleScript solution from jesseweb.com:
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)
if length of (theHours as text) = 1 then
set theHours to "0" & (theHours as text)
end if
if length of (theMinutes as text) = 1 then
set theMinutes to "0" & (theMinutes as text)
end if
if length of (theRemainderSeconds as text) = 1 then
set theRemainderSeconds to "0" & (theRemainderSeconds as text)
end if
set theTimeString to theHours & ":" & theMinutes & ":" & theRemainderSeconds as text
return theTimeString
end FormatSeconds
For reference, there are also generic functions from intSeconds -> [mins, hours, days, weeks]
at:
http://rosettacode.org/wiki/Convert_seconds_to_compound_duration#JavaScript
http://rosettacode.org/wiki/Convert_seconds_to_compound_duration#AppleScript
nice, thank you
Nicely done, @Tom!
I agree, your non-script KM solution is defintely the more compact.
I also agree with "more elegant", but that is a subjective evaluation.
However, both the AppleScript and JavaScript solutions could come in handy if you need to perform the function as part of a larger script. All good tools to have in one's tool kit.
Not mine. It’s just a literal transcription of Peter’s instructions.
Just thought I'd add this as another possible solution. I was just looking for it, and it was hard to find.
Thank you @Tom for the macro.
I'd like to add days to the calculation;
would the following be correct?
Created with KM.v.8.2.4:
This should work:
_Seconds to days:hours:minutes:seconds.kmmacros (3.1 KB)
Use with caution and make some more tests, I’m not good at maths. The changed and new calculation actions are selected in the screenshot.
I converted all variables to local variables, since we don’t need global variables here.