Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I press the shortcut key and use Google to search for the selected content? #2535

Closed
GanZhiXiong opened this issue Dec 9, 2020 · 31 comments

Comments

@GanZhiXiong
Copy link
Contributor

No description provided.

@MuhammedZakir
Copy link

MuhammedZakir commented Dec 9, 2020

You will have to use a script for that and call it from shell_command. You can use any language for that. A sample shell script:

#!/bin/zsh

searchTerm="$(pbpaste)"

open "https://www.google.com/search?q=$searchTerm"

To use this, put two events in to - command+v (copy text) and shell_command: /path/to/script (execute script). See pqrs-org/KE-complex_modifications#697. It may help you.

Or use Hammerspoon (or QuickSilver, Alfred, BetterTouchTool, etc.). If you want a lot of customization, or already use one, this will be the better option.


To do this without any script, the hardest part is opening browser and inputting selected text (or google search link). The problem is that depending on CPU usage, time taken for opening browser will vary, but we can only mention a fixed time in KE. A 3-5 seconds delay is close enough, but even that maybe slow. If you want to give it a try, this can be done using multiple to events:

  • copy selected text
  • open browser
  • open new tab (in case browser is already open)
  • wait for some time (to_delayed_action)
    • input search link
    • past text
    • press enter

@GanZhiXiong
Copy link
Contributor Author

@MuhammedZakir
Thank you! 👍
Through your guidance, I have achieved it!😀

{
    "from": {
        "modifiers": {
            "optional": [
                "any"
            ]
        },
        "simultaneous": [{
                "key_code": "i"
            },
            {
                "key_code": "o"
            }
        ]
    },
    "to": [{
        "key_code": "c",
        "modifiers": [
            "left_command"
        ]
    }],
    "to_delayed_action": {
        "to_if_invoked": [{
            "shell_command": "open https://www.google.com/search?q=$(pbpaste)"
        }]
    },
    "parameters": {
        "basic.to_delayed_action_delay_milliseconds": 100
    },
    "type": "basic"
}

@MuhammedZakir
Copy link

Nice! :-) I guess I made it more complicated.

A suggestion: I would also use another "pboard" to save current clipboard content and then after opening link, copy the saved content back to main clipboard. See pqrs-org/KE-complex_modifications#697 (comment):

  1. Before executing command+v, execute shell command - pbcopy | pbpaste -pboard ruler
  2. Then after opening URL, execute - pbpaste -pboard ruler | pbcopy

@yu-soong
Copy link

yu-soong commented Sep 7, 2021

@MuhammedZakir
Thank you! 👍
Through your guidance, I have achieved it!😀

{
    "from": {
        "modifiers": {
            "optional": [
                "any"
            ]
        },
        "simultaneous": [{
                "key_code": "i"
            },
            {
                "key_code": "o"
            }
        ]
    },
    "to": [{
        "key_code": "c",
        "modifiers": [
            "left_command"
        ]
    }],
    "to_delayed_action": {
        "to_if_invoked": [{
            "shell_command": "open https://www.google.com/search?q=$(pbpaste)"
        }]
    },
    "parameters": {
        "basic.to_delayed_action_delay_milliseconds": 100
    },
    "type": "basic"
}

It only works for English characters, not for words containing special characters such as å or Chineses characters. Don't know if there is a solution on it. Thanks

@MuhammedZakir
Copy link

MuhammedZakir commented Sep 7, 2021

It only works for English characters, not for words containing special characters such as å or Chineses characters. Don't know if there is a solution on it. Thanks

I think you will have to encode the text before using them in the URL. Haven't tried it though.

E.g. using NodeJS

"shell_command": "open \"https://www.google.com/search?q=$(node -p \"encodeURIComponent($(pbpaste))\")\""

@GanZhiXiong
Copy link
Contributor Author

@yu-soong
I do not have this problem here, Chinese is also ok.

@yu-soong
Copy link

yu-soong commented Sep 8, 2021

@MuhammedZakir, Using NodeJS doesn't work neither. It only opens google but no word is pasted into the search bar.
@GanZhiXiong Interesting. Your OS language is Chinese or English? I am using English, don't know whether this is the reason.

@GanZhiXiong
Copy link
Contributor Author

GanZhiXiong commented Sep 8, 2021

@yu-soong
My system is in Chinese.
I think you might want to UTF-8 encode the clipboard contents.

@GanZhiXiong
Copy link
Contributor Author

@yu-soong
并且我的 to_delayed_action_delay_milliseconds 现在是设置 300,由于 100 太快,有时候会出现先搜索后复制,导致搜索的关键字还是上次剪贴板的内容。

"parameters": {
                    "basic.to_delayed_action_delay_milliseconds": 300
                },

@yu-soong
Copy link

yu-soong commented Sep 8, 2021

@GanZhiXiong
It is even worse when I change OS language from English to Chinese. Now when I press the shortcut on Chinese characters, it only copies the characters but doesn't use Google to search them: doesn't open a new tab in Safari.
But when I was using English OS language, it did search Chinese characters using Google (open a new tab in Safari), but in the search bar the Chinese characters were replaced by question marks "?".

@GanZhiXiong
Copy link
Contributor Author

@yu-soong

  • 我用的是 Chrome,没有用 Safari 试过,你可以用 Chrome 试下。
  • 升级KE。

@yu-soong
Copy link

yu-soong commented Sep 8, 2021

