Script that cannot be set to a shortcut key, but runs OK otherwise
  • I am baffled. I'm trying to set up some simple AppleScripts to start/stop/backup audio clips being played in QuickTime, so I can use it when transcribing recorded conversations in TextEdit. There is no problem setting keyboard shortcuts via FastScripts or via other means (e.g. QuicKeys) for starting/stopping QuickTime while keeping focus on TextEdit.

    But scripts like this, where I attempt to paste in a time code along with a speaker name via a shortcut key combination, refuse to work. They run fine when run from the Scripts menu, or from Script Editor - but I just get a beep or various weird behaviors when run via a shortcut key. Doesn't matter what shortcut key combination I choose - it never works.

    Here is a sample script - here the letters "TL" that are part of what I want to keystroke just stand for the initials of a speaker's name. I have more complicated scripts that allow these to be read from a config file for convenience, but no use in trying them when even something simple like this doesn't work.

    I have also tried other ways of sending the keystrokes, but though this can be coded in slightly different ways, no variation has helped.




    --set printable current time of playback in document being played
    tell application \"QuickTime Player\"
    set thisDoc to document 1
    set thisNow to current time of thisDoc
    set thisSeconds to thisNow / 600 as integer
    set thisMinutes to thisSeconds / 60
    copy (AppleScript's text item delimiters) to old_delimiters
    set AppleScript's text item delimiters to \".\"
    set stringMinutes to thisMinutes as string
    set PrettyMinutes to text item 1 of stringMinutes
    set PrettySeconds to (thisSeconds - PrettyMinutes * 60)
    set prettyTime to PrettyMinutes & \":\" & PrettySeconds
    set AppleScript's text item delimiters to old_delimiters
    end tell

    set speakerInsert to \"TL\" & \" \" & prettyTime
    tell application \"System Events\"
    tell application \"TextEdit\" to activate
    keystroke speakerInsert
    keystroke return
    end tell
  • Oops, never mind - think I figured it out. Essentially the problem occurs because the shortcut key combination is being played at the same time (or nearly so) as the keystrokes in the script - and whatever the precise mechanism is, it leads to a roadblock, thus the weird behavior.

    Adding "delay .5" write before the call to System Events seems to create enough elbow room for the shortcut key to work properly.
  • This is an unfortunate, and common roadblock when using synthesized keystrokes in conjunction with a keyboard shortcut. It would be nice if Apple would provide some kind of "clear all keystrokes" script command or other means by which users of synthesized key events could convey, "No really, just pretend like only these keys were pressed."

    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!