Search code examples
macososascript

In OS X 10.9.2, command: osascript -e 'tell application "System Events" to keystroke "Honk" adds 'a' to end of all typed strings


Well, I mean, the title pretty much says it all. I'm using Python to pass

osascript -e 'tell application "System Events" to keystroke "Honk"'

to a 10.9 terminal. Using osascript to pass keystrokes to applications, and in 10.8 and below, this command was working perfectly. But now I'm using it in 10.9 to type into applications, and I get an 'a' added to every type.

For instance, my program starts crontab in insert mode:

~                                                                             
~                                                                               
~                                                                               
~                                                                               
-- INSERT --

But, after using the osascript command above, produces

Honka
~
~
~
~
-- INSERT --

This also occurs with commands like

osascript -e 'tell application "System Events" to keycode 52'

(Keycode 52 is the enter key, though strangely this doesn't seem to occur with keycode 53 (Escape key))

While I can pass backspaces through osascript, the extra 'a' is causing problems like starting commands, which can't simply be backspaced out of. How do I avoid, solve, or otherwise work around this problem? I've googled extensively, and can't find working alternative commands that work on 10.9.


Solution

  • Although I'm consistently getting this issue on multiple 10.9.2, I guess I'm either doing something wrong or it's not a wide issue yet.

    After lotsa trying, I've come to a workaround using code to write data into the clipboard, and then osascript to paste that in. I'm using python currently (in fact, I should've left python details in my question, as this is just a workaround). But the concept can be extended to other circumstances, I'm sure:

    def setClipboardData(data): #Copies data into clipboard, clearing old contents
        p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
        p.stdin.write(data)
        p.stdin.close()
        retcode = p.wait()
    
    def PasteType(data):
        setClipboardData(data)
        string = 'v'
        using = 'command'
        string = "\"" + string + "\""
        if using != '':
            string = string + ' using {'+using+' down}'
        system("osascript -e \'tell application \"System Events\" to keystroke " + string + "'")
    

    So simply, to type letters, you'll have to copy-paste into things use

    pbcopy <TEXT>
    osascript -e \'tell application \"System Events\" to keystroke "v" using {command down}'
    

    As an addition, I was having the same issue with

    osascript -e \'tell application \"System Events\" to key down (key code #)'
    

    to press keys like Esc and Enter, to solve this you simply have to make

    osascript -e \'tell application \"System Events\" to key up (key code #)'.
    

    For some reason this presses the key without making weird problems.