@GanZhiXiong
Chrome 上也不行。KE 刚刚升级过。
It doesn't work in Chrome neither.

@GanZhiXiong
Copy link
Contributor Author

那你直接在终端执行如下命令呢?

$ open 'https://www.google.com/search?q=abc123你好'

@yu-soong
Copy link

yu-soong commented Sep 8, 2021

@GanZhiXiong
The file /Users/username/https:/www.google.com/search?q=abc123你好 does not exist.

@GanZhiXiong
Copy link
Contributor Author

GanZhiXiong commented Sep 8, 2021

@yu-soong
怎么回是这样的提示,我这里执行该命令后,不管是 Chrome 还是 Safari,都能正确打开网页。

估计是你系统或 shell 的问题吧?
明明是链接,怎么会当文件打开呢。

@yu-soong
Copy link

yu-soong commented Sep 8, 2021

@GanZhiXiong
我也不知道怎么回事😂 系统是刚重新装的。用iTerm2和Terminal打开都是这样的。不知道是什么地方出了问题。貌似open没有认出来这个应该是链接而不是文件。

@GanZhiXiong
Copy link
Contributor Author

GanZhiXiong commented Sep 8, 2021

@yu-soong
我刚用一台 Big Sur 11.3.1 的电脑试了下,是有这个问题:

ganzhixiong@ganzhixiongdeMacBook-Pro-2 ~ % open 'https://www.google.com/search?q=abc123你好'
The file /Users/ganzhixiong/https:/www.google.com/search?q=abc123你好 does not exist.

通过参考这篇文章 https://developer.apple.com/forums/thread/663762 ,我找到了解决方法:对剪贴板内容进行 URL 编码即可。
你使用如下命令,应该是可以打开的

 open "https://www.google.com/search?q=$(python -c "import urllib, sys; print urllib.quote(sys.argv[1])"  "abc123你好")"

@yu-soong
Copy link

yu-soong commented Sep 8, 2021

@GanZhiXiong
我刚才试了一下,只有用 open 'http://www.google.com’ 才可以打开网页。
你这个也可以。
那么在karabiner中应该怎么写呢?
谢谢!

@GanZhiXiong
Copy link
Contributor Author

@yu-soong
只要包含中文必须要进行 URL 编码才能打开的,你用我的命令,对剪贴板的内容进行 URL 编码即可。

@yu-soong
Copy link

yu-soong commented Sep 8, 2021

@GanZhiXiong 用以下的还是中文用不了,显示的是问号。英语的可以。是不是我写的有问题?
"shell_command": "open https://www.google.com/search?q=$(python -c \"import urllib, sys; print urllib.quote(sys.argv[1])\" \"$(pbpaste)\")"

@GanZhiXiong
Copy link
Contributor Author

@yu-soong

open "https://www.google.com/search?q=$(python -c "import urllib, sys; print urllib.quote(sys.argv[1])"  "$(pbpaste )")" 

@yu-soong
Copy link

yu-soong commented Sep 8, 2021

@GanZhiXiong
还是不行。我觉得是系统的问题。放弃了 😂
不过还是谢谢你~

@GanZhiXiong
Copy link
Contributor Author

@yu-soong
你用我发的命令,应该是没问题的,我测试都是OK的。
其实你你现在只需要把这条命令放到KE中应该就可以了,你应该是遇到双引号转义的问题。

@yu-soong
Copy link

yu-soong commented Sep 8, 2021

@GanZhiXiong
不是转义符的问题。我放在terminal里用可以,但是在KE里就是不行。您看我上面那个应该是没有转义符问题的。

@GanZhiXiong
Copy link
Contributor Author

GanZhiXiong commented Sep 8, 2021

@yu-soong
没有尝试你的,你用我的试下,我的在我电脑上是没有问题的。

            {
                "from": {
                    "modifiers": {
                        "optional": [
                            "any"
                        ]
                    },
                    "simultaneous": [{
                            "key_code": "s"
                        },
                        {
                            "key_code": "d"
                        }
                    ]
                },
                "to": [{
                    "key_code": "c",
                    "modifiers": [
                        "left_command"
                    ]
                }],
                "to_delayed_action": {
                    "to_if_invoked": [{
                        "shell_command": "open \"https://www.google.com/search?q=$(python -c \"import urllib, sys; print urllib.quote(sys.argv[1])\"  \"$(pbpaste )\")\""
                    }]
                },
                "parameters": {
                    "basi": 300
                },
                "type": "basic"
            }

@yu-soong
Copy link

yu-soong commented Sep 8, 2021

@GanZhiXiong
还是不行 😂 比如“是OK的”会变成“?OK?” 不知道是不是我系统有什么问题。

@GanZhiXiong
Copy link
Contributor Author

@yu-soong
我在如下环境下测试也是OK的,你可以检测下软件版本:

  • Big Sur 11.3.1
  • KE 13.7.0
  • shell is zsh

@yu-soong
Copy link

yu-soong commented Sep 8, 2021

  • Big Sur 11.5.2
  • KE 13.7
  • shell zsh

@ibehnam
Copy link

ibehnam commented Aug 2, 2022

This doesn't work on my M1 Pro (Monterey).

@roachsinai
Copy link

@GanZhiXiong M1 mac os 11 not work. But run script in command line works well.

And I make an issue #3754 for more details.

Any suggestions is appreciated!

@roachsinai
Copy link

@GanZhiXiong fix it by set export LC_ALL=zh_CN.UTF-8; in shell_command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants