Archive for the 'URL Access Scripting' Category
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 |
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in インターネット(internet), アプリケーション操作(app control), URL Access Scripting, 10.5対応, 10.6対応, 10.4対応 | No Comments »
Twitterrifficで選択中の発言が行われた時間をGrowlでアイコン付き表示するAppleScriptの改良版です。
URL Access Scriptingに指定するURLのうちファイル名に該当する部分の長さが32文字以上だと、勝手にダウンロード時にファイル名を丸められてしまう、という問題に対処するため、28文字でファイル名チェックを行い、短く付け替えています。
ちょうど、坂本龍一教授をFollowして、たまたま発言を見つけたので時刻を調べてみたらエラーに遭遇。ファイル名を調べてみたら37文字で、テンポラリフォルダに入っていた画像ファイルがすべて途中で丸められていることが判明。URL Access Scriptingの32文字制限に気付いて、対策を施したものです。
ダウンロード後に画像が真っ白になっているケースが見られたので、syncコマンドを実行してキャッシュの内容をHDDにシンクロさせています。
(more…)
Posted in インターネット(internet), アプリケーション操作(app control), URL Access Scripting, 10.5対応, Twitterrific, Growl | No Comments »
AppleScriptでインターネットからダウンロードを行う際に用いる「URL Access Scripting」に、ファイル名の32文字制限があることに気付きました。US Appleのメーリングリストの過去ログをあさってみても、「何かがあるらしい」とまでは2001年ごろに話題にはなっていたものの、具体的な症状まで明らかになることはありませんでした(「気のせいではないか?」といった論調)。
URLの長さについては、32文字以上を許容しますし、URLの後に&でパラメータを付けた場合にも文字列長の制限は(AppleScript的には)とくにないのですが(フォームの側のリクエスト長の制限はまた別として)、URLの中に含まれるファイル名の部分が32文字以上だと途中で切られてしまうようです。
32文字といえば、Classic Mac OSのファイル名の長さの制限値であり、その時代にはたしかにそうした制限に意味はあったのでしょうが、Mac OS Xの時代までその制限がある必要はありません。
その制限が、Mac OS X 10.5の時代まで引き継がれてきたこと自体に驚きを禁じ得ません。
Posted in URL Access Scripting, 10.5対応, How To | No Comments »
Twitterrifficからアイコンを取得して、ダウンロードを行おうとすると弾かれるケースが見られたので、とりあえず「すでにダウンロードしたアイコンがあれば使用する」ように処理を変更したバージョンです。
アイコンのダウンロードが行えない時に、TwitterアカウントのURLを個別にAppleScriptで取得して、そのままSafariで表示すると問題ないようなので、案外User Agent名称のチェックをサーバー側で行っているのかもしれません。
URL Access ScriptingにはUser Agent名称を詐称する機能はないため、そのための対策を行うのであればシェルからcurlコマンドを呼び出す必要が出てくることでしょう。
もう少し、エラーが発生する状況を観察してみることとします。
(more…)
Posted in インターネット(internet), アプリケーション操作(app control), URL Access Scripting, 10.5対応, Twitterrific, Growl | No Comments »
Safariで、Podcastingのバックナンバーをダウンロードしようとして……ダウンロードフォルダにファイルとしてダウンロードできず、なおかつSafari中のQuickTimeプラグインの保存コマンドも使えなかったので(何故?) その場でありあわせの部品を組み合わせて3分ぐらいで作ったAppleScriptです。
# Mac OS X 10.7では、URL Access Scriptingが廃止になったので、本Scriptはそのままでは動きません

(1)まず、Safariの最前面のウィンドウからURLを取得
(2)Safariのダウンロードフォルダの場所を取得
(3)(1)で取得したURLからファイル名部分を分離
(4)URL Access Scriptingで自前でダウンロード
という動作を行います(=という部品をかき集めてつなげただけです)。
| スクリプト名:Safariで表示中の保存不能なファイルをダウンロード |
tell application “Safari“ tell document 1 set aURL to URL end tell end tell
set dlFol to getSafariDownloadFolder() of me
set aFileName to getFNfromURL(aURL) of me
set dlPath to (dlFol as string) & aFileName
try with timeout of 3600 seconds tell application “URL Access Scripting“ activate download aURL to file dlPath with progress end tell end timeout on error display dialog “Download Error“ end try
–URLからファイル名のみを抽出 on getFNfromURL(aURL) if aURL ends with “/“ then display dialog “与えられたURLの末尾がスラッシュで終了しているため、ファイルではありません。“ buttons {”OK“} default button 1 return “” 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
–Safariのダウンロードフォルダを取得 –By Phillip Aker, Mark J Reed on getSafariDownloadFolder() set preferencesPath to POSIX path of (path to preferences) set bundleId to bundle identifier of (info for (path to application “Safari“)) set safariPrefsFile to preferencesPath & bundleId & “.plist“ tell application “System Events“ to set folderName to get value of property list item “DownloadsPath“ of property list file safariPrefsFile set downloadFolder to (POSIX file (do shell script “echo “ & folderName)) as alias return downloadFolder end getSafariDownloadFolder
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), Safari, URL Access Scripting, 10.5対応 | No Comments »
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
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in インターネット(internet), アプリケーション操作(app control), URL Access Scripting, 10.5対応 | No Comments »
URL Access Scriptingで指定URLのファイルをローカルのHDD上にダウンロードするサンプルです。AppleScriptでファイルのダウンロードを行う場合には、URL Access Scriptingで行うか、shell commandのcurlを用いるのが一般的です。
# Mac OS X 10.7では、URL Access Scriptingが廃止になったので、本Scriptはそのままでは動きません
ほかに、ユーザー認証を行う必要のある場所にファイルが存在している場合には、Safariをコントロールしてダウンロードしたりもします。同期処理を行いたい場合にはURL Access Scriptingを、非同期で多数のファイルを処理する場合にはcurlを使ったりしていますが、このあたりは個人的な「趣味」の問題でしょう。Mac OS 9の時代にはURL Access Scriptingの信頼性はいまひとつでしたが、Mac OS Xの時代になってからは、連続して稼働してもとくに問題は見られません。
| スクリプト名:URL Access ScriptingでDL |
–パスをstringでcastしたあとにunicodeにcastしているのはMac OS X 10.4までの仕様。10.5では必要ない set aPath to (path to desktop folder from user domain as text) set dPath to aPath & “test.ics” as Unicode text
try with timeout of 30 seconds tell application “URL Access Scripting“ activate set aURL to “http://homepage.mac.com/ical/.calendars/Japanese32Holidays.ics“ download aURL to file dPath with progress end tell end timeout on error display dialog “Download Error“ end try |
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in インターネット(internet), アプリケーション操作(app control), URL Access Scripting | No Comments »