2016-09-07 2 views
1

전면에 가져올 새 Chrome 인스턴스 (아래 스크립트 참조)를 어떻게 시작할 수 있는지 궁금합니다. 현재 셸 스크립트는 백그라운드에서 새 Chrome 인스턴스를 엽니 다.이 인스턴스는 최적 상태가 아닙니다. Applescript에서 쉘 스크립트를 실행해도이 문제를 해결할 수 없습니다.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" 

쉘 스크립트

:

재미있는 것은 내가 애플 스크립트에서 직접 쉘 명령어를 사용하여 크롬을 열 경우 전경에서 열 것이다

#!/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="[email protected]" 

# 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" 
+0

의 명령은 앞에 해당 응용 프로그램을 제공합니다 '응용 프로그램 "XXX"를 말할 활성화'마지막 명령의 프로세스 ID를 얻을 수 있습니다. – pbell

+0

알지만 Chrome 인스턴스가 여러 개 있습니다. 마지막 하나를 어떻게 제어하나요? 피디? – oligofren

+0

'응용 프로그램 "XXX"의 마지막 창에 활성화를 알리십시오 -이 방법은 작동하지 않지만 시작해야합니다. 나는 그것을 확인하고 작동하게 만들었지 만 나는 지금 창문에있다. –

답변

1

명령을 백그라운드에서 실행한다. (명령어 끝 부분에 &을 넣는다).

사용 $!

경우 애플
# 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' 
+0

괜찮았다. 불만은 applescript의 쉘 명령 (Finder에서 실행할 수 있음)에서 스크립트를 시작할 때 한 번에 둘 이상의 인스턴스를 시작할 수 없다는 것입니다. 이를 위해서는 명령 줄에서 스크립트를 실행해야합니다. 그러나 그것은 좋은 한계입니다. 일반적으로 디버깅을 위해 하나의 신선한 인스턴스가 필요합니다. – oligofren

1

아래 스크립트에서 AppleScript와 Shell 명령을 혼합하여 사용했습니다. 나는 Shell 전문가가 아니므로 가장 효율적인 방법이있을 수 있습니다. 적어도,이 스크립트가 작동된다

1) 그것은 모든 발견 과정을 통해가는 특정 이름을 포함하는 모든 과정 (예 = 크롬)

2) 소요, 그리고 그것을 사용하기 시작하기 때문에 각각의 시간을 얻을 "ps"셸 명령.

3) 이전 시간과 이전 시간을 비교하여 낮 으면 프로세스 정보를 유지합니다. 최저 시간 값은 프로세스의 마지막 시작 인스턴스에 링크됩니다.

4) 시작 이후 가장 짧은 시간을 가진 프로세스가 마지막 프로세스입니다. frontmost 속성을 true로 설정하여 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 

"ps"명령에 대해 분명히하기 위해 몇 가지 주석을 달았습니다. 누구든지 ps 출력에서 ​​두 번째로 직접 시간을 얻는 방법을 알고 있다면 감사합니다. (나는 가장 쉬운 방법이 있어야한다고 확신한다.)

+0

열심히 노력해 주셔서 감사합니다. 그러나 'do shell script "$ CHROME_CMD &>/dev/null & echo $!"; 결과에 chrome_pid를 설정하십시오' ' – oligofren

관련 문제