简体   繁体   中英

How do I copy and paste the URL from Chrome using Applescript?

I've searched high and low and cannot find the answer to this.

How do I copy and paste the URL from Chrome into Excel?

Essentially, I have a project at work where I have to copy and paste values into a search engine. I have to save the URL of the results from that search. I don't know how to do this using Applescript. This is the code I have so far. I know it's not elegant, but I'd appreciate any help!


tell application "Microsoft Excel" to activate

tell application "System Events"

tell process "Microsoft Excel"
    keystroke "c" using command down
end tell

end tell

delay 0.4

tell application "Google Chrome" to activate

tell application "System Events"

tell process "Google Chrome"
    keystroke "a" using command down
    keystroke "v" using command down
end tell
delay 0.5
tell application "System Events"
    tell process "Google Chrome"
        keystroke return
    end tell
end tell
delay 5.0
tell application "Google Chrome"

    get URL of active tab of first window as text

end tell
delay 5.0
tell application "System Events" to keystroke "w" using command down

end tell

tell application "Microsoft Excel" to activate

tell application "System Events"

tell process "Microsoft Excel"


    keystroke tab
    keystroke "v" using command down
    keystroke down
    keystroke left
end tell

end tell

the script itself seems a little bit quirky, because you use a lot of unnecessary UI scripting. This said the solution (for your script) is, that you copy the url and paste it into the browser. After a while you want to read the url and paste it into the cell next to the initial url. You forgot to copy the url before that step. If you paste using keystroke "v" using command down there is only the initial url stored in the clipboard.

If you want the script working in that way you have to change the line

get URL of active tab of first window as text

to

set the clipboard to (URL of active tab of first window as text)

But: I strongly recommend to recreate your script using the full possibilities (aka dictionaries) of the targeted applications! You can reach your aim using vanilla applescript only!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM