Skip to content

Instantly share code, notes, and snippets.

@zarigani
Created June 6, 2009 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zarigani/125007 to your computer and use it in GitHub Desktop.
Save zarigani/125007 to your computer and use it in GitHub Desktop.
sayEB AppleScript
(*
このスクリプトは、表計算ソフトに入力された全銀データの読み合わせを行います。
このスクリプトを動かすために必要なもの
◆SayKana
SayKana(Mac版 日本語音声合成ソフト)が/usr/local/bin/以下にインストールされている必要があります。
http://www.a-quest.com/aquestalk/saykana/
◆AppleScriptライブラリ
依存するAppleScriptライブラリを以下のページからダウンロードしておく必要があります。
http://github.com/zarigani/AppleScript-bebe-s-Library/tree/master
そして、以下のファイルをユーザースクリプトフォルダ(~/Library/Scripts)にインストールしてください。
_lib.scpt
_gui.scpt
*)
property LIB : load script file ((path to scripts folder as text) & "_lib.scpt")
property GUI : load script file ((path to scripts folder as text) & "_gui.scpt")
--文字列中の置き換えで利用
property KIGO : "/()"
property YOMI : {"、すら’っしゅ、", "、か’っこ、", "、か’っことじ、"}
--セル単位の置き換えで利用
property ZENGIN_RYAKUGO : {{"1", "ふつうよ’きん"}, {"2", "とうざよ’きん"}, {"9", "その’た"}, {"カ", "かぶしきが’いしゃ"}, {"ユ", "ゆうげんが’いしゃ"}, {"ザイ", "ざいだんほ’ーじん"}, {"シャ", "しゃだんほ’うじん"}, {"ソ", "そうごが’いしゃ"}, {"エイ", "えいぎょうしょ"}, {"シユツ", "しゅっちょうしょ"}}
property is_ZENGIN_reading : true
GUI's init()
GUI's shortcut("", "command-c")
LIB's message("SayEB", "読み取りを開始しました...")
set a_list to LIB's do_ruby_jcode_u("'" & (the clipboard as string) & "'" & ".gsub(/\\t+[\\n\\r]/,\"\\r\")") --右側の余分な空白セルを削除
set a_list to LIB's every_split(a_list, {"\n", "\r"}) --改行でリストに変換
--set a_list to LIB's reject_item(a_list, "")--空文字リストを削除する
zengin_check(a_list)
set is_ZENGIN_reading to reading_select() = "全銀データとして読む"
set i to 0
repeat with a_line in a_list
set i to i + 1
say_line(a_line, i)
end repeat
LIB's message("SayEB", "完了しました。")
--1行を空白セルで区切った範囲リストに変換
--次の空白セルまで読んで、次を読む指示待ち
on say_line(a_line, num)
set ranges to LIB's every_split(a_line, {tab & tab})
set complete to ""
repeat with a_range in ranges
repeat
say_range(a_range)
set complete to complete & "\"" & LIB's every_replace(a_range, {tab}, {" "}) & "\"" & return
if one_stop(complete, num) = "次を読む[return]" then exit repeat
end repeat
end repeat
end say_line
--1範囲をtabと()で区切ったリストに変換
--口座種と略語を変換した後、「、」で区切ったテキストに戻す
on say_range(a_range)
if is_ZENGIN_reading then
set kugiri to "()"
else
set kugiri to ""
end if
set word_list to LIB's every_split(a_range, tab & kugiri)
repeat with a_word in word_list
if is_ZENGIN_reading then
LIB's look_up(ZENGIN_RYAKUGO, a_word's contents) as text --口座種と略語を変換
if result ≠ "" then set a_word's contents to result
end if
end repeat
say_text(LIB's join(word_list, "、"))
end say_range
--saykanaで発声する
--マイナス、長音記号、ハイフンの使い分けをする
--半角カナを全角カナに変換する
on say_text(str)
LIB's every_replace(str, {tab, "\"", "'"}, {"、", "”", "’"})
LIB's do_ruby_jcode_u("('" & result & "'" & ".gsub(/(\\d)(?=(?:\\d\\d\\d)+(?!\\d))/, '\\1,')" & " if /^-?\\d+$/ =~ " & "'" & result & "')" & "|| '" & result & "'") --数値を3桁区切りにする
LIB's do_ruby_jcode_u("'" & result & "'" & ".gsub(/([^0-9]),([^0-9])|([0-9]),([^0-9])|([^0-9]),([0-9])/,'\\1\\3\\5か’んま\\2\\4\\6')")
LIB's do_ruby_jcode_u("'" & result & "'" & ".gsub(/(^|\\s)[-]([0-9])/,'\\1まいなす\\2')")
LIB's do_ruby_jcode_u("'" & result & "'" & ".gsub(/([ァ-ン])[-ー]([ァ-ン])/,'\\1ー\\2')")
LIB's do_ruby_jcode_u("'" & result & "'" & ".gsub(/([^\\^\\sァ-ン])[-]([^ァ-ン])/,'\\1は’いふん\\2')")
LIB's do_ruby_jcode_u("'" & result & "'" & ".gsub(/(^|\\s)[-]([^0-9ァ-ン])/,'\\1は’いふん\\2')")
LIB's do_ruby_jcode_u("'" & result & "'" & ".gsub(/ /,'、ぜんかくすぺ’ーす、')")
LIB's do_ruby_jcode_u("'" & result & "'" & ".gsub(/ (.)/,'\\1’')")
LIB's every_replace(result, KIGO, YOMI)
set voice to LIB's kana_han2zen(result)
do shell script "/usr/local/bin/saykana " & quoted form of voice & "> /dev/null 2>&1 &" --デーモンで実行、戻り値は無視する必要あり
end say_text
--次を読む指示待ち
on one_stop(msg, num)
activate
"■■■ No." & num & " ■■■" & return & msg
--display dialog result buttons {"キャンセル", "もう一度繰り返す", "次を読む[return]"} default button 3
display alert result buttons {"キャンセル", "もう一度繰り返す", "次を読む[return]"} default button 3 cancel button 1
result's button returned
end one_stop
--全銀フォーマット対応文字かどうかのチェック
--対応文字は以下の通り
-- 数字の0から9 英大文字のA-Z 半角カタカナ大文字と濁点と半濁点のア-ン゙゚
-- ピリオド. 括弧() マイナスハイフン- スラッシュ/ 半角スペース
on zengin_check(a_list)
set i to 0
set err_list to {}
repeat with a_row in a_list
set i to i + 1
if not (LIB's reg("/^[\\s0-9A-Zア-ン゙゚.()\\-\\/]+$/", a_row)) then
set err_list to err_list & (("■■" & i & ". " & a_row) as text)
end if
end repeat
if err_list's number > 0 then
beep
activate
"■■■全銀フォーマット規格外の文字を含んでいます。\n\n" & LIB's every_replace(LIB's join(err_list, "\n"), "\t", " ")
display alert result buttons {"キャンセル", "続ける"} default button 1 cancel button 1 as warning
if result's button returned = "キャンセル" then
LIB's message("SayEB", "読み上げを中止しました。")
error number -128 --スクリプトを中止する
end if
end if
end zengin_check
--全銀フォーマットとして読むかどうか選択する
on reading_select()
activate
"準備が整いました。\nreturnキーを押すごとに、1件ずつ読み上げます。"
--display dialog result buttons {"キャンセル", "そのまま読む", "全銀データとして読む"} default button 3
display alert result buttons {"キャンセル", "そのまま読む", "全銀データとして読む"} default button 3 cancel button 1
result's button returned
end reading_select
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment