Search code examples
applescriptbbedittextexpander

Why is the system beeping when I use AppleScript to set text in BBEdit from TextExpander?


I've written an AppleScript that is designed to fire when TextExpander notices a keystroke. It works fine when run from the AppleScript Editor but under certain conditions it also beeps when run from TextExpander.

Here's the script:

tell front window of application "BBEdit"
    if (length of selection) is not 0 then
        add prefix and suffix of selection prefix "[" suffix "]"
    else
        set text of selection to "["
    end if
end tell

It's set to fire when I type the [ character, with the idea that if text is selected it will wrap the text in [ and ] but if no text is selected then it should simply type the [ character as normal.

It works perfectly however it's run, but if run from TextExpander and the "else" path is follow (set text of selection to "[") the system beeps. I'm not sure if BBEdit or TextExpander is generating the beep, but there's no beep if I completely remove the "else" section or if it runs with text selected (the "if" path).


Solution

  • The folks at Smile Software (makers of TextExpander) found a perfectly workable solution for me.

    Instead of

    set text of selection to "["
    

    (which had to be followed by select insertion point after selection in order to deselect the [ anyway), this works perfectly:

    return "["
    

    which is a better idea anyway.