LoginSignup
8
5

More than 5 years have passed since last update.

Safari のページタイトルとURLをクリップボードにコピーする AppleScript

Last updated at Posted at 2016-05-26

Sarari のURLとタイトルをコピー

Safari で表示しているページをメモしたりするときに、ページのタイトルとURLをセットで使いたいですよね。

 手動だと、ページタイトルを選択してコピー、それを何処かに貼り付けておいて、URLを選択してコピーして、貼り付けるという手順になって結構面倒。

 なので、だいぶ前に AppleScript でこんなコードを書いて、最近まで使っていました。

URLcopy.scpt
tell application "Safari"
    set thisURL to URL in front document
    set thisTitle to do JavaScript "document.title" in front document
    set the clipboard to thisTitle & return & thisURL
end tell

Safari のバージョンアップでエラーが出る!

 ところが、最新の Safari では JavaScriptの実行が制限されて、エラーで動かなくなってしまった。(「開発」メニューの「Apple Events からの JavaScript を許可」を選択すると実行するようにはなる)

 ということで、AppleScript の Safari拡張である do JavaScript を使わないバージョンに変更したのがこちら。

URLcopy2.scpt
tell application "Safari"
    set thisURL to URL in front document
    set thisSource to source in front document
    ignoring case
        set titleBegin to the offset of "<TITLE>" in thisSource
        set titleEnd to the offset of "</TITLE>" in thisSource
    end ignoring
    set thisTitle to characters (titleBegin + 7) thru (titleEnd - 1) of thisSource as string
    set the clipboard to thisTitle & return & thisURL
end tell

HTML ソースから title タグを探しているので、HTMLの書き方によっては上手く行かないかもしれませんが、ま、大体は動くということで。

ちなみに、こんなブックマークレットでも実現できるのだが...

URLcopy.js
javascript:prompt('Title%20%28Return%29%20URL',document.title+'%20%3d%3d%3e%20'+location.href);

ダイアログが表示されたら編集メニューでコピーするとタイトルとURLがクリップボードにコピーされます。(prompt では改行コードが入れられなかったので、代わりに「 ==> 」で区切るようにしました)

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5