Scriptable Timer / Stopwatch

I need a very timer/stopwatch (like a kitchen timer with start, stop, and reset):

–that is not a Widget/Dashboard app (needs to run as an app in the Finder environment)

–whose features are either: a) scriptable via AppleScript, or b) have keyboard shortcuts

Looking to have a stopwatch for billing purposes for when doing certain types of work. Want to be able to access the timer features via my X-keys using ControllerMate (which means I can do it via keyboard shortcuts or via embedded AppleScripts, which is pretty cool).

If you do not need a built-in UI or high accuracy, you could use a stay-open AppleScript application to record start/stop times and give totals. Because of the AppleEvent overhead, the times may be off a bit. Here is a rough version that uses a script object (though it could be done with just plain lists.):

-- TimerManager: saved as a stay-open "application bundle" ("application" in Snow Leopard)
property timers : {}

to listUsedTimerNumbers()
	local usedTimers, i
	set usedTimers to {}
	repeat with i from 1 to length of timers
		if item i of timers is not missing value then ¬
			set end of usedTimers to i
	end repeat
	usedTimers
end listUsedTimerNumbers

to _findUnusedTimerNumber()
	local timerNumber
	repeat with timerNumber from 1 to length of timers
		if item timerNumber of timers is missing value then ¬
			return timerNumber
	end repeat
	set end of timers to missing value
	return length of timers
end _findUnusedTimerNumber
to createTimer()
	local timerNumer
	set timerNumber to _findUnusedTimerNumber()
	set item timerNumber of timers to _makeTimer()
	return timerNumber
end createTimer

to removeTimer(timerNumber)
	set item timerNumber of timers to missing value
end removeTimer

to startTimerNumber(timerNumber)
	tell item timerNumber of timers to start()
end startTimerNumber

to stopTimerNumber(timerNumber)
	tell item timerNumber of timers to |stop|()
end stopTimerNumber

to getElapsedTimeOfTimerNumber(timerNumber)
	tell item timerNumber of timers to total()
end getElapsedTimeOfTimerNumber

to _makeTimer()
	script timer
		property finishedIntervals : {}
		property mostRecentStartDate : missing value
		to start()
			local now
			set now to current date
			if mostRecentStartDate is missing value then
				set mostRecentStartDate to now
			else
				error "already started"
			end if
		end start
		to |stop|()
			local now
			set now to current date
			if mostRecentStartDate is not missing value then
				set end of finishedIntervals to {mostRecentStartDate, now}
				set mostRecentStartDate to missing value
			else
				error "not running"
			end if
		end |stop|
		to total()
			local now, t
			set now to current date
			set t to 0
			repeat with interval in finishedIntervals
				set {start, |stop|} to interval
				set t to t + (|stop| - start)
			end repeat
			if mostRecentStartDate is not missing value then ¬
				set t to t + (now - mostRecentStartDate)
			return t
		end total
	end script
end _makeTimer
on run
	tell application "TimerManager"
		launch
		set a to createTimer()
		set b to createTimer()
		
		startTimerNumber(a)
		startTimerNumber(b)
		delay 5
		stopTimerNumber(a)
		delay 1
		stopTimerNumber(b)
		delay 2
		startTimerNumber(b)
		delay 3
		stopTimerNumber(b)
		set |data| to {getElapsedTimeOfTimerNumber(a), getElapsedTimeOfTimerNumber(b)}
		removeTimer(a)
		set newA to createTimer()
		removeTimer(b)
		set newB to createTimer()
		set |data| to |data| & {a, newA, b, newB}
	end tell
end run

You would probably want to add some sort of checkpoint/restore feature and maybe “named” timers (to access timers by a descriptive name instead of an opaque number). Oh, and there is no reset. It would be easy enough to add though; removeTimer and createTimer can simulate it as long as you can stand a potential change in timer number.

Yeah, I need a user interface so I can start/stop the timer as needed. I get interrupted alot. So I need a “pause” or “start/stop” feature, which would be handiest via a UI. At minimum I need a visual “clock” on screen showing elapsed…start/stop/reset could be via script-only if pressed.

