Shell-Script

如何在 Linux Mint 中為 Lightshot 創建 PrtSc(列印螢幕)快捷方式

  • January 2, 2020

我使用 Linux Mint 18.3 Cinnamon 64 位。我已經xdotool安裝了。

Lightshot自登錄以來一直在執行,我使用啟動應用程序工具在登錄後立即創建一個啟動:

env WINEPREFIX="/home/vlastimil/.lightshot" wine C:\\windows\\command\\start.exe /Unix /home/vlastimil/.lightshot/dosdevices/c:/users/Public/Start\ Menu/Programs/Lightshot/Lightshot.lnk

目標:

PrtSc(列印螢幕鍵)進入全屏剪切模式(事實上,與 Lightshot 在 Windows 下的功能相同)。


什麼有效(從 執行時gnome-terminal):

xdotool key --window $(xdotool search --limit 1 --all --pid $(pgrep Lightshot) --name Lightshot) "Print"

但是,如果將其複制粘貼到“添加自定義快捷方式”對話框中,它不會執行任何操作:

添加自定義快捷方式對話框

我只是想知道為什麼會這樣,更重要的是,如何讓它發揮作用?

2020 年 1 月 2 日更新

您可以在其 GitHub 頁面上找到最新的修訂使用說明


原帖

原因可能是,我沒有指定要執行的任何 shell,因此以下工作:

sh -c 'xdotool key --window $(xdotool search --limit 1 --all --pid $(pgrep Lightshot) --name Lightshot) "Print"'

編輯:

值得注意的是,我現在決定為它編寫一個腳本。充分驗證每一步:

#!/bin/sh

is_number()
{
   # check if exactly one argument has been passed
   test "$#" -eq 1 || print_error_and_exit 5 "is_number(): There has not been passed exactly one argument!"

   # check if the argument is an integer
   test "$1" -eq "$1" 2>/dev/null
}

# ------------------------------------------------------------------------------

print_error_and_exit()
{
   # check if exactly two arguments have been passed
   test "$#" -eq 2 || print_error_and_exit 3 "print_error_and_exit(): There have not been passed exactly two arguments!"

   # check if the first argument is a number
   is_number "$1" || print_error_and_exit 4 "print_error_and_exit(): The argument #1 is not a number!"

   # check if we have color support
   if [ -x /usr/bin/tput ] && tput setaf 1 > /dev/null 2>&1
   then
       bold=$(tput bold)
       red=$(tput setaf 1)
       nocolor=$(tput sgr0)
       echo "$bold$red$2 Exit code = $1.$nocolor" 1>&2
   else
       echo "$2 Exit code = $1." 1>&2
   fi

   exit "$1"
}

# ------------------------------------------------------------------------------

check_for_prerequisite()
{
   # check if exactly one argument has been passed
   test "$#" -eq 1 || print_error_and_exit 2 "check_for_prerequisite(): There has not been passed exactly one argument!"

   # check if the argument is a program which is installed
   command -v "$1" > /dev/null 2>&1 || print_error_and_exit 6 "check_for_prerequisite(): I require $1 but it's not installed :-("
}

# ------------------------------------------------------------------------------

# check if no arguments have been passed to the script
test "$#" -gt 0 && print_error_and_exit 1 "$0: You have passed some unexpected argument(s) to the script!"

# ------------------------------------------------------------------------------

# check for prerequisites
check_for_prerequisite "pgrep"
check_for_prerequisite "xdotool"

# ------------------------------------------------------------------------------

# global constants
lightshot_key="Print"
lightshot_name="Lightshot"

# ------------------------------------------------------------------------------

# get the lightshot pid
lightshot_pid=$(pgrep "$lightshot_name")

# test if a pid has been successfully acquired
is_number "$lightshot_pid" || print_error_and_exit 7 "lightshot_pid: The argument is not a number!\\nLightshot is most probably not running."

# ------------------------------------------------------------------------------

# get the window id from lightshot pid
lightshot_wnd=$(xdotool search --limit 1 --all --pid "$lightshot_pid" --name "$lightshot_name")

# test if a window handler has been successfully acquired
is_number "$lightshot_wnd" || print_error_and_exit 8 "lightshot_wnd: The argument is not a number!\\nLightshot is most probably not running."

# ------------------------------------------------------------------------------

# simulate a print screen key press on the lightshot window
xdotool key --window "$lightshot_wnd" "$lightshot_key"

引用自:https://unix.stackexchange.com/questions/421428