检测剪贴板复制/粘贴事件并修改剪贴板内容

Sid*_*Man 9 bash scripts clipboard xclip xsel

将某些内容复制到剪贴板(使用 ctrl+c)后,我想要一个脚本(bash、python 或任何其他语言)自动检测新条目已添加到剪贴板,更改其内容并将其放回剪贴板,以便在粘贴时我得到修改后的文本。该脚本应不断在后台运行并监视剪贴板的变化。

以下脚本描述了所需的修改:
来源:https : //superuser.com/questions/796292/is-there-an-efficient-way-to-copy-text-from-a-pdf-without-the-换行

#!/bin/bash

# title: copy_without_linebreaks
# author: Glutanimate (github.com/glutanimate)
# license: MIT license

# Parses currently selected text and removes 
# newlines that aren't preceded by a full stop

SelectedText="$(xsel)"

ModifiedText="$(echo "$SelectedText" | \
    sed 's/\.$/.|/g' | sed 's/^\s*$/|/g' | tr '\n' ' ' | tr '|' '\n')"

#   - first sed command: replace end-of-line full stops with '|' delimiter and keep original periods.
#   - second sed command: replace empty lines with same delimiter (e.g.
#     to separate text headings from text)
#   - subsequent tr commands: remove existing newlines; replace delimiter with
#     newlines
# This is less than elegant but it works.

echo "$ModifiedText" | xsel -bi
Run Code Online (Sandbox Code Playgroud)

我不想使用快捷键绑定来运行脚本。

Sid*_*Man 8

幸得肯恩

我根据我的要求修改了脚本,并添加了检测剪贴板复制事件并修改其内容的功能。

来源

从 PDF 复制文本时删除换行符 (Linux)

这个 bash 脚本在从 PDF 复制文本时删除换行符。它适用于 Linux 的主要选择和剪贴板。

#!/bin/bash

# title: copy_without_linebreaks
# author: Glutanimate (github.com/glutanimate)
# modifier: Siddharth (github.com/SidMan2001)
# license: MIT license

# Parses currently selected text and removes 
# newlines

while ./clipnotify;
do
  SelectedText="$(xsel)"
  CopiedText="$(xsel -b)"
  if [[ $SelectedText != *"file:///"* ]]; then
    ModifiedTextPrimary="$(echo "$SelectedText" | tr -s '\n' ' ')"
    echo -n "$ModifiedTextPrimary" | xsel -i
  fi
  if [[ $CopiedText != *"file:///"* ]]; then
    ModifiedTextClipboard="$(echo "$CopiedText" | tr -s '\n' ' '  )"
    echo -n "$ModifiedTextClipboard" | xsel -bi
  fi
done
Run Code Online (Sandbox Code Playgroud)

依赖关系

  1. 塞尔sudo apt install xsel
  2. 剪辑通知。您可以使用存储库中提供的预编译的 clipnotify 或自己编译。
    自己编译clipnotify
    sudo apt install git build-essential libx11-dev libxtst-dev
    git clone https://github.com/cdown/clipnotify.git
    cd clipnotify
    sudo make
    
    Run Code Online (Sandbox Code Playgroud)

使用

  1. 以 zip 格式下载此存储库,或将脚本复制并粘贴到文本编辑器中,并将其另存为 copy_without_linebreaks.sh。
  2. 确保脚本和剪辑通知(下载或预编译)在同一文件夹中。
  3. 在脚本文件夹中打开终端并设置权限
    chmod +x "copy_without_linebreaks.sh"
  4. 双击脚本或在终端中输入运行:
    .\copy_without_linebreaks.sh
  5. 复制 pdf 中的文本并将其粘贴到任何位置。换行符将被删除。