Archive for the 'インターネット(internet)' Category

03/01 curlでダウンロードv2

curlで指定URLから、指定フォルダにダウンロードを行うAppleScriptです。ファイル名はURLから自動取り出しを行います。

Mac OS X 10.7でURL Access Scriptingが廃止になりました。URL Access Scriptingは、たしかに手軽で便利ではありましたが、非同期ダウンロードができないため、かなり昔から個人的にはcurlを愛用していました。

URL Access ScriptingはMac OS 9の頃からの「負の遺産」(ダウンロード時のファイル名の長さに、いまだに32文字制限があったなど)ともいえる仕様を引きずっており、Appleに作り直す気がなければ消えることを宿命づけられていたものです。

で、一応curlでダウンロードを行うサブルーチンを、少し整備してみましたが……このあたり、人によってずいぶんと趣味が違うはずなので、もっとシンプルなものがいいとか、ゴージャスなものがいいとか、自分の趣味に応じて作り変えるべきだと思います。普段使っている処理では、ダウンロードを非同期で行わせることがよくあるので、そういう方向で強化してもよいと思われます。

スクリプト名:curlでダウンロードv2
–動作確認のためにダイアログ入力しているだけ
set aURL to text returned of (display dialog “URL to download” default answer “” buttons {“OK”} with icon 1)

set dlFolder to path to desktop –ダウンロード先フォルダ(Desktop)
set dRes to downloadFile(aURL, dlFolder, 3600) of me
–POSIX pathが返ってくる。あとでのんびりaliasに変換

–通常ダウンロード
on downloadFile(aURL, outPath, tOut)
  
  
set aFileName to getFNfromURL(aURL) of me
  
if aFileName = false then
    return “”
  end if
  
  
set dlPathStr to POSIX path of outPath
  
set aPOSIXpath to dlPathStr & aFileName
  
  
set dssText to “curl -s “ & aURL & ” -o “ & quoted form of aPOSIXpath
  
try
    with timeout of tOut seconds
      do shell script dssText
    end timeout
  on error
    return false
  end try
  
  
return aPOSIXpath
  
end downloadFile

on getFNfromURL(aURL)
  if aURL ends with “/” then
    –display dialog “与えられたURLの末尾がスラッシュで終了しているため、ファイルではありません。” buttons {”OK”} default button 1
    
return false
  end if
  
set aLen to length of aURL
  
set aR to reverse of (characters of aURL)
  
set aNstr to aR as string
  
set aLoc to offset of “/” in aNstr
  
set aRes to text (aLen - aLoc + 2) thru -1 of aURL
  
return aRes
end getFNfromURL

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

12/24 dict.orgで指定の単語の意味を調べて返す

JN SoftwareのAppleScriptObjCサンプル「MenuApp ASOC」に入っていたサブルーチンを抜き出して、個人的な趣味に合うように書き換えたものです。dict.orgで指定の英単語を調べて意味を返します。

searchAndReplaceというサブルーチンは、オリジナルでは「snr」という名前であり、プログラムを読めば置換ルーチンであることは理解できるのですが、そんなアセンブラのニーモニック並みに短い名前にされては可読性が落ちまくりです(かといって、AppleScriptObjCのCocoaのClass名並みにダラダラ長くても困るのですが……なんであれは別名命名機能でも用意して短縮名称で呼ぶようなことができないんだろう ーー;)

人のソースを分析しては、使えるものはきちんと整理、分析、評価を行って、再利用しやすいように補助コードをつけて保存していますが、世界はけっこう広いので、流儀がまったく異なる流派もあってなかなか苦労することがあります。

分析時には、まず自分の流儀に合うようにプログラムを書き換えることが重要です。実にそう思います。

「my」を使う派 vs 「of me」を使う派。「toでハンドラを宣言する派」vs「onでハンドラを宣言する派」。「tellブロックで対象objectをまとめる派」vs「object’sで対象を記述する派」といった、些細な相違点があって……ASOC系のサンプルコードは「tellブロックが嫌い派」のChris Page@Appleの影響が色濃く出ており、いまひとつ読みにくいように感じます。

