とりあえず3案考えてみました。シェルスクリプトではなく AppleScript で良ければA案がシンプルで良いと思います。動作確認は Mojave 10.14.6 ですが、Monterey でも大丈夫だと思います。多分...。
当方の環境
- Mojave 10.14.6 (Parallels Desktop 15)
- Google Chrome 97.0.4692.99
- Automator 2.9
ーーーーー
A案
- 「シェルスクリプトを実行」アクションではなく「AppleScriptを実行」アクションを使用
on run
tell application "Google Chrome"
activate
# 1. Google Chrome のアクティブなタブのタイトルを変数 theTitle に格納
set theTitle to window 1's active tab's title
# 2. タイトルをクリップボードにコピー
set the clipboard to theTitle
# 3. クリップボードの中身をダイアログに表示
display dialog theTitle
end tell
return theTitle
end run
ーーーーー
B案
- シェルスクリプトを実行アクションを使用 (bash か zsh)
- A案をそのままラッピング
theTitle=$(osascript <<'EOF'
on run
tell application "Google Chrome"
activate
set theTitle to window 1's active tab's title
set the clipboard to theTitle
display dialog theTitle
end tell
return theTitle
end run
EOF
)
echo "${theTitle}"
ーーーーー
C案
- シェルスクリプトを実行アクションを使用 (bash か zsh)
theTitle=$(osascript <<'EOF'
on run
tell application "Google Chrome"
return window 1's active tab's name
end tell
end run
EOF
)
LANG=en_US.UTF-8 pbcopy <<< "${theTitle}"
osascript <<'EOF' - "${theTitle}"
on run (argv)
tell application "Google Chrome"
activate
display dialog (argv's item 1 as text)
end tell
return
end run
EOF
echo "${theTitle}"