FlexTime Scripted Cues

August 6th, 2006

I’ve posted FlexTime 1.0b8, which contains a great number of minor fixes, as well as my first stab at built-in Help documentation. While you’re waiting for the excitement of WWDC to commence, why not spend a few minutes optimizing your favorite timed activities?

AppleScript got a great deal of enhancement on this round. I finally implemented a scripting interface for setting cue parameters, and fixed a few little bugs that were preventing FlexTime’s Sofa Control Plugin from working as well as it could.

A scripting feature I didn’t really go into detail about last time is an enhancement I’ve added to the “Run Script” functionality. If FlexTime finds an AppleScript handler called “HandleFlexTimeCue,” it will attempt to call it directly instead of running the script as a whole. This mechanism gives your scripted cues access to information about the activity and routine they are being asked to cue on behalf of. For instance, a simple scripted cue that displays the name of the activity and routine document might look like this:


-- If somebody runs us directly, set them straight
display dialog "This is a FlexTime cue script! " & ¬
	"Don't run me like that!"

on HandleFlexTimeCue(myDocument, myRoutine)
	
	-- Just beep for fun
	beep
	
	-- And ask FlexTime to show a message 
	set routineName to name of myDocument
	set activityName to name of myRoutine
	set myText to "Time to do " & activityName & ¬
		" for " & routineName
	tell application "FlexTime"
		display message myText ¬
			at screen position bottom left ¬
			dismissing after delay 5
	end tell
	
end HandleFlexTimeCue

This script also demonstrates how you might use a “Run Script” cue handler to overcome some of the shortcomings in FlexTime’s built-in cues. For instance, the text message displayed by the above script is configured to automatically dismiss after 5 seconds, and is positioned at the lower-left hand of the screen.

As FlexTime evolves the UI will get more powerful, but it will probably always lag somewhat behind the power exposed by the scripted interface. The Run Script cue is a good option for FlexTime routines that need to push the envelope of possible actions.

Oh, and one more not-so-obvious feature of the “Run Script” cue is that it will launch/run/open practically anything you can think of. It’s using the same mechanism as FastScripts, which is very liberal in its willingness to call something a “script.” It will run shell scripts, launch/activate applications, run Automator workflows – you can even ask it to open a document file for you.

Stay tuned for more on FlexTime’s secret powers :)

3 Responses to “FlexTime Scripted Cues”

  1. Ryan Ballantyne Says:

    The scripting support in this release feels silky smooth (if such superlatives can be meaningfully applied to scripting support, that is).

    This post has made me realize that you can use the scripting support to create new types of cues. For example, here’s one that creates a countdown (or up):

    on HandleFlexTimeCue(theDocument, theActivity)
    	--Parameters are set by the cue name. The format is:
    	-- Count up|down numReps beatDuration [cueSound]
    	-- where up or down specifies the direction of the count,
    	-- numReps specifies how many counts to make,
    	-- and beatDuration tells how long (in seconds) each count should take.
    	-- The optional cueSound is a string of the name of the sound that should be played on each beat.
    	set text item delimiters to " "
    	tell application "FlexTime"
    		set actName to name of theActivity
    		set paramsList to get text items of actName
    		
    		set numReps to item 3 of paramsList
    		set beatDuration to item 4 of paramsList
    		set upOrDown to item 2 of paramsList
    		
    		if upOrDown is "down" then
    			set cnt to numReps
    		else
    			set cnt to 1
    		end if
    		repeat numReps times
    			display message cnt dismissing after delay beatDuration
    			delay beatDuration
    			
    			if upOrDown is "up" then
    				set cnt to cnt + 1
    			else
    				set cnt to cnt - 1
    			end if
    		end repeat
    	end tell
    end HandleFlexTimeCue
    

    So awesome. I love Applescript.

    I wanted to make it say the count, too, but the time it took to speak the number messed with the timing of the beats. Hmm…

  2. Daniel Jalkut Says:

    Ryan: Awesome! I’m really glad you’re playing with the scripting interface and making good progress with it. I took the liberty of putting “<pre>” tags around your script to improve the readability.

    The problem with the timing getting messed up on time-intensive scripted cues is something I hope to improve in the “engine” of FlexTime as time goes on. I am currently sort of firing the cue directly from the engine but over time I hope to make it so it sort of “spins off” the cue mechanism and allows the timing to continue completely uninterrupted.

  3. Ryan Ballantyne Says:

    Funny thing; I put <pre> tags in too, and they didn’t stick.

Comments are closed.

Follow the Conversation

Stay up-to-date by subscribing to the Comments RSS Feed for this entry.