Chromeプロファイルを複数作成、起動をしAppleScriptでwindowの位置サイズ指定

macos10.14.5環境でChromeを複数起動を利用しています。


(以下を使用しプロファイルを複数作成し起動)

do shell script "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --user-data-dir=/Users/$USER/Library/Application\\ Support/Google/me01 > /dev/null 2>&1 &"


AppleScriptを利用し、サイズや位置を指定したいのですが、いくつか起動したChromeの指定する方法がわからず悩んでます。

これを解くことが出来る方いらっしゃりませんか?


色々と調べてみたんですがあまり詳しくないものでしてご教授頂けると助かります。

どうぞ宜しくお願い致します。


▼現状下記

tell application "System Events"

set app_name to name of first item of (every application process that (frontmost is true))

tell process "Google Chrome"

set win3 to (1st item of (every window that subrole is "AXStandardWindow"))

set position of win3 to {20, 60}

set size of win3 to {700, 800}

end tell

end tell


Mac Pro

投稿日 2019/06/25 23:11

返信
スレッドに付いたマーク ランキングトップの返信

投稿日 2019/06/26 01:02

こんな感じでどうでしょうか。なお、当方は Google Chrome を使ってないので Chromium でテストしました。


tell application "System Events"
    set ids to {}
    set win_size to {600, 800}
    set {p1, p2} to {0, 22}

    repeat with proc in processes
        if name of proc is "Google Chrome" then
            set end of ids to id of proc
        end if
    end repeat

    set n to length of ids
    repeat while n > 0
        tell process id (item n of ids as integer)
            set size of item 1 of (windows whose subrole is "AXStandardWindow") to win_size
            set position of item 1 of (windows whose subrole is "AXStandardWindow") to {p1, p2}
        end tell
        set {p1, p2} to {p1 + 20, p2 + 20}
        set n to n - 1
    end repeat
end tell


返信: 56
スレッドに付いたマーク ランキングトップの返信

2019/06/26 01:02 Piman への返信

こんな感じでどうでしょうか。なお、当方は Google Chrome を使ってないので Chromium でテストしました。


tell application "System Events"
    set ids to {}
    set win_size to {600, 800}
    set {p1, p2} to {0, 22}

    repeat with proc in processes
        if name of proc is "Google Chrome" then
            set end of ids to id of proc
        end if
    end repeat

    set n to length of ids
    repeat while n > 0
        tell process id (item n of ids as integer)
            set size of item 1 of (windows whose subrole is "AXStandardWindow") to win_size
            set position of item 1 of (windows whose subrole is "AXStandardWindow") to {p1, p2}
        end tell
        set {p1, p2} to {p1 + 20, p2 + 20}
        set n to n - 1
    end repeat
end tell


2019/06/26 06:45 Piman への返信

C1、C2 というのがよく分かなかったので、同一 ID でウインドウが2枚と考えました。ウインドウのサイズと位置は適宜変更してください。あと、do s**** script で Google Chrome を複数起動した後、長めに待機 (delay) した方が良いかもしれません。


tell application "System Events"
    set ids to {}

    repeat with proc in processes
        if name of proc is "Google Chrome" then
            set end of ids to id of proc
        end if
    end repeat

    # A
    tell process id (item 1 of ids as integer)
        -- set frontmost to true
        tell item 1 of (windows whose subrole is "AXStandardWindow")
            set size to {500, 800}
            set position to {0, 22}
        end tell
    end tell

    # B
    tell process id (item 2 of ids as integer)
        --set frontmost to true
    end tell

    # C
    tell process id (item 3 of ids as integer)
        --set frontmost to true

        # C1
        tell item 1 of (windows whose subrole is "AXStandardWindow")
            set size to {500, 400}
            set position to {500, 22}
        end tell

        # C2
        tell item 2 of (windows whose subrole is "AXStandardWindow")
            set size to {500, 400}
            set position to {500, 422}
        end tell
    end tell

    # D
    tell process id (item 4 of ids as integer)
        --set frontmost to true
        tell item 1 of (windows whose subrole is "AXStandardWindow")
            set size to {500, 400}
            set position to {1000, 22}
        end tell
    end tell

    # E
    tell process id (item 5 of ids as integer)
        --set frontmost to true
    end tell
end tell


追記: Chromium を Google Chrome に修正しました。


2019/06/26 20:08 Hiro__S への返信

以下は図説にも貼りましたものになります。


tell application "System Events"

set ids to {}


repeat with proc in processes

--set frontmost to true

if name of proc is "Google Chrome" then

set end of ids to id of proc

end if

end repeat


# A

tell process id (item 1 of ids as integer)

--set frontmost to true

tell item 1 of (windows whose subrole is "AXStandardWindow")

