将选定的文本发送到命令行参数

发布于 2024-08-13 21:43:18 字数 507 浏览 4 评论 0原文

我发现了这个实用程序 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扎心 2024-08-20 21:43:18
#!/bin/bash
pytranslate "$(xsel -p)"

现在只需将其放入 ~/bin 中(确保它包含在您的 PATH 中),然后运行它。 (您可能需要安装 xsel 包。)它将获取主选择缓冲区的当前内容并将其传递给 pytranslate。

如果您希望将其作为按钮,请创建一个在终端中运行它的启动器,并使用 bash 的读取命令来执行“按 ENTER 继续”。

#!/bin/bash
pytranslate "$(xsel -p)"

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".

红衣飘飘貌似仙 2024-08-20 21:43:18

从罗杰·佩特 (Roger Pate) 精彩的一行中汲取灵感,我为 pytranslate 创建了一个简单的循环脚本。目前这是临时的 - 因为我还没有实现错误捕获 - 等待新的编辑。

#!/bin/bash
# Primary Clipboard poller using xsel (middle click) and pytranslate
# Checks for changes every 1 second
# If change has occured, a command is executed (pytranslate here)
########## Information ########## 
# It now stores definitions in a text file - saves bandwith and reduces hits on google (caseless)
# Works for Romance languagse
#TODO
# Character based langauges
# Catch errors

if [ ! -e "pytranslatecache" ]; then
touch pytranslatecache
fi

while [ 1 ]
do
   OLDENTRY="$(xsel -p)"
   sleep 1
   NEWENTRY="$(xsel -p)"
   if [ "$NEWENTRY" != "$OLDENTRY" ] ; then
     if [ "$(grep -F -o -i "$NEWENTRY" pytranslatecache)" = "$NEWENTRY" ] ; then
    echo "From Cache:"
        echo "$(grep -i "$NEWENTRY" pytranslatecache)" 
     else
    DEFINITION=""$(pytranslate -s fr "$(xsel -p)")""
        echo "$NEWENTRY"":"$DEFINITION
        echo "$NEWENTRY"":"$DEFINITION >> pytranslatecache
     fi
   fi
# Catch Errors - Commands
   if [ $? != 0 ]; then
   {
       echo "Failed to translate string."
    } fi
done

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.

#!/bin/bash
# Primary Clipboard poller using xsel (middle click) and pytranslate
# Checks for changes every 1 second
# If change has occured, a command is executed (pytranslate here)
########## Information ########## 
# It now stores definitions in a text file - saves bandwith and reduces hits on google (caseless)
# Works for Romance languagse
#TODO
# Character based langauges
# Catch errors

if [ ! -e "pytranslatecache" ]; then
touch pytranslatecache
fi

while [ 1 ]
do
   OLDENTRY="$(xsel -p)"
   sleep 1
   NEWENTRY="$(xsel -p)"
   if [ "$NEWENTRY" != "$OLDENTRY" ] ; then
     if [ "$(grep -F -o -i "$NEWENTRY" pytranslatecache)" = "$NEWENTRY" ] ; then
    echo "From Cache:"
        echo "$(grep -i "$NEWENTRY" pytranslatecache)" 
     else
    DEFINITION=""$(pytranslate -s fr "$(xsel -p)")""
        echo "$NEWENTRY"":"$DEFINITION
        echo "$NEWENTRY"":"$DEFINITION >> pytranslatecache
     fi
   fi
# Catch Errors - Commands
   if [ $? != 0 ]; then
   {
       echo "Failed to translate string."
    } fi
done
偏闹i 2024-08-20 21:43:18

您能否在 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.

我们只是彼此的过ke 2024-08-20 21:43:18

注意:这个答案对于提问者来说毫无用处,因为他没有使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文