AutomatorでGoogle Chromeのタブのテキストをクリップボードにコピーしたい

Automatorにおいて下記の動作をするシェルスクリプトの記述をお教え願えないでしょうか?


  1. (Google Chromeブラウザがアクティブな状態で)Chromeのアクティブなタブのタイトルを変数theTitleに取得する
  2. そのタイトルをクリップボードにコピーする
  3. クリップボードの中身をダイアログに表示する


環境

  • MacBook Pro 13-inch 2020 / OS v. 12.1 (Monterey)
  • Automator v. 2.10 (512)


ネットの様々な記述を参考に試みたのですがうまくいきませんでした。どうかよろしくお願い申し上げます。

MacBook Pro (2020 and later)

投稿日 2022/01/31 08:24

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

投稿日 2022/01/31 17:40

とりあえず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}"


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

2022/01/31 17:40 poyo176 への返信

とりあえず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}"


2022/01/31 19:22 poyo176 への返信

A案は AppleScript ですからロケール環境変数が存在しないので不要です。(入出力共に UTF-8)


B案はロケール環境変数がセットされていなくても文字化けしないので設定しませんでした。(Terminal とは挙動が異なるようです)


C案も基本的にはB案と同じですが、pbcopy はロケール環境変数がセットされていないと期待どおりに動かないので環境変数を付けました。(詳細は man pbcopy の Encoding: のところに書いてあります)


2022/01/31 18:26 Hiro__S への返信

すべての案が正常に動作いたしました(なおB・CはAutomatorでChromeを制御する許可が必要でした)。

ありがとうございます。


もう一つお教えいただければと思います。

C案のみ、文字コード関係(?)の工程(10行目)があるようなのですが、

これがA・B案にないのはなぜなのでしょうか?

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

AutomatorでGoogle Chromeのタブのテキストをクリップボードにコピーしたい

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