AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1394019
Accepted
Lexible
Lexible
Asked: 2022-02-21 12:32:14 +0800 CST2022-02-21 12:32:14 +0800 CST 2022-02-21 12:32:14 +0800 CST

从 bash 中打开已启动的 Xorg 应用程序中的文档

  • 772

有没有办法告诉已经运行的 x 程序从 bash 打开文件?(即不调用程序的新实例。)当我在 的上下文中提出这个问题时xstata-mp,我更感兴趣的是这种解决方案是否普遍存在于Xorg 应用程序中。

我有一个运行xstata-mp良好的 x 应用程序(专有的,对这个问题很重要)。我设法制作了一个很好的启动脚本,它首先检查是否xstata-mp已经在运行,如果是,将它带到前台,否则启动它。很像这个问题的公认答案。

我的问题是,有时我想打开一个由(例如,数据文件、脚本文件、帮助文件等)使用的文档。如果我双击此类文件的图标,或者在已经运行时选择该图标并点击,则启动脚本被调用(它在文件的部分中被引用)并被提升到可见窗口的顶部,但是无需打开文档。xstata-mp.dta.do.sthlp<ENTER>xstata-mpexecxstata-mp.desktopxstata-mp

对于它的价值,如果我在其中打开多个文档(例如,使用),检查pidof它似乎xstata-mp 不会启动新进程;与多个标签/站点进行对比。xstata-mp<CTRL>-OFirefox

这是我要修改的启动脚本:

# 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
bash
  • 2 2 个回答
  • 60 Views

2 个回答

  • Voted
  1. mark
    2022-02-21T14:13:50+08:002022-02-21T14:13:50+08:00

    首先,您应该找出打开文档的正确命令。所以尝试运行以下命令,看看你得到了什么。

    /usr/local/stata17/xstata-mp --help
    

    当然,这是关键部分。在这里,您完全依赖程序的功能。如果它不提供该选项,那么您就不能这样做,因为 bash 所能做的就是启动另一个程序。从我从StataCorp LLC下载的 399 页的 9.4 MB 用户手册中,没有一个关于如何使用的提示xstata-mp。

    也许该-q选项已经是您想要的。但是让我们假设将另一个文档添加到已经运行的实例中的正确选项xstata-mp是-a(如在 VS Code 中:)code -a。然后,您所要做的就是在您的else声明下方添加以下行:

    /usr/local/stata17/xstata-mp -a "$1"
    

    它会打开您的文档并立即显示当前窗口。

    • 1
  2. Best Answer
    Lexible
    2022-02-24T16:15:21+08:002022-02-24T16:15:21+08:00

    是的:有一种方法可以告诉已经运行的 x 程序从 bash 中打开文件!

    因此,根据 StataCorp 技术支持人员的提示,事实证明(如果您的系统上尚未xdotool安装,则可以使用 安装)为我的问题中的那种问题提供了解决方案。sudo apt install xdotool来自项目网站:

    xdotool让您模拟键盘输入和鼠标活动、移动和调整窗口大小等。它使用 X11 的 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
    

    关于这个简短脚本的一些评论:

    1. 路径可能不是/usr/bin/xdotool,因此请务必确认 withwhich xdotool或类似。
    2. 第二行将部分匹配名称"Stata/MP 17.0"与上面提供的字符串。这很有用,例如,将版本升级到 17.1 或 18.x 不会破坏脚本。该search命令标识了我要与之交互的 X 应用程序窗口。
    3. xdotool脚本接受参数,遵循bash-like $1、$2等约定。
    4. 该type命令按字面意思输入提供的文本——在我的例子中,在第2 行的命令标识的第一个(仅在我的例子中)窗口中提供的两个字符串参数$1的内容。$2search
    5. 该key命令向同一个 Stata 窗口发送<ENTER>(或者<RETURN>如果您愿意)。

    现在让我们看看我修改后的启动脚本,它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
      fi
    

    注意:这是在 Xorg 上运行的 Gnome 的解决方案。

    • 0

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve