0

如何在 AppleScript 中将任何应用程序的选定文本放入 Safari 到谷歌?

这是我想要做的示例代码:

tell application "System Events" to set selectedText to get selection
     tell application "Safari"
         open location "https://www.google.com/search?q=" & selectedText
         activate
     end tell
end tell
4

1 回答 1

0

过去,我为类似的任务编写了脚本。您可能还更喜欢这种方法:

tell application "Safari"
    open location "https://www.google.com/search?q=" & selectedText
    activate
end tell

------------------------------- HANDLERS ------------------------------------

on getSelectedText(appName)
    
    tell application appName to activate -- bring application to front
    
    -- get selected text, if it is selected
    set the clipboard to "" 
    tell application "System Events" to keystroke "c" using command down
    set {aSelection, maxTimeOut} to {"", 0}
    repeat while aSelection is ""
        set maxTimeOut to maxTimeOut + 0.1
        if maxTimeOut > 1 then error "NOTHING SELECTED."
        set aSelection to (the clipboard) as text
    end repeat
    
    return aSelection

end getSelectedText

on encode_URL(txt)
    set python_script to ¬
        "import sys, urllib; print urllib.quote(sys.argv[1])"
    set python_script to "/usr/bin/python -c " & ¬
        quoted form of python_script & " " & ¬
        quoted form of txt
    return do shell script python_script
end encode_URL
于 2021-11-07T12:44:25.997 回答