環境:
Mac OS X 10.4.9 (PPC)
Safari 2.0.4
Safari のアクティブなウインドウで
http://ブログID.blog数字.fc2.com/blog-entry-数字.html
という URL を開いているときに、そのエントリーに対応した編集ページを開く AppleScript。
管理画面からでは過去のエントリーを見つけにくいときなどに使います。
FC2 ブログにログインしている状態であれば URL を並べ替えて location.href で一発。
しかし最近なぜかログイン状態を長く維持できなくなってしまったため、自動ログインも含めて長めのスクリプトになりました。
※実行する前にスクリプト中の "hogemaru@hage.com" を FC2 ID の自分のメールアドレスに、"password" という部分を自分のパスワードに書き換えておかないとログインに失敗します。
--スクリプトここから
--http://ブログID.blog数字.fc2.com/blog-entry-数字.html
--という URL を開いてから実行。
tell application "Safari"
activate
try
set theURL to (URL of document 1)
theURL
on error
return
end try
set bs to (ASCII character 128)
set aBool to (do shell script "echo -n " & quoted form of theURL & " | ruby -n -e 'print %r|^http://.+" & bs & ".blog" & bs & "d+" & bs & ".fc2" & bs & ".com/blog-entry-" & bs & "d+" & bs & ".html$| =~ $_'")
if aBool is "0" then
set rbScript to "str1 = $_.split('/'); str2 = str1[2].to_s.split('.'); str3 = str1[3].gsub(/[^" & bs & "d]/, ''); print 'http://' + str2[1]+ '.' + str2[2] + '.' + str2[3] + '/' + str2[0] + '/' + 'admin.php?mode=editentry&no=' + str3"
set adminURL to (do shell script "echo -n " & quoted form of theURL & " | ruby -n -e " & quoted form of rbScript)
do JavaScript "location.href='" & adminURL & "'" in document 1
else
return
end if
if not my readyStateComplete(90) then return
if "admin.php?mode=admin" is in (source of document 1) then return
--(*
do JavaScript "location.href='http://id.fc2.com/'" in document 1
if not my readyStateComplete(90) then return
if (name of document 1) does not contain "管理パネル" then
do JavaScript "document.form_login.email.value='" & "hogemaru@hage.com" & "'" in document 1
--hogemaru@hage.com を FC2 ID のメールアドレスに書き換える
do JavaScript "document.form_login.pass.value='" & "password" & "'" in document 1
--password を FC2 ID のパスワードに書き換える
do JavaScript "document.form_login.submit()" in document 1
--*)
if not my readyStateComplete(90) then return
end if
set blogLink to ""
repeat 10 times
set linkCnt to (do JavaScript "document.links.length" in document 1)
repeat with i from 1 to linkCnt
set curLink to (do JavaScript ("document.links[" & (i - 1) as string) & "].href" in document 1)
if curLink starts with "http://blog" and curLink contains "/admin.php?mode=admin" then
set blogLink to curLink
exit repeat
end if
end repeat
if blogLink is not "" then exit repeat
end repeat
if blogLink is "" then return
do JavaScript "location.href='" & blogLink & "'" in document 1
if not my readyStateComplete(90) then return
do JavaScript "location.href='" & adminURL & "'" in document 1
end tell
on readyStateComplete(timeoutValue)
with timeout of 120 seconds
set bs to (ASCII character 128)
tell application "Safari"
repeat 4 times
set theState to (do JavaScript "document.readyState" in document 1)
if theState is "uninitialized" or theState is "loading" or theState is "interactive" then
exit repeat
else
delay 1
end if
end repeat
repeat timeoutValue times
if (do JavaScript "document.readyState" in document 1) is "complete" then
delay 1
return true
else
delay 1
end if
end repeat
do JavaScript "alert('ページの読み込み完了を確認できませんでした。" & bs & "nスクリプトを中断します。')" in document 1
return false
end tell
end timeout
end readyStateComplete
--スクリプトここまで
どうも document.readyState の使い勝手が良くないなあ。
たまに location.href の後にジャンプがなかなか始まらないことがあると移動前のページの complete が返ってしまうし、始まったら始まったで完全に読み込まないうちに complete を返してくる。
大事をとって delay や repeat を使わざるをえないので次の実行に移るまでに無駄な待ち時間が発生してしまいます。
結局、手動でログインしてから実行するのが確実。