【问题标题】:Bring Chrome started from script to front将 Chrome 从脚本启动到前面
【发布时间】:2016-09-07 21:49:53
【问题描述】:

我想知道如何启动一个全新的 Chrome 实例(请参阅下面的脚本),它将被带到前面。目前,shell 脚本在后台打开新的 Chrome 实例,这不是最佳的。从 Applescript 执行 shell 脚本并不能解决这个问题。

有趣的是,如果我直接从 AppleScript 使用 shell 命令打开 Chrome,它似乎会在前台打开:

set q to "'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' --user-data-dir=/tmp/1234"
do shell script q

Applescript

do shell script "~/bin/chrome-fresh"

Shell 脚本

#!/bin/sh
# This is quite useful for front-enders, as it will launch a fresh
# Chrome instance with no loaded plugins or extensions that messes
# with your performance profiling or network debugging
#
# Install:
#       install -m 555 ~/Downloads/chrome-fresh  /usr/local/bin/

CHROME="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
ARGS="$@"

# make a fresh user directory
TMP_USERDIR=$(mktemp -d);

# avoid the dialog on the first startup
touch "$TMP_USERDIR/First Run";

# start chrome using a fresh user directory
"$CHROME"   --user-data-dir="$TMP_USERDIR"  "$ARGS"

【问题讨论】:

  • 在 Applescript 中,“告诉应用程序“xxx”激活”命令将该应用程序置于前面。
  • 我知道,但是有几个 Chrome 实例。最后一个怎么控制? PID?
  • tell the last window of application "XXX" to activate - 这不起作用,但应该让你开始。我会检查它并让它工作,但我现在在 Windows 上。

标签: macos shell google-chrome applescript


【解决方案1】:

在后台运行命令(将&放在命令末尾)。

使用$!获取最后一条命令的进程ID

# start chrome using a fresh user directory
"$CHROME" --activate-on-launch --user-data-dir="$TMP_USERDIR" "$ARGS" &
chromePid=$!
sleep 2
# bring Chrome 
osascript -e 'tell application "System Events"' -e "tell (first process whose its unix id is \"$chromePid\" ) to set frontmost to true" -e 'end tell'

【讨论】:

  • 没关系。唯一的不满是,当从 applescript 中的 shell 命令启动该脚本时(能够从 Finder 运行它),它一次无法启动多个实例。为此,我需要从命令行运行脚本。但这是一个可以的限制。我通常只需要一个新的实例来调试东西。
【解决方案2】:

在下面的脚本中,我混合使用了 Applescript 和 Shell 命令。我不是壳牌专家,所以可能有最有效的方法。至少,这个脚本是有效的:

1) 它需要包含特定名称的所有进程(即 = Chrome)

2) 它遍历所有找到的进程,并为每个进程获取自它开始使用“ps”shell 命令以来的时间。

3) 它将该时间与找到的先前时间进行比较,如果较低,则保留进程信息。最低时间值与流程的最后一个启动实例相关联。

4) 自启动以来时间最短的进程是最后一个:它将最前面的属性设置为 true 以使其成为前台。

tell application "System Events"
set lastTime to 3600 -- max possible value of start time
set lastPID to -1 -- impossible : used to check if process has been found !
set Prlist to every process whose name contains "Chrome"
repeat with aProc in Prlist
    set PStext to do shell script "PS -o etime -p " & unix id of aProc -- get the start time of the process
    -- output is dd-hh:mm:ss if process has been stared few days ago
    -- output is hh:mm:ss if process has been stared few hours ago
    -- output is mm:ss if process has been stared few minutes or seconds ago
    -- assumption is made that it just started few seconds ago
    -- convert in seconds = mm*60 + ss
    set runningTime to ((word 1 of paragraph 2 of PStext) as integer) * 60 + (word 2 of paragraph 2 of PStext) as integer
    if runningTime < lastTime then
        set lastTime to runningTime
        set lastPID to unix id of aProc
        set MyProc to aProc
    end if
end repeat
if lastPID > 0 then -- if a process has been found
    set the frontmost of MyProc to true -- set it in foreground
end if
end tell

我做了几个cmets来明确“ps”命令。如果有人知道如何从 ps 输出中直接获取时间,谢谢。 (我很确定应该有最简单的方法!)

【讨论】:

  • 感谢您的辛勤工作,但您本可以让自己变得更轻松 :-) do shell script "$CHROME_CMD &amp;&gt; /dev/null &amp; echo $!";set chrome_pid to the result `
猜你喜欢
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
  • 2012-10-17
相关资源
最近更新 更多