2012年10月30日火曜日

[Mac] Firefox, Chrome, Sleipnirで見ているページをSafariのリーディングリストに送るAppleScript

iPhoneでオフラインでもウェブページが見れるようにしたい。
つまり、Safariのリーディングリストを使いたい(もちろん他にもありますけど)。
でも日頃はFirefox、Chrome(Sleipnirも追加)を使っており、リーディングリストのためだけにSafariを立ち上げるのは面倒です。

少し調べてみると、以下のサイトがあったので参考にさせていただきました。ありがとうございます。
Chrome版は1つめのサイトで即完了。しかしFirefox用は2つ目も合わせてAppleScriptを作ったので備忘録として載せておきます。これをアプリケーションとして作っておけば、Alfredなどのランチャーを使ってさっと起動してリーディングリストに送れます。
しかし、FirefoxはApplescriptの点ではダメなブラウザですね…。なお、Firefox用AppleScriptの最後の行は、上記サイトのものではアプリケーション実行後にSafariが起動したままだったので追記しました。

2013年11月12日追記
  1. Firefox用スクリプトの一行目と後のgui_checkの部分は無くても動作することが分かりました。元々は必要だったみたいですが、MavericksにしたせいかFirefoxのバージョンが上がったせいなのか分かりませんがエラーが出ました。詳しいことは検証できていませんが、現在ではgui_check()がなくても問題なく動作しますのでコメントアウトしてあります。コピペして使用されるときはコメントアウト部分は削除していただいて大丈夫です。
  2. Sleipnir用スクリプトも追加しました。 
    ※たまに動作しません。。。が、とりあえず載せておきます。
    • with title "from Sleipnir"
      という部分はSleipnirで開いているタブのタイトルを取得する方法がわからなかったために苦し紛れに入れています。タイトルがないとリーディングリストに追加できないみたいなので。スクリプト実行後すぐにSafariでリーディングリストを確認すると、オフライン用に保存する前はタイトルが from Sleipnir になっていますが、保存が終わるとページのタイトルを自動的に取得してくれます。さすがSafari。なので、とりあえずこの書き方でも問題なく動作します。
    • key code 53
      という部分はescキーを押すことと同じです。
      番号の割り当ては AppleScript Key Code List を参照してください。

Chrome

tell application "Google Chrome"
 set myURL to get URL of active tab of first window
 set myTitle to get title of active tab of first window
end tell
tell application "Safari" to add reading list item myURL with title myTitle

Firefox

-- my gui_check()
set temp to the clipboard as text
tell application "Firefox"
 activate
 set myTitle to the name of front window
 tell application "System Events"
  keystroke "l" using {command down}
  keystroke "c" using {command down}
  delay 0.5
  set myURL to the clipboard as text
 end tell
end tell
tell application "Safari" to add reading list item myURL with title myTitle
delay 0.5
set the clipboard to temp as text

(* to gui_check()
 tell application "System Events"
  if UI elements enabled is false then
   tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.universalaccess"
    set msg to "GUI scripting is not available." & return & "Do you put the check in" & return & "\"Enable access for assistive devices\" ?"
    --display dialog msg buttons {"OK"} default button "OK" with icon note --giving up after 10
    display dialog msg buttons {"cansel", "check and try"} with icon note
   end tell
   set UI elements enabled to true
   delay 0.5
   tell application "System Preferences" to quit
   delay 0.5
  end if
 end tell
end gui_check *)
tell application "Safari" to quit

Sleipnir

(2014年1月23日追記)すみません、下記を以前テストしたときには動いたのですが、たまにうまくいきません。。。改善の余地ありですが時間があるときにやります。。。
set temp to the clipboard as text
tell application "Sleipnir" to activate
tell application "System Events"
 keystroke "l" using {command down, option down}
 keystroke "c" using {command down}
 key code 53
 delay 0.5
 set myURL to the clipboard as text
end tell

tell application "Safari" to add reading list item myURL with title "from Sleipnir"
delay 0.5
set the clipboard to temp as text

tell application "Safari" to quit

しかし…

「リーディングリストに登録したらオフラインでも読める」というのはいつまでなんでしょうかね。かなり前にリーディングリストに追加した記事に久しぶりにアクセスすると「Page not found」と出ました。おそらく一定期間か、一定サイズのどちらかしかオフライン用に保存できないのでしょうね。。。使えるんだか使えないんだか、この機能。いつまでも保存しておきたいなら別のサービスを使ったほうが良いでしょうね。