set size to {900, 2000}

set position to {6000, -160}

end tell

end tell


# B

tell process id (item 2 of ids as integer)

--set frontmost to true

tell item 1 of (windows whose subrole is "AXStandardWindow")

set size to {1500, 1000}

set position to {2000, 25}

end tell

end tell


# C1

tell process id (item 3 of ids as integer)

--set frontmost to true

tell item 1 of (windows whose subrole is "AXStandardWindow")

set size to {1273, 560}

set position to {273, 30}

end tell

end tell


# E

tell process id (item 4 of ids as integer)

--set frontmost to true

tell item 1 of (windows whose subrole is "AXStandardWindow")

set size to {680, 1447}

set position to {15, 1400}

end tell

end tell


# C2

tell process id (item 4 of ids as integer)

--set frontmost to true

tell item 2 of (windows whose subrole is "AXStandardWindow")

set size to {680, 800}

set position to {15, 595}

end tell

end tell


end tell

2019/06/28 17:43 mNeji への返信

冷静に見直しました。何重にもミスを重ねてしまいました、ごめんなさい。


正解は次のコマンド(雛形)です;

  • ps -U u1 | grep '/Application Support/Google/cr1' | head -1 | awk '{print $1}'


ここで、

  • psの「u1」は、ご自分のアカウント名です。
  • grepのパラメータ「/Application Support/Google/cr1」は、AppleScriptで生成したchromeのプロファイルです。


上記の結果を「Activity Monitor」の結果と比較してくだされば、一致すると思います。もしお作りの複数プロファイルに基づいて生成された複数のchromeを開いた上で、上記スクリプトが正しく表示できるでしょうか?


もし上手くいけば、開いた複数のchromeとpidが紐づいたことになりそうです。


2019/06/28 03:39 mNeji への返信

今回は頭を解す為に、別プロファイルで立ち上がるcromeアプリを立ち上げた時に、そのプロセスID(pid) を入手する流れを試してみました。


ターミナルでの作業となります;

昨日作ったcromeアプリを確認:ls -ld /Applications/googleScript1.app

それを開く:open -a googleScript1

このアプリ名のpidを含む1行を入手:ps -u your-account-name | grep your-chrome.app


実際のターミナル画面は下図;


2019/06/30 09:58 Piman への返信

プロファイルとそれに対応するアクティブPODとの対応付けをするスクリプトを作りました、下図;



「Google Chrome」本体を使う場合はコマンド・エイリアスの「cr」によりオープンする必要があります。


他のプロファイルを開けるコマンド・エイリアスの「cr0, cr1, cr2, 」などには制限はありません。



今回の「activeChromeProfie2Pid 」というスクリプトは2種類のコマンドを用いました;

  • ps -axc | grep 'Google Chrome$'
  • ps -U u1 | grep "Application Support/Google/$profile_dir" | head -1 | awk '{print $1}'


恐らく、AppleScriptで対応スクリプトを作っていただくと良いと思います。チェックには「Activity Monitor」と突き合わせで、確認しました。


位置サイズの設定にはコマンド・エイリアスの「cr, cr0, cr1, cr2 」に入れ込む形式を取ろうと思っています。

2019/07/02 00:55 Piman への返信

> Chromeを複数起動する理由


なるほど。事情は理解できました。


> 状況


ご説明ありがとうございます。最前面ウインドウを期待通りに捕まえることができてないような印象ですね...。ダイアログなしの A案 (.scpt) もダメですか?


あと、スクリプトエディタでスクリプトファイルを開いておいて以下のようにすると、スクリプトエディタが背面のままスクリプトを走らせることができます。


  1. Google Chrome を複数起動
  2. 任意のウインドウをクリック (最前面にする)
  3. スクリプトエディタが背面にある状態で「実行」ボタンを「コマンド+クリック」


で、スクリプトの内容をひとつずつ確認すれば何か分かるかもしれません。


# 急ぎませんので、時間があるときにでも試してもらえればと。


2019/06/26 02:15 Hiro__S への返信

ありがとうございます。

レスポンスも早く助かります。


試してまして、可動を確認出来ました。

ただ、私の方でもう少し状況を丁寧に説明するべきでした。

すみません。。


一応参考にまとめました。

MACを毎日立ち上げた際に5つのChromeを下記のように指定し配置したいと考えてます。

尚私の使用してるモニタが**でかいので数値はあくまで参考程度で見て頂ければ。。

もしご都合含め可能でしたら宜しくお願い致します。


ChromeA 位置{6000,-160} サイズ{900, 2000} 

ChromeC1 位置{0, 570} サイズ{700, 850}

ChromeC2 位置{25, 350} サイズ{1200, 560}

