Bash 程序在第二台显示器上打开文件

Ada*_*dam 3 command-line bash scripts multiple-monitors

我用 bash 编写了一个小脚本,它打开一个文件,然后五分钟后关闭它。它不断重复这个过程。

问题是,无论鼠标指向哪里,程序都会打开,我希望它在我的第二台显示器上打开,这是查看文件的完美尺寸。

我曾尝试通过以下方式(在命令行)export DISPLAY=:0.1 使用显示变量。但是在此之后我从 CLI 调用的任何程序都会返回错误。(错误:无法打开显示:0.1)

有人有建议吗?

编辑:

这是每五分钟打开一个名为 thesis.pdf 的文件的脚本

#! /bin/bash

while true; do
    evince /home/adam/Desktop/Thesis.pdf &
    sleep 5m
    ps -ef | grep "Thesis.pdf" | awk '{print $2}' | xargs kill
done

exit 0
Run Code Online (Sandbox Code Playgroud)

ter*_*don 5

要定位窗口,您可以使用操作 X 事件的工具,例如xdotoolwmctrl。例如,使用wmctrl,您可以使用-e

   -e <MVARG>
          Resize  and  move  a  window  that  has been specified with a -r
          action according to the <MVARG> argument.
   <MVARG>
          A move and resize argument has the format 'g,x,y,w,h'.  All five
          components are integers. The first value, g, is the  gravity  of
          the  window,  with  0  being  the most common value (the default
          value for the window). Please see  the  EWMH  specification  for
          other values.

          The four remaining values are a standard geometry specification:
          x,y is the position of the top left corner of  the  window,  and
          w,h  is  the  width and height of the window, with the exception
          that the value of -1 in any position is interpreted to mean that
          the current geometry value should not be modified.
Run Code Online (Sandbox Code Playgroud)

您通常可以忽略重力,因此要在屏幕的左上角放置一个窗口并使其为 1200 x 700 像素,您可以运行:

wmctrl -r :ACTIVE: -e 1,1,1,1200,700
Run Code Online (Sandbox Code Playgroud)

-r让你选择一个窗口和:ACTIVE:手段目前集中的窗口。

您还可以简化您的脚本。没有理由解析ps,特殊变量$!保存了最近放置在后台的作业的 PID。无论如何,解析ps通常会失败,因为可能有多个进程匹配Thesis.pdf. 总会有两个:那个evincegrep Thesis.pdf你刚刚跑过的。

因此,考虑到所有这些,您可以执行以下操作:

#! /bin/bash
while true; do
    ## Open the pdf
    evince ~/doc/a.pdf &
    ## Save the PID of evince
    pid="$!"
    ## Wait for a 1.5 seconds. This is to give the window time to
    ## appear. Change it to a higher value if your system is slower. 
    sleep 1.5
    ## Get the X name of the evince window
    name=$(wmctrl -lp | awk -vpid="$pid" '$3==pid{print $1}')
    ## Position the window
    wmctrl -ir "$name" -e 1,1,1,1200,700
    ## Wait
    sleep 5m
    ## Close it
    kill "$pid"
done
Run Code Online (Sandbox Code Playgroud)

请注意,我删除了exit 0因为您的while true, 永远不会到达并且没有意义。您可以使用位置参数来确定要放置窗口的位置。

最后,关于DISPLAY. 此变量指向 X 显示。这不是屏幕,它是活动的 X 服务器。许多用户可能在一台机器上运行并行的 X 服务器,这允许您选择窗口应该显示在其中的哪一个上。它与连接的物理屏幕数量完全无关,除非每个屏幕都在运行单独的 X 会话。