Kinda why I was hoping someone knew of a basic stopwatch/timer app that had AppleScript support or could at least be UI scripted.

Or another thought–just a nice timer which has keyboard shortcuts for star/stop/pause/reset.

Did you figure this out?

Hi kamil,

Do you mean a stopwatch (with or without split time) or an egg timer?

Edited: also, what kind of accuracy (seconds, decimals, etc.)?

gl,
kel

Hi,

Here’s a rough stopwatch with pause if you’re interested. It uses the reopen handler to toggle the pause on and off. It might still need more testing, but I think it works to stop the stopwatch, quit the app.

Note that there is a bug in stay open apps, where if there is an error, then the app hangs and you need to force quit. Hence the try handler for the dialog in case the user cancels and you get an error 128.

There are various ways to inform the user of split times and total times.

Remember to save the app as a stay open application.

I don’t know if you wanted a Timer or a stopwatch, but good luck in your endeavors.

Edited: oopsy, forgot to post the script :slight_smile:

global run_time, is_paused, start_time

on run
	set run_time to 0
	set is_paused to false
	set start_time to current date
end run

-- reopen to toggle pause and restart
on reopen
	if not is_paused then -- pause
		-- record run time
		set cur_date to current date
		set run_time to run_time + (cur_date - start_time)
	else -- reset start_time to restart timer
		set start_time to current date
	end if
	-- toggle pause
	set is_paused to not is_paused
end reopen

on quit
	-- display total run time without the pauses
	set cur_date to current date
	set run_time to run_time + (cur_date - start_time)
	try -- trap error bug if user cancel dialog
		display dialog "" & run_time & " secs"
	end try
	continue quit
end quit

Edited: and btw, I have a cocoa script written by Shane Stanley, where you can use modifier keys instead of rerunning the app. this can be used to toggle the pauses. Something like the old scripting addition by jon or John’s scripting additions. Shanes cocoa script is posted somewhere on this site also. gl

kel

@kel1
My goal is to:
â–¸ create a stopwatch, which counts in format hh:mm:ss, from 00:00:00 up.
â–¸ I need only Start, Stop, Reset function.
â–¸ being able to copy the time mark thru AppleScript and paste it using Keyboard Maestro

The purpose is that is that when I’m recording a lecture, sometimes I can’t catch up with the teacher, so I just write “???” to get back to the topic. Also, when I’m not sure about certain concept, before I ask the question I always make sure I got everything covered from what the lecturer said. I’m using OmniOutliner for note taking, and I record the audio inside the app. Since I always see the time, I can write the time mark with ±10 sec delay, however an AppleScript solution would be a great thing, making life a little easier, possibly for more than just one student.

Any ideas where should I start? I’m fairly new to AppleScript and my only experience with code is with HTML.
But, I’m eager to learn. I just need a little guidance.

Thanks a lot,
K.

Hi kamil,

All you want then is ‘time string’ of ‘current date’.

time string of (current date)

If you want it in 24 hour format, then you’ll need to format it which is easily done through AppleScript or unix.

But, I seem to remember an easy way to time stamp documents. Can’t remember what it was at the moment, but try to remember. Note that there are many better experts on this site and I hope they are back from work or whatever soon.

gl,
kel

@kel1

I might was not fully clear. I would need a stopwatch feature, meaning I start the time, and is starts counting up from 00:00:00.

I need to easily access the exact time of the audio recording for the content I want to hear again.

The way I think about it is:
â–¸ I press the “Record” button in my software
â–¸ 1-2 seconds after I press the “Start” button in the Stopwatch
â–¸ the time stamp is the same, like the audio
¢ assuming I paste the timestamp from the Stopwatch near the correct content

Thanks,
K.

Hi kamil,

I think I see what you’re trying to do. Something like watching a movie and you run the cursor over the time line and when you find the right time, you click the cursor and the movie starts at that time. Just an analogy.

Interesting. How do you know what time to choose? Is it in a list? I need to read your post again.

Later,
kel

Hi kamil,

