英日翻訳を便利に使いたい(後半です) [AppleScript辞書はつくれるか?]
(* 正しく開いたか確認をする *)
if not (aSource contains "</html") then
-- |読み込みの失敗が発生した場合は結果エリアのログ表示と、ダイアログを表示して終了します|
return "翻訳サイトが確認できませんでしたので、終了します"
end if
end tell
if aText = "" then return "false : 1"
if keyText = false then return "false : 2"
log ("翻訳:" & keyText & return & "語句:『" & aText & "』")
(* 1行ごと翻訳していく処理 *)
set allText to ""
repeat with oneLine in (every paragraph of aText)
set oneLine to oneLine as text
if oneLine ≠ "" then
(* URLエンコード処理 *)
-- | 文章中にシングルクオートがあると不具合を起こすため回避 |
set aKey to "4649314"
if oneLine contains "'" then
set lastDelimit to AppleScript's text item delimiters
set AppleScript's text item delimiters to "'"
set obj to every text item of oneLine
set AppleScript's text item delimiters to aKey
set oneLine to obj as text
set AppleScript's text item delimiters to lastDelimit
end if
-- | %エンコードに変換 |
set urlencode to do shell script ("php -r 'echo rawurlencode(" & quote & oneLine & quote & ");'")
-- | 不具合部分の回復 |
if oneLine contains aKey then
set lastDelimit to AppleScript's text item delimiters
set AppleScript's text item delimiters to aKey
set obj to every text item of oneLine
set AppleScript's text item delimiters to "'"
set oneLine to obj as text
set AppleScript's text item delimiters to lastDelimit
end if
(* Google翻訳サイトにテキストを送信 *)
tell application "Safari"
if keyText = "英 > 日" then set keyUrl to "&sl=en&tl=ja"
if keyText = "日 > 英" then set keyUrl to "&sl=ja&tl=en"
set URL of document focusDocument to ¬
"https://translate.google.co.jp/?hl=ja#view=home&op=translate" & keyUrl & "&text=" & urlencode
end tell
(* サイト側の翻訳作業が終了しているかの判断 *)
tell application "Safari"
activate
--| ページソースが完全に読み込まれたか確認 |
repeat 30 times
try
if ((source of document focusDocument) contains "</html") then exit repeat
end try
delay 0.5
end repeat
--| 翻訳が途中になっていないか確認 |
repeat 10 times
set resultText to text of document focusDocument
if not ((resultText contains "...") or (resultText contains "訳文")) then exit repeat
delay 0.5
end repeat
end tell
(* 翻訳後の必要なテキストを抽出 *)
set c to offset in resultText of "/5000"
set resultText to text (c + 6) thru -1 of resultText
set resultText to paragraph 1 of resultText
set allText to allText & resultText & return
end if
end repeat
--| 不要な場合末尾の改行は削除 |
try
if ((character -1 of aText) ≠ (ASCII character 10)) and ((character -1 of aText) ≠ (ASCII character 13)) then
set allText to text 1 thru -2 of allText
end if
end try
(* 出力処理をする *)
-- | outputMode 1:クリップボードへ / 2:ダイアログで表示 / 3:テキストエディターの選択部分に上書き |
-- | outputModeが3でもinputModeが2(テキストエディタ経由)でなかった場合は、outputModeを2に変更 |
if outputMode = 3 then
if inputMode = 2 then
-- | テキストエディターへの上書き |
set the clipboard to allText
activate application "TextEdit"
delay 0.3
tell application "System Events" to keystroke "v" using command down
set outputMode to 0
set the clipboard to clipboardData -- |保護したクリップボードデータの復帰|
else
set outputMode to 2 --入力がダイアログモードの場合は、出力もダイアログモードに変更
end if
end if
if outputMode = 1 then
-- | 結果をクリップボードにコピー |
set the clipboard to allText
else if outputMode = 2 then
-- | 結果をダイアログで表示 |
activate
set buttonResult to button returned of (display dialog "翻訳データ:" default answer allText ¬
buttons {"キャンセル", "クリップボードへコピー", "OK"} default button 2)
if buttonResult = "OK" then
set the clipboard to allText
else
set the clipboard to clipboardData -- |保護したクリップボードデータの復帰|
end if
end if
(* 最後に結果をログに表示 *)
log "翻訳が終了しました。"
"翻訳結果--------------------" & return & allText & return & "------------------------------ "
コメント 0