Setting "Run script" cues via AppleScript
  • (Oh, how meta!)

    So what I'm trying to do is to set up a set of AppleScripts so I can enter a time in text mode in Quicksilver and have FlexTime make a new (temporary) routine on the fly with a single action to count down the time entered, then run a script to display a message, play a sound, and close the routine as the finishing cue.

    I've got it almost all worked out: I have a script with which I can use Quicksilver to open up a new FlexTime document, set the time and start it counting down, and another script that I can set as a Finishing Cue, which plays my sounds, displays my message, and closes the window.

    What I can't seem to figure out is how to set the finishing cue to call the second script from within the first (Quicksilver-triggered) AppleScript. I can set non-script finishing cues; for example:

    tell application \"FlexTime\"
    set action of finishing cue of first activity of document \"Untitled Routine\" to \"Play sound\"

    set parameter of finishing cue of first activity of document \"Untitled Routine\" to \"Glass\"
    end tell

    but the equivalent syntax for setting a script as a finishing cue doesn't work:

    tell application \"FlexTime\"
    set action of finishing cue of first activity of document \"Untitled Routine\" to \"Run Script\"

    set parameter of finishing cue of first activity of document \"Untitled Routine\" to \"BotBook:Users:adpeters:Desktop:QSFTNotify.scpt\"
    end tell

    will set the action correctly (to Run Script), but gives me an "NSUnknownKeyScriptError" and doesn't actually set the script file to run. I've double-checked that the path to the file is correct. What am I doing wrong?
  • Very cool idea. It looks like you've uncovered a bug in FlexTime's scripting support. I'm adding it to the list for a future bug-fix update.

    In the mean time a workaround is to specify the file in "POSIX File" format:

    set parameter of finishing cue of first activity of document \"Untitled Routine\" to \"/Users/adpeters/Desktop/QSFTNotify.scpt\" as POSIX file

    Hope this helps!

    Daniel
  • Thanks Daniel, that works great! (And thanks for telling me about the "as POSIX file" trick -- I've been getting very sick of using classic-Mac paths in my scripts.)

    FYI, here are the current versions of my scripts. They work pretty well, though some of the time they seem to cause FlexTime to hang after running the finishing cue, leaving the notification bezel on the screen.

    This is the Quicksilver script, saved as a script in ~/Library/Application Support/Quicksilver/Actions, with filename Timer.scpt . To run it, invoke QS, enter text mode (hit "." by default), and type the length of time you want (this script takes a real number and counts down in minutes; it's pretty trivial to change this), then choose "Timer" from the Action pane (you'll need to restart QS before it can see Timer as an action):

    using terms from application \"Quicksilver\"
    on process text theText
    tell application \"FlexTime\"
    set theDocName to \"QS\" & theText

    make document with properties {name:theDocName, repeats when finished:\"False\"}

    set name of first activity of document theDocName to \"QS\"
    set base unit of first activity of document theDocName to minute
    set numeric duration of first activity of document theDocName to theText
    set action of finishing cue of first activity of document theDocName to \"Run Script\"
    set parameter of finishing cue of first activity of document theDocName to \"~/Library/Scripts/Applications/FlexTime/QSFTNotify.scpt\" as POSIX file

    set running of document theDocName to \"true\"
    end tell
    end process text
    end using terms from

    That script assumes you have another script saved in your ~/Library/Scripts/Applications/FlexTime folder, named QSFTNotify.scpt, to run when the timer finishes. Here's the one I'm using:

    using terms from application \"FlexTime\"
    on HandleFlexTimeCue(myDocument, myActivity)
    set routineName to name of myDocument
    set myText to routineName & \" finished .\"
    set SoundFile to \"~/Documents/Multimedia/PFTime.aif\" as POSIX file
    tell application \"Play Sound\"
    open SoundFile
    end tell
    tell application \"FlexTime\"
    display message myText dismissing after delay 5
    close myDocument saving no
    end tell
    end HandleFlexTimeCue
    end using terms from

    This script, in turn, assumes (1) that you have the freeware app Play Sound, which does what its name implies; and (2) that you have a sound file at ~/Documents/Multimedia/PFTime.aif . This can obviously be changed to whatever sound file you want to play.

    These scripts would obviously benefit from some error-checking (e.g., a handler in case the text is in the wrong format), but it's a nice little start. Thanks, Daniel, for making such a nice scriptable timer application to make this possible.
  • I've played with this a bit more, and FlexTime seems to hang or crash whenever the Quicksilver-induced routine document is the only one open. FT crashes when it closes the window. I've tried setting a delay before closing the window, but FT still hangs. I have a crash log if it'd be useful, Daniel.
  • Interesting - thanks. I guess these features haven't been so fully exercised until now :) I'm glad you are finding some creative uses for the scriptability, but sorry to hear that there are some crashing bugs.

    I'm going to look into these ASAP and try to get an update out that will address the crashes/hangs.

    Definitely email me a copy of the crash log if that's convenient.

    Daniel
Start a New Discussion

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!