将选定的文本发送到命令行参数
我发现了这个实用程序 pytranslate,它可以使用谷歌的翻译API。它的工作原理与描述的完全一样。
然而,我已经厌倦了选择一个我不理解的单词,然后在命令中单击鼠标中键。命令格式是这样的:
pytranslate WORD
是否有一个程序/脚本能够检测我何时用鼠标选择一个单词或一系列单词,然后用所选文本代替 WORD 中的位置执行上述命令终端窗口?
示例:
所选文本:
Bonjour mondiale...
结果命令/结果:
pytranslate Bonjour mondiale
Detected source language: fr
Hello World
I found this utility, pytranslate, which translates a variety of languages into each other using Google's translation API. It works exactly as described.
However I've gotten sick of selecting a word I do not understand, then middle-clicking it into the command. The command format is as such:
pytranslate WORD
Is there a program/script that has the ability to detect when I select a word, or a series of words with my mouse, and then executes the above command with the selected text in the place of WORD in a terminal window?
Example:
Selected text:
Bonjour mondiale...
Resultant command/result:
pytranslate Bonjour mondiale
Detected source language: fr
Hello World
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
现在只需将其放入
~/bin
中(确保它包含在您的 PATH 中),然后运行它。 (您可能需要安装 xsel 包。)它将获取主选择缓冲区的当前内容并将其传递给 pytranslate。如果您希望将其作为按钮,请创建一个在终端中运行它的启动器,并使用 bash 的读取命令来执行“按 ENTER 继续”。
Now just put this in
~/bin
(make sure that's included in your PATH), and run it. (You may need to install the xsel package.) It will take the current contents of the primary selection buffer and pass it to pytranslate.If you want it as a button, create a launcher which runs this in a terminal, and use bash's read command to do "Press ENTER to continue".
从罗杰·佩特 (Roger Pate) 精彩的一行中汲取灵感,我为 pytranslate 创建了一个简单的循环脚本。目前这是临时的 - 因为我还没有实现错误捕获 - 等待新的编辑。
Taking inspiration from Roger Pate's brilliant one liner I've created a simple looping script for pytranslate. This is currently provisional - as I haven't implemented error catching yet - wait for new edits.
您能否在 PyGTK 包中使用剪贴板支持做这份工作?它声称可以访问“主”X 剪贴板,它表示您通常可以在其中找到所选文本。
Would you be able to use clipboard support in the PyGTK package to do the job? It claims to have access to the "primary" X clipboard which it says is where you'd normally find the selected text.
注意:这个答案对于提问者来说毫无用处,因为他没有使用 Windows。鉴于标题没有指定操作系统,我将把它留给可能采用这种方式的 Windows 用户。
您可以使用 pywin32 包和 win32clipboard 模块轻松地自己制作一个。例如,请参阅此问题。
我过去曾使用一个例程来完成此操作,该例程只是定期轮询剪贴板,每隔几秒钟左右,每当发现更改时,它就会抓取内容并对其执行某些操作。在您的情况下,使用 subprocess 包调用文本 pytranslate 。
Note: this answer was useless to the questioner, who wasn't using Windows. Given that the title doesn't specify the OS, I'll leave it around for Windows users who may come this way.
You can easily whip one up yourself using the pywin32 package and the win32clipboard module. See, for example, this question.
I've done this in the past with a routine that just polled the clipboard periodically, every few seconds or so, and whenever it found a change it grabbed the contents and did something with it. In your case, use the subprocess package to call out to pytranslate with the text.