문제

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

도움이 되었습니까?

해결책 3

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

다른 팁

Another way to solve this is to put the text in the clipboard, then paste it. You can save the clipboard contents first and put it back afterward, so you don't lose what's already there. This method works in other situations when you want to enter a lot of text, not just for browser URLs like the other answers.

    set clipboard_contents to (the clipboard)
    set the clipboard to "Some long string that would take a while with the keystroke method"
    tell application "System Events" to keystroke "v" using command down
    delay 0.2 -- needed because otherwise the next command can run before the paste occurs
    set the clipboard to clipboard_contents

I'm pretty sure you can script your browser to open the URL directly, instead of typing in the keystrokes (faking input events in AppleScript should be considered a last resort).

You can do this with the open location standard addition:

open location "http://google.com"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top