激活程序的命令(运行或如果已运行则置于最前面)

激活程序的命令(运行或如果已运行则置于最前面)

我希望能够有一个键盘快捷键来启动或将程序置于最前面。(如果重要的话,使用 sqldeveloper)

我知道如何运行它,剩下:

1)如何知道某个程序是否已在运行?

2)如何将该程序置于其他打开的窗口的顶部?

~~ 编辑 ~~

感谢大家到目前为止的回复。当我运行上述任一命令来获取正在运行的进程列表时,我得到了以下信息:

doneill   3492     1  0 Nov16 ?        00:00:00 /bin/sh /usr/local/bin/sqldeveloper
doneill   3493  3492  0 Nov16 ?        00:00:00 /bin/bash /opt/sqldeveloper/sqldeveloper.sh
doneill   3495  3493  0 Nov16 ?        00:00:00 bash sqldeveloper
doneill   3552  3495  1 Nov16 ?        00:25:07 /usr/lib/jvm/java-6-sun-1.6.0.22/bin/java -Xmx640M -Xms128M -Xverify:none -Doracle.ide.util.AddinPolicyUtils.OVERRIDE_FLAG=true -Dsun.java2d.ddoffscreen=false -Dwindows.shell.font.languages= -XX:MaxPermSize=128M -Dide.AssertTracingDisabled=true -Doracle.ide.util.AddinPolicyUtils.OVERRIDE_FLAG=true -Djava.util.logging.config.file=logging.conf -Dsqldev.debug=false -Dide.conf="/opt/sqldeveloper/sqldeveloper/bin/sqldeveloper.conf" -Dide.startingcwd="/opt/sqldeveloper/sqldeveloper/bin" -classpath ../../ide/lib/ide-boot.jar oracle.ide.boot.Launcher

我确实通过 .sh 脚本启动了它。它是一个基于 Java 的程序。我对其中哪一个感兴趣?我尝试使用它xdotool来提升它,但我还没有成功,尽管今晚我没有时间深入研究手册页。

如果我运行xdotool search Orac(“Oracle SQL Developer”是窗口标题的开头),它会返回一堆数字。其中一个代表我感兴趣的内容吗?

答案1

一点点打击和工具应该可以解决问题。文章末尾有安装 xdotool 的说明。

ActivateOrLaunch.sh

#!/usr/bin/env bash

# usage:
#    ActivateOrLaunch.sh firefox-bin
#
# Will launch firefox-bin or activate the first window in the windows stack
# --- Remember to chmod a+x ActivateOrLaunch.sh in order to execute.

# pgrep looks through the currently running processes and lists the process
#       IDs which matches the selection criteria to stdout
#       for more information on pgrep http://linux.die.net/man/1/pgrep

# if process is found by pgrep then pipe through head to get firt instance in
#    case of multiple processes running. If process not found $pid will be empty
pid=$(pgrep ${1} | head -n 1)

# Using "-z" check if $pid is empty, if so execute the process
if [ -z "$pid" ]; then
   echo "$1 not running... executing..."
   $1 &
else
   # process was found, find the first visible window and activate it
   echo "Found process $1 with PID $pid"

   # using xdotool [http://www.semicomplete.com/projects/xdotool/] get the first
   # visible windows using $pid. Redirect stderr to /dev/null and only select
   # the first visible windows using head
   wid=$(xdotool search --onlyvisible --pid $pid 2>/dev/null | head -n 1)

   # if $wid is empty the process does not have any visible windows... do nothing
   if [ -z "$wid" ]; then
     echo "Didn't find any visible windows for process $1 with PID: $pid"
   else
     # send the window id ($wid) from the window stack to xdotool
     echo "Activating first windows in stack"
     xdotool windowactivate $wid
   fi
fi

# ******** NOTES **********
# In order for this script to work correctly you need to pass the complete process 
# name for it to find any visible windows. The process needs needs to be in the path
# for it to execute if not running.
#
# For example
#
# If you try it with firefox-bin on Ubuntu 10.10 it will find the running process and 
# activate the window, but it will not be able to launch the process since the executable 
# in the path is called firefox which is a link to firefox.sh
# (/usr/bin/firefox -> ../lib/firefox-3.6.12/firefox.sh) which in turn executes firefox-bin
# (not in path).
#
# Next, if you use firefox it will find a process but won't be able to find any windows 
# since it's a wrapper function calling firefox-bin and doesn't have any windows. 

您可以在以下位置找到代码github

我在 Ubuntu 10.10 (Gnome) 上测试了代码,它运行正常。它需要一些润色,因为我刚刚输入它,并不担心它是否漂亮(想确保得到注释)

要安装 xdotool,您可以apt-get 安装 xdotool(从我这里获取版本 2.20100701.2691),如果你想要最新最好的版本,请从这里(截至 20101116 其版本为 2.20101012.3049-4)

答案2

在主目录中创建一个名为 sql-raise.sh 的文件

/bin/sh #!/bin/sh 复制代码
如果 ps -ef | grep 进程名称 | grep -v grep ; 那么
    #该进程正在运行,将其置于最前面
    xdotool search --name 进程名称 windowraise
    出口 0

#该进程未运行,启动它
进程名称

该行的开头if ps -ef...翻译为:

  • 列出所有正在运行的进程
  • 过滤掉不包含process-name
  • 过滤掉grep命令本身,以防止误报

起始行xdotool是将流程带到最前面的行。

在命令行上执行chmod +x ~/sql-raise.sh以使文件可执行。

要将其绑定到某个键,请使用系统 -> 偏好设置 -> 键盘快捷键

答案3

如果 Firefox 未运行,则“launchorsitch.sh firefox”会启动它;如果 Firefox 未处于活动状态,则切换到它;如果 Firefox 是活动窗口,则将其最小化。

#!/bin/bash
# usage: "launchorswitch.sh xterm" to change to/start/hide xterm
app=$1
app_win_id=`wmctrl -lx|grep -i $app|cut -d ' ' -f 1`
case $app in
    terminator)
        app_exec="terminator --geometry=1000x720+140+28"
    ;;
    *)
        app_exec=$app
    ;;
esac
if [ -z $app_win_id ]; then
    $app_exec & # app not started, so start it
else

    active_win_id=`wmctrl -r :ACTIVE: -e 0,-1,-1,-1,-1 -v 2>&1|grep U|cut -d ' ' -f 3`
    if [ $app_win_id == $active_win_id ]; then
        wmctrl -r :ACTIVE: -b toggle,hidden    # hide app when active
    else
        wmctrl -i -a $app_win_id    #switch to app
    fi;
fi;

开头有针对程序的特殊规则(这里是终止符)。我的 hack 使用 wmctrl 并允许指定要查找/运行的可执行文件的特殊规则。欢迎对风格、明显错误等提出任何意见。

相关内容