Search code examples
performanceapplescriptkeystrokes

Increase speed of applescript keystroke


I'm using applescript to automate some browser activity. I have it tied to the speech recognition, so that I can make some custom voice commands.

One such command is "I wanna go home", which when heard, pulls up the relevant bus schedule on my browser. Given that the public transit system uses PHP and GET, the URL gets pretty long. Therefore, the keystroke myURL in the applescript takes a while to execute (something like 1-2 seconds). While I can live with losing 1-2 seconds, I'd really rather not.

With that in mind, is it possible to send these keystrokes faster? I believe I read somewhere that using key code is faster than using keystroke, but for the life of me, I can't figure out where I read that.

EDIT: My browser of choice is Google Chrome, which I couldn't find URL hooks for. This is why I had to resort to using keystrokes. Therefore, I'd prefer answers that work with Chrome over Safari


Solution

  • Open location is defined in Standard Additions and should not be enclosed in a tell statement.

    open location "http://google.com"
    

    If you want to target a specific browser you can use:

    tell application "Safari"
        if not (exists document 1) then reopen
        set URL of document 1 to "http://google.com"
    end tell
    

    To target Chrome:

    tell application "Google Chrome"
        if not (exists window 1) then reopen
        set URL of active tab of window 1 to "https://www.google.com/"
    end tell