oligofren
oligofren

Reputation: 22923

Bring Chrome started from script to front

I am wondering how I can launch a fresh new Chrome instance (see my script below) that will be brought to the front. Currently the shell script opens the new Chrome instance in the background, which is less than optimal. Executing the shell script from Applescript does nothing to remedy this.

The interesting thing is that if I open Chrome using a shell command directly from AppleScript it seems to open in the foreground:

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 script

#!/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"

Upvotes: 1

Views: 838

Answers (2)

jackjr300
jackjr300

Reputation: 7191

Run the command in background (put the & at the end of the command).

Use $! to get the process ID of the last command

# 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'

Upvotes: 2

pbell
pbell

Reputation: 3095

In the script bellow, I used a mix of Applescript and Shell commands. I am not Shell expert, so may be there are most efficient way to do it. At least, this script is working :

1) it takes all process containing specific name (i.e. = Chrome)

2) it goes through all found processes, and for each, get the time since it starts using "ps" shell command.

3) it compares that time with previous times found and if lower then it keeps the process information. The lowest time value is linked to the last starting instance of the process.

4) the process with the shortest time since it starts is the last one : it sets the frontmost property to true to make it foreground.

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

I made several comments to make it clear about the "ps" command. If anyone knows how to get directly time in second from ps output, thanks. (I am quite sure there should be an easiest way !)

Upvotes: 1

Related Questions