自分は、「tellブロックでまとめる派」なので、サンプルを書き換えてから読むようにしていますが、いつかChris Pageとは直接話をして決着を付ける必要がありそうです(ーー;。

スクリプト名:dict.orgで指定の単語の意味を調べて返す
set ASCII_13 to (ASCII character 13)
set word_to_define to "AppleScript"
set the_definitions to get_definition(word_to_define) of me
set aRes to string_to_list(the_definitions, (ASCII_13 & "." & ASCII_13)) of me

–>
(*
{"\"applescript\" foldoc \"The Free On-line Dictionary of Computing (27 SEP 03)\"
AppleScript

<language> An {object-oriented} {shell} language for the
{Macintosh}, approximately a superset of {HyperTalk}.

(1995-12-10)

."}
*)

on get_definition(word_to_define)
  try
    set ASCII_13 to (ASCII character 13)
    
set the_script to snr(("dict://dict.org/d:" & word_to_define & ":*"), " ", "%20") of me
    
–Thanks to Greg Spense for the grep code
    
set the_definitions to do shell script "curl -s " & (quoted form of the_script) & " | egrep -v ‘^220.*|^250.*|^150.*|^221.*’"
    
set the_definitions to searchAndReplace(the_definitions, "151 ", "") of me
    
set the_definitions to string_to_list(the_definitions, (ASCII_13 & "." & ASCII_13)) of me
    
return the_definitions
  on error the_err
    return the_err
  end try
end get_definition

on searchAndReplace(the_string, search_string, replace_string)
  tell (a reference to AppleScript’s text item delimiters)
    set {old_tid, contents} to {contents, search_string}
    
set {the_string, contents} to {the_string’s text items, replace_string}
    
set {the_string, contents} to {the_string as Unicode text, old_tid}
  end tell
  
return the_string
end searchAndReplace

on string_to_list(s, d)
  tell (a reference to AppleScript’s text item delimiters)
    set {o, contents} to {contents, d}
    
set {s, contents} to {s’s text items, o}
  end tell
  
return s
end string_to_list

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

12/15 Googleの未公開APIで英文テキスト読み上げ

Googleの未公開APIを呼び出して英文テキストの読み上げ(Text To Speech)を行うAppleScriptです。

# Mac OS X 10.7では、URL Access Scriptingが廃止になったので、本Scriptはそのままでは動きません

本当は「open location」文でURL+パラメータを指定すればWebブラウザで音声が再生されてそれでオシマイ、という程度の処理なのですが……それではあまりに芸がないので、ローカルにGoogleが吐いたMP3データをダウンロードして、QuickTime Player Xで再生(10.5や10.4を使っている人は、この部分だけ書き換えてください)するようにしてみました。

いつまでもこのAPIが使えるかの保証はありません(使えなくなっていたら教えてください)。ただ……このまま行くと「多言語Text To Speech」の機能に育っていきそうな予感が……。

別に、Mac OS Xではsayコマンドで英文テキスト読み上げの機能があるわけですが、Web上のサービスを使えるということは、同時に並列処理でいくつもリクエストを送って処理できるというわけで……長い文章も並列リクエスト&ダウンロードだけで読み上げデータが作れる可能性があるわけです。

スクリプト名:Googleの未公開APIで英文テキスト読み上げ
–Googleの未公開APIで英文テキスト読み上げ

–参照:
–http://jp.techcrunch.com/archives/20091214the-unofficial-google-text-to-speech-api/

–文章をダイアログから入力
set aMes to “I’m lerning AppleScript very hard”
set aMes to (text returned of (display dialog “Googleにしゃべらせる英語の文章を入力(100文字以内)” default answer aMes))

–文章の長さチェック。文字種類チェックはしていないので自己責任で
if length of aMes > 100 then
  display dialog “100文字以内で入力してください。” buttons {“OK”} default button 1 with icon 1
  
return
end if

–パスをstringでcastしたあとにunicodeにcastしているのはMac OS X 10.4までの仕様。10.5以降では必要ない
set aPath to (path to temporary items from user domain as text)

–URL Access Scriptingで生成できるファイルの名称が32文字までなので、ちょっとuuidgenの結果を短くして使う
set dPath to aPath & (text 1 thru 20 of (do shell script “/usr/bin/uuidgen”)) & “.mp3″ as Unicode text

set baseURL to “http://translate.google.com/translate_tts?q=”
set aParam to repChar(aMes, ” “, “+”) of me

set aURL to baseURL & aParam

–Googleにリクエストを投げて、返ってきたMP3ファイルをローカルに保存する
try
  with timeout of 30 seconds
    tell application “URL Access Scripting”
      activate
      
download aURL to file dPath with progress
    end tell
  end timeout
on error
  display dialog “Download Error” buttons {“OK”} default button 1 with icon 2
  
return
end try

–ダウンロードした音声ファイルをオープンして再生
tell application id “com.apple.QuickTimePlayerX”
  open file dPath
  
tell document 1
    play
  end tell
end tell

–文字置換ルーチン
on repChar(origText, targStr, repStr)
  set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
  
set temp to text items of origText
  
set AppleScript’s text item delimiters to repStr
  
set res to temp as text
  
set AppleScript’s text item delimiters to txdl
  
return res
end repChar

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

08/15 メールアドレスチェック

メールアドレスの妥当性チェックルーチンです。(複数の)アドレスをリストに入れて呼び出すと、妥当性を判断して返します。

実際に妥当性を判断するのは、文字種別によるものと、ドメイン部分についてはwhoisによるリクエストを実行しています(インターネット接続されている環境でないと本ルーチンは動作できません)。

whoisコマンドのオーバーヘッドが大きいので、これを並列処理すれば大幅にスピードアップするものと思われます。

ただし、妥当性を判断するといってもこの程度なので、当該のメールアドレスが生きているかどうか、存在するかどうかについては検証を行っていません。

そのかわりといっては何ですが、ドメインの検証については……ドメインが存在しなかった場合にはサブドメインをある程度さかのぼって検証するようになっています。

説明が乱暴だったので補足すると……サブドメイン付きでwhoisを発行しても、「そんなもん知らない」と言われるので、1階層ずつサブドメインをさかのぼって、xx.xxのレベルになるまで繰り返します。途中でヒットしたら、そういうドメインが存在するという判断を行います。

このへん、大量に検証する場合には「検証ずみドメイン」のリスト(問い合わせキャッシュ)を作成しておいて、同じドメインには二度問い合わせを行わないといった処理を付加すると、ちょっとだけ賢い感じがします。

whoisでドメインの存在確認を行うより、もうすこしスマートでマシな方法もありそうですが、結局スパムすれすれな処理になってしまいそう(メールを送りつけてUser Unknownでエラーが返ってくるかどうか判定するとか)なので、まあこんなもんだろうかと。

あと、ANKというのは……ものすごく昔、8ビットCPUの時代の頃に1バイトのASCII文字列(Alphabet, Numeric, Kana)のことを指して言った太古の昔の言葉であり、だいたいいまどき半角カナなど使わないので……死語間違いなし。もうちょっと分りやすい言葉を使うべきであったかと反省しています。

スクリプト名:メールアドレスチェック v2
set adrList to {”maro@asia.damedame-domain.co.jp“, maro@piyocast.com“, maro@appleco.jp“}

set resList to {}

repeat with i in adrList
  半角文字列チェック
  
set aRes to detectOutANK(i) of me
  
if aRes = true then
    whoisを使用してドメインの妥当性を確認
    
set aDomain to splitAfterAtmark(i) of me
    
set aRes to checkMailAddr(aDomain) of me
  end if
  
set the end of resList to aRes
end repeat

resList
> {false, true, true}

メールアドレスのチェック(whois実行)
on checkMailAddr(anAdr)
  マッチしない場合には”aaa.aaa”の状態(ピリオド1こ)になるまでドメインをさかのぼる
  
repeat
    set aRes to countPeriod(anAdr) of me
    
if anAdr is in {”co.jp“, or.jp“, ne.jp“, go.jp“, ac.jp“, ad.jp“} then return false このへん、まだ見直す必要がある
    
    
if aRes = 0 then return false
    
    
with timeout of 3600 seconds
      set aRes to do shell script whois & anAdr
    end timeout
    
    
ignoring case
      if aRes contains No match then
        set anAdr to makeDomainShorter(anAdr) of me
        
if anAdr = false then return false
      else
        return true
      end if
    end ignoring
  end repeat
  
return false
end checkMailAddr

メールアドレスのサブドメイン部分を1階層分削る
Input: “asia.apple.com”
Output: “apple.com”
on makeDomainShorter(anAdr)
  set dotPos to offset of . in anAdr
  
if dotPos = 0 then return false
  
set resText to text (dotPos + 1) thru -1 of anAdr
  
return resText
end makeDomainShorter

@以降の文字列を返す
on splitAfterAtmark(anAdr)
  set atPos to offset of @ in anAdr
  
if atPos = 0 then return false
  
set domainText to text (atPos + 1) thru -1 of anAdr
  
return domainText
end splitAfterAtmark

ANK範囲外の文字があるかどうかをテストする
ANK文字以外のものが入っていたらfalse
on detectOutANK(testText)
  ANK文字列(大文字小文字は問わない)
  
set ankChar to {”0“, 1“, 2“, 3“, 4“, 5“, 6“, 7“, 8“, 9“, a“, b“, c“, d“, e“, f“, g“, h“, i“, j“, k“, l“, m“, n“, o“, p“, q“, r“, s“, t“, u“, v“, w“, x“, y“, z“, .“, -“, @“, _“}
  
  
set _testChar to testText as Unicode text
  
  
ignoring case
    repeat with i in _testChar
      set j to contents of i
      
if j is not in ankChar then
        return false
      end if
    end repeat
  end ignoring
  
  
return true
end detectOutANK

on countPeriod(aStr)
  set cList to characters of aStr
  
set pCount to 0
  
repeat with i in cList
    if contents of i = . then set pCount to pCount + 1
  end repeat
  
log {”pCount“, pCount}
  
return pCount
end countPeriod

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

07/20 Twitterifficで選択中の発言の時間をGrowlで表示。アイコン付きで v3

Twitterrifficで選択中の発言が行われた時間をGrowlでアイコン付き表示するAppleScriptの改良版です。

URL Access Scriptingに指定するURLのうちファイル名に該当する部分の長さが32文字以上だと、勝手にダウンロード時にファイル名を丸められてしまう、という問題に対処するため、28文字でファイル名チェックを行い、短く付け替えています。

ちょうど、坂本龍一教授をFollowして、たまたま発言を見つけたので時刻を調べてみたらエラーに遭遇。ファイル名を調べてみたら37文字で、テンポラリフォルダに入っていた画像ファイルがすべて途中で丸められていることが判明。URL Access Scriptingの32文字制限に気付いて、対策を施したものです。

ダウンロード後に画像が真っ白になっているケースが見られたので、syncコマンドを実行してキャッシュの内容をHDDにシンクロさせています。

(more…)

07/20 Twitterifficで選択中の発言の時間をGrowlで表示。アイコン付きで v2

Twitterrifficからアイコンを取得して、ダウンロードを行おうとすると弾かれるケースが見られたので、とりあえず「すでにダウンロードしたアイコンがあれば使用する」ように処理を変更したバージョンです。

アイコンのダウンロードが行えない時に、TwitterアカウントのURLを個別にAppleScriptで取得して、そのままSafariで表示すると問題ないようなので、案外User Agent名称のチェックをサーバー側で行っているのかもしれません。

URL Access ScriptingにはUser Agent名称を詐称する機能はないため、そのための対策を行うのであればシェルからcurlコマンドを呼び出す必要が出てくることでしょう。

もう少し、エラーが発生する状況を観察してみることとします。

(more…)

06/25 WordPressのBlogに画像ファイルをアップロード

WordPressのBlogに選択した画像をアップロードするAppleScriptです。

WordPressに対してXML-RPC経由でああでもないこうでもない、と画像のアップロードを行うテストを繰り返してきたものの、アップロードされるのにbase64でエンコードされたテキストになるばかりで、デコードされませんでした。

いろいろ調べてみたところ、こちらに画像アップロードのサンプルAppleScriptが置いてあるのを見つけました。しかし、アップロードするための呼び出し部分は書いていないし、そのままでは正規表現用のOSAXが必要なものでした。

  「この程度、OSAX使わなくても書けるだろー」

OSAX依存部分をAppleScriptのサブルーチンで置き換えてみました。本サンプルには、Blogのユーザー名とパスワードは記入していませんが、ご自分でお持ちのBlogのユーザー名とパスワードを記入し、BlogのXML-RPCで呼び出すURLを記入して実行してみてください。

WordPress ME 2.0.11および(本稿執筆時点で)最新のWordPress2.8での動作を確認してあります。

スクリプト名:WordPressのBlogに画像ファイルをアップロード
http://theotherblog.com/wp-content/files/newMediaObject.html
書き換えて、TextCommands OSAXを使用しなくても動くようにした

property curl : "/usr/bin/curl"
property theUsername : "Blogのユーザー名"
property thePassword : "Blogのパスワード"

set aFile to choose file
UploadImage(aFile) of me

on UploadImage(theFile)
  
  
try
    tell application "Image Events"
      launch
      
set this_image to open theFile
      
extract the property value
      
set theType to file type of this_image
      
set theLocation to location of this_image
      
set theName to name of this_image
      
set {theWidth, theHeight} to dimensions of this_image
      
close this_image
    end tell
    
display dialog "Resolution: " & (xres as string)
  on error error_message
    display dialog error_message
  end try
  
  
get the action from the pages source code
  
set action to "http://piyocast.com/as/xmlrpc.php"
  
  
set theBits to encodeAsBase64(theFile)
  
set theType to "image/png"
  
  
set theTemplate to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<methodCall>
<methodName>metaWeblog.newMediaObject</methodName>
<params>
<param>
<value><string>1</string></value>
</param>
<param>
<value><string>" & theUsername & "</string></value>
</param>
<param>
<value><string>" & thePassword & "</string></value>
</param>
<param>
<value><struct>
<member>
<name>bits</name>
<value><base64>
" & theBits & "</base64></value>
</member>
<member>
<name>name</name>
<value><string>
" & theName & "</string></value>
</member>
<member>
<name>type</name>
<value><string>
" & theType & "</string></value>
</member>
</struct></value>
</param>
</params>
</methodCall>
"
  
  
set theScript to (curl & " -d" & space & "" & theTemplate & "" & space & " -H \"Content-Type: text/xml\" -X POST " & action) as text
  
  
try
    set theText to do shell script theScript
    
set theURL to doRegex(theText, "<string>(http://.*)</string>")
    
set theText to retNoTagValue(theText, "string") of me
    
return theText contains a URL to the file on the server
  on error err
    display dialog err as string
    
return false
  end try
  
  
I could use parse XML except fishing out the URL is easier…
  
  
end UploadImage

on encodeAsBase64(theFilePath)
  set thePath to quoted form of POSIX path of (theFilePath)
  
set theScript to "openssl base64 < " & thePath
  
return do shell script theScript
end encodeAsBase64

文字置換ルーチン
on repChar(origText, targStr, repStr)
  set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
  
set temp to text items of origText
  
set AppleScript’s text item delimiters to repStr
  
set res to temp as text
  
set AppleScript’s text item delimiters to txdl
  
return res
end repChar

指定タグを外す
on retNoTagValue(aLine, parseTagVal)
  set parseTagVal to "string"
  
set beginTag to "<" & parseTagVal & ">"
  
set endTag to "</" & parseTagVal & ">"
  
  
set sNum to offset of beginTag in aLine
  
set sNum to (length of beginTag) + sNum
  
set eNum to (offset of endTag in aLine) - 1
  
set resText to (characters sNum thru eNum of aLine) as text
  
  
return resText
  
end retNoTagValue

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/20 WordPress.comが受け取るXML-RPCのメソッド名一覧を取得する

本Blogでも利用しているBlogソフトウェアWordPress。さまざまな外部のサービスと連携するために、XML-RPCによるAPIが用意されています。そのWordPressのAPIを呼び出すサンプルです。

手始めに、WordPressのホスティングを行っていて、3GBまで無料で利用できるWordPress.comにアカウントを取得。このサービスがどのようなAPIを持っているか、その一覧を取得するAppleScriptを書いてみました。

XML-RPCによるWebサービスは、必死になって探さないとなかなか継続して行われているサービスを(日本国内で)みつけることは難しいところですが、Blogはその例外といえるのかもしれません。

ちなみに、WordPress.comにコンテンツをホスティングすると、AS-Holeのコンテンツ内に記述しているスクリプトリンク(「applescript://」ではじまるリンク)がすべて無効化されてしまうので、現状ではWordPress.comにAS Holeを移転させることはできません。

スクリプト名:WordPress.comが受け取るXML-RPCのメソッド名一覧を取得する
tell application "http://piyomaru.wordpress.com/xmlrpc.php" たぶん、最新版のWordPress(2.8)
  call xmlrpc {method name:"mt.supportedMethods", parameters:{}}
end tell

> {"wp.getUsersBlogs", "wp.getPage", "wp.getPages", "wp.newPage", "wp.deletePage", "wp.editPage", "wp.getPageList", "wp.getAuthors", "wp.getCategories", "wp.getTags", "wp.newCategory", "wp.deleteCategory", "wp.suggestCategories", "wp.uploadFile", "wp.getCommentCount", "wp.getPostStatusList", "wp.getPageStatusList", "wp.getPageTemplates", "wp.getOptions", "wp.setOptions", "wp.getComment", "wp.getComments", "wp.deleteComment", "wp.editComment", "wp.newComment", "wp.getCommentStatusList", "blogger.getUsersBlogs", "blogger.getUserInfo", "blogger.getPost", "blogger.getRecentPosts", "blogger.getTemplate", "blogger.setTemplate", "blogger.newPost", "blogger.editPost", "blogger.deletePost", "metaWeblog.newPost", "metaWeblog.editPost", "metaWeblog.getPost", "metaWeblog.getRecentPosts", "metaWeblog.getCategories", "metaWeblog.newMediaObject", "metaWeblog.deletePost", "metaWeblog.getTemplate", "metaWeblog.setTemplate", "metaWeblog.getUsersBlogs", "mt.getCategoryList", "mt.getRecentPostTitles", "mt.getPostCategories", "mt.setPostCategories", "mt.supportedMethods", "mt.supportedTextFilters", "mt.getTrackbackPings", "mt.publishPost", "pingback.ping", "pingback.extensions.getPingbacks", "demo.sayHello", "demo.addTwoNumbers", "wpStats.check_key", "wpStats.get_blog_id", "wpStats.get_site_id", "wpStats.update_bloginfo", "wpStats.update_postinfo", "wpStats.ping_blog", "wpStats.flush_posts"}

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/17 郵便専門ネットでXML-RPC経由で郵便番号を6桁(チェックデジット付き)の全国地方公共団体コード/JISコード/市町村コードに変換

郵便専門ネットで、指定の郵便番号に対応する地方公共団体コード(JISコード、チェックサム付き=6桁)を取得するAppleScriptです。

スクリプト名:郵便専門ネットでXML-RPC経由で郵便番号を6桁(チェックデジット付き)の全国地方公共団体コード/JISコード/市町村コードに変換
tell application "http://yubin.senmon.net/service/xmlrpc/"
  call xmlrpc {method name:"yubin.postcodeToJiscode6", parameters:{"1760024"}}
end tell

> "131202"

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/17 郵便専門ネットで引数に指定した郵便番号で何件ヒットするのかをint型で返す

郵便専門ネットで、XML-RPC経由で引数に指定した郵便番号(7桁なくてOK)で、該当する郵便番号が何件ヒットするかを数値で返すAppleScriptです。

スクリプト名:郵便専門ネットで引数に指定した郵便番号で何件ヒットするのかをint型で返す
tell application http://yubin.senmon.net/service/xmlrpc/
  call xmlrpc {method name:”yubin.getFetchCountByPostcode“, parameters:{”19800“}}
end tell

> 41

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/17 郵便専門ネットで郵便番号の存在チェック

郵便専門ネットで、郵便番号の存在チェックを行うAppleScriptです。存在する場合にはtrue、存在しない場合にはfalseが返ります。

スクリプト名:郵便専門ネットで郵便番号の存在チェック
tell application http://yubin.senmon.net/service/xmlrpc/
  call xmlrpc {method name:”yubin.postcodeExists“, parameters:{”1760024“}}
end tell
> true

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/17 郵便専門ネットでXML-RPC経由で郵便番号から住所を返す

郵便専門ネットで、XML-RPC経由で郵便番号から住所を返すAppleScriptです。

スクリプト名:郵便専門ネットでXML-RPC経由で郵便番号から住所を返す
tell application http://yubin.senmon.net/service/xmlrpc/
  call xmlrpc {method name:”yubin.fetchAddressByPostcode“, parameters:{”1760024“}}
end tell

> {{data_type:”p”, town_kana:”なかむら”, town:”中村”, pref_kana:”とうきょうと”, yid:39309, jiscode:”13120″, pref:”東京都”, city:”練馬区”, city_kana:”ねりまく”, addr_name_kana:application “http://yubin.senmon.net/service/xmlrpc/”, other:application “http://yubin.senmon.net/service/xmlrpc/”, addr_name:application “http://yubin.senmon.net/service/xmlrpc/”, postcode:”1760024″}}

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/17 郵便専門ネットでXML-RPC経由で引数に都道府県のJISコード(JISコードの先頭2文字)を渡すと、その都道府県に属しているJISコードを取得

郵便専門ネットで、XML-RPC経由で地方公共団体コード(JISコード)の先頭2文字を渡すと、そこに所属するJISコードの一覧を返すAppleScriptです。

スクリプト名:郵便専門ネットでXML-RPC経由で引数に都道府県のJISコード(JISコードの先頭2文字)を渡すと、その都道府県に属しているJISコードを取得
tell application http://yubin.senmon.net/service/xmlrpc/
  call xmlrpc {method name:”yubin.jiscodeFromPrefcode“, parameters:{”13“}}
end tell

> {”13101″, “13102″, “13103″, “13104″, “13105″, “13106″, “13107″, “13108″, “13109″, “13110″, “13111″, “13112″, “13113″, “13114″, “13115″, “13116″, “13117″, “13118″, “13119″, “13120″, “13121″, “13122″, “13123″, “13201″, “13202″, “13203″, “13204″, “13205″, “13206″, “13207″, “13208″, “13209″, “13210″, “13211″, “13212″, “13213″, “13214″, “13215″, “13218″, “13219″, “13220″, “13221″, “13222″, “13223″, “13224″, “13225″, “13227″, “13228″, “13229″, “13303″, “13305″, “13307″, “13308″, “13361″, “13362″, “13363″, “13364″, “13381″, “13382″, “13401″, “13402″, “13421″}

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/17 郵便専門ネットで一度に取得できるデータ件数を返す

郵便専門ネットで、XML-RPC経由で一度に取得できるデータ件数を返すAppleScriptです。

スクリプト名:郵便専門ネットで一度に取得できるデータ件数を返す
tell application http://yubin.senmon.net/service/xmlrpc/
  call xmlrpc {method name:”yubin.getMaxFetchCount“, parameters:{}}
end tell
> 100

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/17 郵便専門ネットでXML-RPC経由で郵便番号に対応する世界測地系(WGS84)の緯度経度コード(Geocode)を返す

郵便専門ネットでXML-RPC経由で郵便番号に対応する世界測地系(WGS84)の緯度経度コード(Geocode)を返すAppleScriptです。

スクリプト名:郵便専門ネットでXML-RPC経由で郵便番号に対応する世界測地系(WGS84)の緯度経度コード(Geocode)を返す
tell application http://yubin.senmon.net/service/xmlrpc/
  call xmlrpc {method name:”yubin.postcodeToGeocode“, parameters:{”1760024“}}
end tell

> {”35.7321262″, “139.6382562″}

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/17 郵便専門ネットで道府県のコード(地方公共団体コードの先頭2文字)から都道府県名を返す

郵便専門ネットで都道府県のコード(地方公共団体コードの先頭2文字)から都道府県名を返すAppleScriptです。

スクリプト名:郵便専門ネットで道府県のコード(地方公共団体コードの先頭2文字)から都道府県名を返す
tell application http://yubin.senmon.net/service/xmlrpc/
  call xmlrpc {method name:”yubin.getPrefName“, parameters:{”13“}}
end tell
> “東京都”

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/17 郵便専門ネットでXML-RPC経由でJISコード(5桁、6桁どちらでも)から、その市区町村に属している郵便番号のリストを取得

郵便専門ネットから、XML-RPC経由で地方公共団体コード(JISコード)の、5桁あるいは6桁(チェックサムつき)から、その地方公共団体に所属する郵便番号をリストで返すAppleScriptです。

スクリプト名:郵便専門ネットでXML-RPC経由でJISコード(5桁、6桁どちらでも)から、その市区町村に属している郵便番号のリストを取得
tell application http://yubin.senmon.net/service/xmlrpc/
  call xmlrpc {method name:”yubin.postcodeFromJiscode“, parameters:{”011037“}}
end tell

> {”0070001″, “0070002″, “0070003″, “0070004″, “0070005″, “0070006″, “0070011″, “0070030″, “0070031″, “0070032″, “0070033″, “0070034″, “0070801″, “0070802″, “0070803″, “0070804″, “0070805″, “0070806″, “0070807″, “0070808″, “0070809″, “0070810″, “0070811″, “0070812″, “0070813″, “0070814″, “0070815″, “0070819″, “0070820″, “0070821″, “0070822″, “0070823″, “0070824″, “0070825″, “0070826″, “0070827″, “0070828″, “0070829″, “0070834″, “0070835″, “0070836″, “0070837″, “0070838″, “0070839″, “0070840″, “0070841″, “0070842″, “0070843″, “0070844″, “0070845″, “0070846″, “0070847″, “0070848″, “0070849″, “0070850″, “0070851″, “0070852″, “0070861″, “0070862″, “0070863″, “0070864″, “0070865″, “0070866″, “0070867″, “0070868″, “0070869″, “0070870″, “0070871″, “0070872″, “0070873″, “0070874″, “0070880″, “0070881″, “0070882″, “0070883″, “0070884″, “0070885″, “0070886″, “0070890″, “0070891″, “0070892″, “0070893″, “0070894″, “0070895″, “0078501″, “0078503″, “0078505″, “0078507″, “0078508″, “0078632″, “0600905″, “0600906″, “0600907″, “0600908″, “0600909″, “0608533″, “0608576″, “0608582″, “0608791″, “0650000″, “0650004″, “0650005″, “0650006″, “0650007″, “0650008″, “0650009″, “0650010″, “0650011″, “0650012″, “0650013″, “0650014″, “0650015″, “0650016″, “0650017″, “0650018″, “0650019″, “0650020″, “0650021″, “0650022″, “0650023″, “0650024″, “0650025″, “0650026″, “0650027″, “0650028″, “0650030″, “0650031″, “0650032″, “0650033″, “0650041″, “0650042″, “0650043″, “0658501″, “0658508″, “0658510″, “0658511″, “0658518″, “0658522″, “0658533″, “0658543″, “0658550″, “0658555″, “0658558″, “0658567″, “0658578″, “0658601″, “0658609″, “0658610″, “0658611″, “0658612″, “0658633″, “0658639″, “0658686″}

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/17 郵便専門ネットでバージョン番号を取得

XML-RPC経由で、インターネット上のWebサービスを呼び出すサンプルのAppleScriptです。

たまたまみつけた、「XML-RPCサービス - 郵便専門ネット」というサイトのサービスを呼び出してみます。とりあえず、ごあいさつまでに(サービスの)バージョン番号を取得。

ちなみに、郵便専門ネットさんのWebサービスでは1分間に30回以上呼び出すとエラーになるとのことです。

スクリプト名:郵便専門ネットでバージョン番号を取得
http://yubin.senmon.net/service/xmlrpc/
tell application http://yubin.senmon.net/service/xmlrpc/
  call xmlrpc {method name:”yubin.getVersion“, parameters:{}}
end tell
> “9.05a”

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/13 津田大介氏の最近の発言を取得してMacJournalにスクラップする

twitterrificをコントロールして、津田大介氏のtwitter上の発言を集めて、時系列順に並べかえ直して、MacJournalの指定のジャーナル内に新規ジャーナルエントリを作成して、そこに文章を入れるAppleScriptです。

mj11.jpg

▲twitteriffic上で津田大介氏によるテキスト中継が行われている様子

mj10.jpg

▲スクリプト実行後 MacJournalの指定エントリに発言内容がまとめられている

スクリプト名:津田大介氏の最近の発言を取得してMacJournalにスクラップする
津田大介氏のtwitter上の発言を集めてテキスト化
tell application "Twitterrific"
  set tList to every tweet whose screen name is equal to "tsuda"
  
set textList to {}
  
repeat with i in tList
    set the end of textList to {date of i, text of i}
  end repeat
end tell

set sortedList to shellSortListAscending(textList, 1) of me

set pureText to {}
repeat with i in sortedList
  set {aDate, aText} to i
  
set the end of pureText to (aText & return & return)
end repeat

set pureText to pureText as string

Journalの階層状況は個人によって異なるので、このまま動くわけではありません
ご自分の環境に合わせて書き換えて使ってみてください
tell application "MacJournal"
  tell document 1
    tell journal "個人"
      tell journal "業界動向"
        tell journal "音楽配信"
          tell journal "津田大介"
            set jRes to make new journal entry
            
tell jRes
              set plain text content to pureText
            end tell
          end tell
        end tell
      end tell
    end tell
  end tell
end tell

シェルソートで入れ子のリストを昇順ソート
on shellSortListAscending(a, keyItem)
  set n to length of a
  
set cols to {1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1}
  
repeat with h in cols
    if (h (n - 1)) then
      repeat with i from h to (n - 1)
        set v to item (i + 1) of a
        
set j to i
        
repeat while (j h) and ((contents of item keyItem of item (j - h + 1) of a) > (item keyItem of v))
          set (item (j + 1) of a) to (item (j - h + 1) of a)
          
set j to j - h
        end repeat
        
set item (j + 1) of a to v
      end repeat
    end if
  end repeat
  
return a
end shellSortListAscending

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/11 twitter検索結果のJSONをparseする

twitter検索のページを見てみたら、さまざまな結果の受け取り方を行えるとのこと。

ひととおり試してみたら、JSON(JavaScript Object Notation)という形式のデータが、きわめてAppleScriptのレコード形式に似ていたので、

  「ちょっとだけ加工すればレコードになるはず!」

と、汎用性については完全に無視して、このケースのみ変換できるように書いてみました。海外のMLで、テキストのペアからレコードを作るAppleScriptについては、いろいろとサンプルが投稿されていたので、その中から使えそうなコードを拾ってきてちゃんと動くように手直しすれば、それなりに汎用性を持たせられそうな気もするのですが、それはまた別の機会ということで。

とりあえず、JSONのテキストを文字置換でレコード形式にrun scriptコマンドで強引に変換してみました。

AppleScriptの内部形式になってしまえばこちらのもの、あとは煮るなり焼くなり、MindJet MindManager上でビジュアル化したり、ExcelやFileMaker Proに突っ込んで再利用するなり、好きにデータ処理できます。

……ただ、このページのJSON形式の返り値は……このままだと50件しか返ってこないので、もうちょっとなんとかしたいところです。あと、「&」などの記号は実体参照で別の文字に置換されているらしいので、そのあたりの処理もやっておくべきでしょう。

スクリプト名:twitter検索結果のJSONをparseする
set searchKey to ぴよまる

set sKey to encodeURL(searchKey) of me
set aPath to (path to temporary items from user domain as text)
set dPath to aPath & test.txt as Unicode text

try
  with timeout of 30 seconds
    tell application URL Access Scripting
      set aURL to http://pcod.no-ip.org/yats/search?query= & sKey & &json
      
activate
      
download aURL to file dPath with progress
    end tell
  end timeout
on error
  display dialog Download Error
  
return
end try

set aData to read file dPath as «class utf8»
tell application Finder to delete file dPath

超やっつけで置換してみた。もっといい方法はいっぱいあるはず
set a1Dat to repChar(aData, [“, {“) of me
set a2Dat to repChar(a1Dat, ]“, }“) of me
set a3Dat to repChar(a2Dat, string id 10, “”) of me
set a4Dat to repChar(a3Dat, \”url\”:“, urlStr:“) of me
set a5Dat to repChar(a4Dat, \”image\”:“, imageURL:“) of me
set a6Dat to repChar(a5Dat, \”time2\”:“, time2:“) of me
set a7Dat to repChar(a6Dat, \”content\”:“, contentStr:“) of me
set a8Dat to repChar(a7Dat, \”user\”:“, userName:“) of me
set a9Dat to repChar(a8Dat, \”time\”:“, time1:“) of me
set a10Dat to repChar(a9Dat, \”id\”:“, idStr:“) of me
なんかもっといい方法が

set a11Dat to return & a10Dat
set aRes to run script a11Dat ここだけは、まあそんなもんだろうという処理

aRes

文字置換ルーチン
on repChar(origText, targStr, repStr)
  set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
  
set temp to text items of origText
  
set AppleScript’s text item delimiters to repStr
  
set res to temp as text
  
set AppleScript’s text item delimiters to txdl
  
return res
end repChar

on encodeURL(str)
  return do shell script (”python -c \”import sys, urllib; print urllib.quote(sys.argv[1]) \” as Unicode text) & quoted form of str
end encodeURL

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/11 日本語の指定キーワードでtwitter検索

指定キーワードをtwitterの日本語検索サービスに投げて、検索を行うという(よくある)AppleScriptです。

例によって、日本語のパラメータをURL Encodingしています。

twitt0.jpg

ためしに「ぴよまる」で検索……

twitt1.jpg

知っている方からお初の方まで、いろいろ出てきますね……

twitterの活用方法がだんだん分ってきたような気がします。

スクリプト名:日本語の指定キーワードでtwitter検索
set aText to text returned of (display dialog "検索対象キーワードを入力" default answer "")
set encText to encodeURL(aText) of me

open location "http://pcod.no-ip.org/yats/search?query=" & encText

on encodeURL(str)
  return do shell script ("python -c \"import sys, urllib; print urllib.quote(sys.argv[1]) \" " as Unicode text) & quoted form of str
end encodeURL

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

12/27 単純ダウンロード

指定のURLからファイルをダウンロードするサンプルです。

# Mac OS X 10.7では、URL Access Scriptingが廃止になったので、本Scriptはそのままでは動きません
(more…)

12/27 与えられたURLからファイル名を取り出す

URLのテキストから、ファイル名の部分を抽出するサブルーチンです。
(more…)

07/28 iCalに「日本の祝日」を追加

iCalに、「Japanese Holidays」というAppleが配布しているWebcalの登録確認を行い、登録していない場合には登録するという処理を行います。

一部、GUI Scriptingによる処理を含んでいるため、Mac OS X 10.6以降でそのまま動くという保証はまったくないのですが、自前で日本の祝日を計算しなくてもiCalに問い合わせるだけでいい(しかも、祝日が増えた場合でも自動対応してくれる)ため、実装実験を行ってみたものです。

結局、このルーチンは実戦投入されませんでしたが、何かの折に使ってみたいものです。

スクリプト名:iCalに日本の祝日を追加
set aRes to addJapaneseHolidays() of iCalLib

script iCalLib
  iCalに「日本の休日」のwebcalを追加する
  
on addJapaneseHolidays()
    OSバージョンの確認を行う
    
set v2 to system attribute sys2 > 4/5
    
if v2 < 4 then return false 10.3や10.2などであれば実行禁止
    
    
GUI scriptingの有効チェックを行う
    
set guiRes to retGUIScriptingEnabled() of me
    
if guiRes = false then return false
    
    
repeat
      using terms from application iCal
        tell application iCal
          set cList to name of every calendar
          
          
if Japanese Holidays is not in cList then
            インターネット接続確認
            
set a to connection_check() of me
            
if a = false then return false
            
            
ignoring application responses
              GetURL webcal://ical.mac.com/ical/Japanese32Holidays.ics
            end ignoring
          else
            return true
          end if
        end tell
      end using terms from
      
      
activate application iCal
      
tell application System Events
        tell process iCal
          tell window 1
            tell sheet 1
              click button 2
            end tell
            
            
repeat
              delay 1
              
tell sheet 1
                set bCount to count every button
                
if bCount is not equal to 1 then exit repeat
              end tell
            end repeat
            
            
tell sheet 1
              if v2 = 4 then
                Mac OS X 10.4の場合
                
set value of checkbox 1 to 1
                
set value of checkbox 2 to 1
                
                
click button 2 of list 1 OKボタン
              else if v2 = 5 then
                Mac OS X 10.5の場合
                
tell list 1
                  set value of checkbox 1 to 1
                  
set value of checkbox 2 to 1
                  
set value of checkbox 3 to 1
                end tell
                
                
click button 1 OK
                
              else
                return false 10.6などの想定外のバージョンであった場合    
              end if
            end tell
          end tell
        end tell
      end tell
    end repeat
  end addJapaneseHolidays
  
  
on connection_check()
    try
      tell application System Events
        do shell script sbin/ping -c 1 www.google.com
      end tell
      
set the connection_status to true
    on error
      set the connection_status to false
    end try
    
return connection_status
  end connection_check
  
  
GUI Scriptingの設定判定。10.4/10.5対応
  
on retGUIScriptingEnabled()
    set v2 to system attribute sys2 > 4, 5
    
    
Mac OS X 10.4以上なら実行
    
if v2 > 3 then
      tell application System Events
        set anUIe to UI elements enabled
      end tell
      
      
if anUIe = true then
        return true UI Element Scripting (GUI Scripting)がイネーブルならtrueを返す
      else
        beep
        
display dialog UI Element Scriptingが有効になっていません。 & return & return & 「システム環境設定」の「ユニバーサルアクセス」で、「補助装置を使用可能にする」のチェックボックスにチェックを入れてから再実行してください。 with icon stop
        
if button returned of result is OK then
          tell application System Preferences
            activate
            
set current pane to pane com.apple.preference.universalaccess
          end tell
          
return false
        end if
      end if
    end if
  end retGUIScriptingEnabled
end script

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/14 ヨドバシ.comのWebの画面に表示されている200×200ピクセル以上の画像をダウンロードする v2

ヨドバシカメラで大量に物品購入を行う際に、申請のあった製品の審査を行うため、データベースに各製品のヨドバシ.com上のURLを溜め込んでいました。そこで、せっかくURLが集まったんだからと、AppleScriptでプログラムを書いて、製品画像だけを抽出して指定フォルダにダウンロードさせてみました。本来は、FileMaker ProのDBに対してアクセスする部分とペアになっているのですが、そのURLと申請者IDを取り出した直後の状態からの処理がこのルーチンです。URLを2つ処理しているのは、1申請者あたり2製品までというスペックで申請を受け付けた事情によるものです。
(more…)

04/22 Safariの選択部分をGoogle Earthで開く

Webサイト上にGoogle Earthの位置情報ファイルkmlの内容が貼付けられているような場合に、それを選択しておくと、kmlファイルに書き出してGoogle Earthでオープンするというサンプルです。

earth1jpeg.jpeg

earth2.jpeg

スクリプト名:Safariの選択部分をGoogle Earthで開く
tell applicationSafari
  set a to do JavaScriptunescape(getSelection())in document 1
end tell

set a to a as Unicode text
if a = “” then return

set dPAth to path to desktop
set tmpOut to (dPAth as string) & “temp.kml
write_to_file(a, tmpOut, false) of me
saveInEncoding(a, tmpOut, “UTF8“) of meUTF8

do shell scriptsync

tell applicationGoogle Earth
  activate
  
open file tmpOut
end tell

指定の文字エンコーディングでファイル書き出し
on saveInEncoding(aText, aPath, aEncoding)
  
  
set a to aText as Unicode text
  
  
set aTmp to path to temporary items
  
  
set outFile to (aTmp as string) & “temporary_utf16be_text_fromAS.txt
  
write_to_file(a, outFile, false) of me
  
  
do shell scriptsync
  
  
set outPPath to quoted form of POSIX path of (outFile as alias)
  
set cnvPPath to quoted form of POSIX path of aPath
  
  
try
    do shell scripticonv -f UTF-16BE -t ” & aEncoding & “ ” & outPPath & “ > ” & cnvPPath
    –
do shell script “iconv -f UTF-16LE -t ” & aEncoding & ” ” & outPPath & ” > ” & cnvPPath
  end try
  
  
do shell scriptsync
  
  
try
    do shell scriptrm -f ” & outPPath
  end try
  
end saveInEncoding

ファイルの追記ルーチン「write_to_file」
追記データ、追記対象ファイル、boolean(trueで追記)
on write_to_file(this_data, target_file, append_data)
  try
    set the target_file to the target_file as text
    
set the open_target_file to open for access file target_file with write permission
    
if append_data is false then set eof of the open_target_file to 0
    
write this_data to the open_target_file starting at eof
    
close access the open_target_file
    
return true
  on error error_message
    try
      close access file target_file
    end try
    
return error_message
  end try
end write_to_file

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

04/22 Safariの選択部分のテキストを取得する

Safariの再前面のウィンドウで選択中のテキストを取得するサンプルです。アプリケーション上で選択中のテキストを取得して処理するというのは、AppleScript対応アプリケーションに期待したい機能です。ただ、アプリケーション開発者の無理解などによりそれが実装されていないケースもあります。

まさにSafariがそれに該当するもので……本来ならお手上げ状態なのですが、SafariがJavaScriptを実行できるためにウラワザ的に選択部分を取得できる、というお話です。

スクリプト名:Safariの選択部分のテキストを取得する(新)
tell applicationSafari
  set a to do JavaScriptunescape(getSelection())in document 1
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

04/22 Safariの最新のダウンロード状況を取得する

Safariのダウンロードリストを監視して最新のダウンロード状況を取得します。途方もなく大きなアーカイブをダウンロードするとか、アクセスが集まりすぎてダウンロードに時間がかかるような場合に、職場のマシンでダウンロードさせつつ(とっとと)帰宅したい。そんなときに、Safariのダウンロード状況を定期的に確認しつつ……完了したらメールで知らせてマシンをスリープさせる……という即席システムを作って運用しました。

dl_statjpeg.jpeg

スクリプト名:Safariの最新のダウンロード状況を取得する
getLatestDLstat() of me

Safariの最新のダウンロード状況を取得する
on getLatestDLstat()
  –activate application “Safari”
  
tell applicationSystem Events
    tell processSafari
      – insert GUI Scripting statements here
      
set wList to name of every window
      
ifダウンロードis not in wList then return “”
      
      
tell list 1 of scroll area 1 of windowダウンロード
        set lastG to last group
      end tell
      
      
tell lastG
        set sCount to count every static text
        
set dispTxt to “”
        
repeat with i from 1 to sCount
          set aVal to value of static text i
          
set dispTxt to dispTxt & aVal & return
        end repeat
      end tell
    end tell
  end tell
  
return dispTxt
end getLatestDLstat

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

04/22 Safariでタイムアウト時間を指定してURLを開く

Safariで指定のURLをオープンするサンプルです。オープンした後にページのローディングが完了するまで待ちますが、指定時間以内に完了しなかったらエラーとします。よくある処理です。

スクリプト名:SafariでURLをローディング
set aURL tohttp://www.piyocast.com/as/
set aURL to “”

tell applicationSafari
  set d to count every window
  
if d = 0 then
    make new window
    
tell document 1
      set URL to aURL
    end tell
  else
    if aURL is not equal to “” then
      tell document 1
        set URL to aURL
      end tell
    end if
  end if
  
  
page_loaded(10) of me
  
  
set a to do JavaScriptdocument.titlein document 1
end tell

on page_loaded(timeout_value)
  delay 2
  
repeat with i from 1 to the timeout_value
    tell applicationSafari
      if (do JavaScriptdocument.readyStatein document 1) iscompletethen
        return true
      else if i is the timeout_value then
        return false
      else
        delay 1
      end if
    end tell
  end repeat
  
return false
end page_loaded

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

04/13 URL選択ダイアログをオープン

choose URLコマンドでローカルネットワーク上にあるWeb/FTP/Telnet/File/News/Directory/Mediaサーバーおよびリモートアプリケーションを選択します。最近のネットワークプリンタは、Web経由でステータスを見られるようになっているようで、EPSONのプリンタPM-T960の情報をWebブラウザで見ることができました。

リモートアプリケーションについて、最近はリモートのAppleEventを受信できないようにしているアプリケーションもあり(FileMaker Proなど)すべてのアプリケーションをLAN経由でリモート制御できるわけではありませんが、リモートAppleEventはひじょうに有用性の高い機能です。

url1.jpg

url3.jpg

url2.jpg

スクリプト名:URL選択ダイアログをオープン
set aURL to choose URL
open location aURL

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に