Is有一种方法可以告诉已经在运行的x程序从bash?打开文件(即不调用程序的新实例)。当我在xstata-mp上下文中问这个问题时,我更感兴趣的是这种解决方案是否存在in通用的用于Xorg应用程序。
我有一个x-应用程序(xstata-mp,专有的,这是突出的问题,正如你将要学习的),它运行很好。我已经成功地制作了一个很好的启动脚本,它首先检查xstata-mp是否已经运行,如果已经运行,则将它带到前台,否则启动它。很像对这个问题的公认的答案。
我的问题是,有时我希望打开xstata-mp使用的文档(例如,.dta数据文件、.do脚本文件、.sthlp帮助文件等)。如果我双击这样一个文件的图标,或者选择图标并在已经运行时点击xstata-mp,启动脚本就会被调用(它在xstata-mp's .desktop文件的exec部分中引用),xstata-mp被提升到可见窗口的顶部,但没有打开文档。
无论是否值得,如果我在pidof中打开多个文档(例如,使用-O),则xstata-mp does not将启动一个新的xstata-mp进程;与Firefox和多个选项卡/站点形成对比。
下面是我想修改的启动脚本:
# Check if xstata-mp v17 is running
exit_code_pidof_xstata_mp=$(pidof /usr/local/stata17/xstata-mp)
# if xstata-mp v17 IS NOT running, then launch it with argument $1
if [ -z "$exit_code_pidof_xstata_mp" ]
then
/usr/local/stata17/xstata-mp -q $1; exit >/dev/null
# but if xstata-mp v17 IS running, then bring it to front instead
else
wmctrl -ia "$(wmctrl -lp | grep "$(pgrep /usr/local/stata17/xstata-mp)" | tail -1 | awk '{ print $1 }')"; exit > /dev/null
fi发布于 2022-02-24 00:15:21
YES:有一种方法可以告诉已经在运行的x程序从bash!打开一个文件。
因此,根据StataCorp技术支持人员的提示,事实证明,xdotool (如果还没有安装在您的系统上,可以与sudo apt install xdotool一起安装)为我问题中的问题提供了一个解决方案。来自项目网站:
xdotool允许您模拟键盘输入和鼠标活动,移动和调整窗口大小等。它使用X11‘S XTEST扩展和其他Xlib函数来实现这一点。⚠️注意:如果您正在使用Wayland,请注意此软件将不能正常工作。使用xdotool,您可以搜索窗口并移动、调整大小、隐藏和修改窗口属性(如标题)。如果您的窗口管理器支持它,您可以使用xdotool切换桌面,在桌面之间移动窗口,并更改桌面的数量。
为了解决xdotool的问题,我需要创建一个xdotool脚本,我称之为statadoc.xdo:
#!/usr/bin/xdotool
search --name "Stata/MP"
type --window %1 '$1 ' '$2'
key --window %1 Return关于这个简短的脚本的几点评论:
/usr/bin/xdotool,所以一定要用which xdotool或类似的方法确认这一点。"Stata/MP 17.0"。这很有用,因此,例如,将版本升级到17.1或18.x不会破坏脚本。search命令标识我想要与之接口的X应用程序窗口。xdotool脚本接受参数,遵循bash-like $1、$2等约定。type命令逐字逐句地键入所提供的文本--在我的示例中,两个提供的$1和$2字符串参数的内容在第1窗口(在我的情况下仅限于)窗口中由第2行的search命令标识。key命令向相同的Stata窗口发送一个 (如果您愿意,也可以发送 )。现在让我们看看我修改过的启动脚本,它在第三行到最后一行调用了statadoc.xdo (我在前面添加了一个部分来识别提供的参数是否指示特定的Stata文件类型-需要不同的使用命令来优雅地处理每个…。我的示例尚未完成,但这些是我在大部分工作中使用的文件类型):
# Check whether there IS NOT an argument. If not, do nothing.
if [ -z "$1" ]
then
break
# Otherwise, set prefix to default value, and then check
# whether file name ends in .hlp, .sthlp, .ado, .do, or .gph
else
prefix="use "
if [ ${1##*.} = "hlp" ] || [ ${1##*.} = "sthlp" ] || [ ${1##*.} = "ado" ] || [ ${1##*.} = "do" ]
then
# If the filename DOES end in one of those four prefixes then
# change prefix to "doedit" so Stata opens the document with
# the do-file editor.
prefix="doedit "
fi
# If the filename ends in .gph, then change prefix to "doedit" so
# Stata opens the document with the graph viewer/editor.
if [ ${1##*.} = "gph" ]
then
prefix="graph use "
fi
break
fi
# Check if xstata-mp v17 is running
exit_code_pidof_xstata_mp=$(pidof /usr/local/stata17/xstata-mp)
# if xstata-mp v17 IS NOT running, then launch it with argument $1
if [ -z "$exit_code_pidof_xstata_mp" ]
then
/usr/local/stata17/xstata-mp -q $1; exit >/dev/null
# but if xstata-mp v17 IS running, then bring it to front instead
else
# First, call statadoc.xdo with the prefix as the first argument,
# and the supplied file path as the second argument
/usr/share/stata17/bin/statadoc.xdo "$prefix" " $1"
wmctrl -ia "$(wmctrl -lp | grep "$(pgrep /usr/local/stata17/xstata-mp)" | tail -1 | awk '{ print $1 }')"; exit > /dev/null
fiNote:这是运行在Xorg上的Gnome的解决方案。
发布于 2022-02-20 22:13:50
首先,您应该找到打开文档的正确命令。因此,尝试运行以下命令并查看您得到了什么。
/usr/local/stata17/xstata-mp --help当然,这是关键的部分。在这里,您完全依赖于程序的功能。如果它没有提供这个选项,那么您就不能这样做,because一个bash所能做的就是启动另一个程序。从我从斯塔塔科公司有限责任公司下载的带有399页的9.4MB用户手册中,没有一个关于如何使用xstata-mp的提示。
也许-q选项已经是您想要的了。但是,让我们假设将另一个文档添加到已经运行的xstata-mp实例的正确选项是-a (类似于VS代码:code -a)。然后,您要做的就是在else语句下面添加以下一行:
/usr/local/stata17/xstata-mp -a "$1"它将打开您的文档并显示当前窗口。
https://askubuntu.com/questions/1394019
复制相似问题