如何设置 xdotool 和 gedit 打开文档并保存两次?

Uri*_*era 6 gedit scripts xdotool

我需要帮助来创建执行以下操作的脚本:

  1. 打开 Gedit
  2. 使用 Gedit 打开文档
  3. 保存文档两次
  4. 关闭 Gedit

我想知道如何使用 Gedit 和 xdotool 实现它,因为 xdotool 可以模拟组合键来保存文档Ctrl+S

ish*_*ish 6

窗口必须聚焦,否则gedit将忽略xdotool发送的 X 事件。

你的脚本应该是这样的:

#!/bin/bash

gedit -s /path/to/document
# -s 是独立的,这意味着将打开一个新窗口而不是任何现有 gedit 中的选项卡

sleep 5 # 等待 gedit 打开,必要时调整

GEDPID=$!# 保存gedit进程的PID

sleep 2 # 等待 gedit 实际打开文档,必要时进行调整

GEDWINID=`xdotool 搜索 --pid $GEDPID | 尾-1`
# 获取gedit的窗口ID,第三个“子”是实际窗口

xdotool windowactivate --sync $GEDWINID key --clearmodifiers --delay 100 ctrl+s ctrl+s alt+F4
# 聚焦 gedit 窗口,等待聚焦,然后保存两次并退出