What I would do is take a movie of the whole lecture, then at various times throughout the lecture I would record the times where I might want to go back to. This is easily done by scripting QuickTime Player.

Edited: I have never done this, but I would think that you would want to set the resolution very low until you come to the parts where you need it to be higher.

gl,
kel

Hi,

very, very, very quick&dirty : StopWatch.zip (code-signed)
Written in Swift, so minimum system version is Mac OS 10.9 Mavericks

¢ 1 button start/stop
¢ reset with ⌘R

AppleScript dictionary:

3 commands : start, stop and reset
1 property : current time (string)

Sample code:


tell application "StopWatch"
	start
	delay 5
	stop
	delay 5
	set currentTime to current time
	reset
end tell

@StefanK Holy crap! This is everything I need. It’s amazing! I’m doing a happy dance now!!!

Thanks so much!

BTW. How do I learn exactly what you did? I want to learn and understand this.

You’re welcome. It took about 30 minutes to write it

I’m afraid this is not trivial. This application is a Cocoa application using Apple’s developer tool Xcode.
You have to gain knowledge in object oriented programming (Objective-C and Swift) and “ still more difficult for beginners “ in Cocoa Scripting which is poorly documented.

In my case it took about a year to get those skills

You just gave me an interesting idea for something else!
It would be too much of work and disk space (storing on Google Drive and sharing with other students) for the purpose I need.

This is very interesting. I want to explore this more.
The conflict I see is that when I cannot catch up, I would want to have also some information back, like starting 30 seconds before I hit record.
I think recording a whole lecture video of my screen would be too much, because it wouldn’t give me any additional info, since I would see only my screen and hear the teacher.

Any thoughts? Let’s brainstorm on this, I feel like something cool can come out from this.
Possible uses:
â–¸ School
¢ Student Collaboration
· Recording study groups for those who cannot participate
“They could view stuff only which is necessary and skip the stuff they know
¢ Tutoring and Online Labs
· Once explaining something, would be easy to “chop” down the file and split it by its content (or link to the proper time)

â–¸ Meetings and Conferences
¢ One person is outlining the main points/quotes of the presenter
· Easy to navigate later on, without the need for the transcripts

â–¸ and of course more, these above are just from the top of my head

I really appreciate your time.

I will start with Objective-C then :slight_smile:

Hi kamil,

Technology is really great for teaching. After all, that is what the students grew up with. If you were very serious, you might want to get QuickTime Pro. I think that’s what they still call it.

Anyway, the scripting part is quite easy to learn I think, but if you want to jump into objective c then that might prove to be a little harder especially if you haven’t had any programming experience.

If that’s the case, then there’s this thing called AppleScript-ObjectiveC I think that was the name that you can use in Xcode. What it is is basically ObjectiveC made easy by combining AppleScript (a scripting language) with ObjectiveC (a lower level language). Note that a lower level language is harder as it gets closer to machine language (I think a bunch of zeroes and ones).

Anyway, If you start with a scripting language and then integrate that with a lower language then it might be easier. Of course, it depends on your learning style. Some People can just catch on to it and others cannot do that (me :D). But then everybody has their strong and weak points.

Just my thoughts.

gl,
kel

@StefanK

In your honor, here I present “Steffuhr” “ the Stopwatch app.
â–¸ the app name comes from the German word, “Stoppuhr,” which uncle Google claims it means stopwatch.

You can see the logo here.

I uploaded the Steffuhr app with the fixed logo here (file “Steffuhr Stopwatch.zip”).

Thank you so much for everything. I will be posting here about any updates I will do, or questions I will have.

appreciated, but the German word for stopwatch is actually “Stoppuhr”

Ohh yes, my bad. Uncle Google was right. Fixed it. Still, the app name comes from your name â–¸ STEFan+stoppUHR=Steffuhr (to make the “pp” consistent I doubled the “f”). I hope it doesn’t sound funny in German and sort of makes sense.

My 1st language isn’t English and I know that native English speakers don’t get making up or changing the names. Not the case where I come from (Poland), where people make up names all the time.