ChromeD 位置{0, 1243} サイズ{700, 1550}


ChromeB は位置サイズを指定しない

ChromeE は位置サイズを指定しない

※Chrome C1、C2は起動するとそれぞれ指定のページが開くように設定されています。


2019/06/26 03:36 T22T への返信

改めて、ありがとうございます。

早速ためしてみまして、window2以降でエラーが出てしまいました。

ただ、私の方でもう少し行いたい状況を丁寧に説明するべきでした。

すみません。


一応参考にまとめました。

MACを毎日立ち上げた際に5つのChromeを下記のように指定し配置したいと考えてます。

尚私の使用してるモニタがでかいので数値はあくまで参考程度で見て頂ければ。。

もし可能でしたらお願い致します。


ChromeA 位置{6000,-160} サイズ{900, 2000} 

ChromeC1 位置{0, 570} サイズ{700, 850}

ChromeC2 位置{25, 350} サイズ{1200, 560}

ChromeD 位置{0, 1243} サイズ{700, 1550}


ChromeB は位置サイズを指定しない

ChromeE は位置サイズを指定しない

※Chrome C1、C2は起動するとそれぞれ指定のページが開くように設定されています。

2019/06/26 04:20 Piman への返信

度々、失礼致します。


私はGoogle Chromeをあまり使っていないので、よく理解していないのですが、


1)Google Chromeのアプリは、複数起動することができるのでしょうか? ChromeA, B, C, D, E とあるのですが、これは複数のChromeが起動しているという状況でしょうか? それとも、アプリの機動は一つで、表示されるWindowが、A, B, C, D, Eとある、ということでしょうか?

2019/06/26 05:18 T22T への返信

あ、ありがとうございます!

以下返信です。

1)→ Chromeは本来は複数起動は出来ない仕様です。ですが上記のdo s**** script "/Ap...を利用し、複数のプロファイルを作成することで裏技的に使える状況です。

ですので複数同時に立ち上げ、ユーザー同期等もそれぞれ設定可能です。5つが個別のアプリ状態ですね。


結構難易度が高い質問になってしまっているかとも思いますので、あまり無理な事にお付合いさせるのも恐縮ですので、もしわかりそうでしたらお願い頂ければ助かります。


2019/06/26 08:24 Piman への返信

Pimanさん、


横から失礼します。こちらは常用ブラウザを「Google Chrome.app」に替えるように準備中です。


で、構想:

  • CLIからスッピンのChrome(A)を立ち上げて検索をさせ、
  • その検索結果をデータ処理の上、別のCLIから興味深いタイトルを選択して、スッピンのChrome(B)を立ち上げてそのタイトルを表示させたい。


もしも、お書きになった「 s**** script」が公開可能であったらご教授お願いできませんか?


急ぎませんので、このトピックが目処が付いた後にでもお答えくだされば、幸いです。

2019/06/26 18:27 mNeji への返信

ありがとうございます。


***は、このサイトで自動で変換されるようですね。

おそらく、中傷言葉なんかも規制で変換されてしまうようです。

下記を貼ってみます。こちらを見れましたら、おそらくわかるかと思います。

http://yakinikunotare.boo.jp/orebase2/browser/google_chrome/google_chrome_for_mac_multi_profile

宜しくお願い致します。

2019/06/26 20:05 Hiro__S への返信

昨晩はありがとうございました。

その後、色々と検証させて頂きました。

頂いたものをエラーが発生しない状況まで色々と試し、5つのウインドウが指定の位置への配置まで辿り尽きました。

ただ、やはりわかった事は、ChromeA~Eそれぞれに紐付け出来る要素がないと理想の処理が出来ないのではと思いました。


状況を文章で書いても伝わり辛いかと思いまして、図説にしてみました。

https://pbs.twimg.com/media/D-CRmCuUEAEVz5f.jpg:orig

もし図に示したエラー(★印の数字をC1とどう連動させるか)の解決法がわかるようでしたら、伺えると助かります。

わかり辛い点ありましたら補足もさせて頂きます。


(A~Eの順番とC1とC2も逆になる事もありますが、それはさすがにそこまで対応できる方法は難しいのかもなぁ、、、と思えてきましたので最悪上記の問題が解決出来ればよいかなと思っております。)

このスレッドはシステム、またはAppleコミュニティチームによってロックされました。 問題解決の参考になる情報であれば、どの投稿にでも投票いただけます。またコミュニティで他の回答を検索することもできます。

Chromeプロファイルを複数作成、起動をしAppleScriptでwindowの位置サイズ指定

Apple サポートコミュニティへようこそ
Apple ユーザ同士でお使いの製品について助け合うフォーラムです。Apple Account を使ってご参加ください。