SSブログ

英日翻訳を便利に使いたい(スクリプトが入りきらず前半です) [AppleScript辞書はつくれるか?]

まったくアメリカ語がちんぷんかんぷんな私は、翻訳する作業が頻繁すぎるのでAppleScriptで翻訳作業の効率化をしたいと思うに至りました。

グーグル先生の翻訳って、少し前に今の新しいインターフェースに変わったのですが微妙に使いにくい。
というのも、翻訳元の入力エリアが選びにくい。選んだつもりが枠のオブジェクトだったりとか変な仕様。

まあ、以前からそんな翻訳するためのプログラミングは作っていましたが、もっと使い勝手を良くしたいと思ったわけです。

・・・で、作りました。

image190816.jpg


ダイアログ入力またはテキストエディットの選択部分から入力。

(* inputMode => 

1:ダイアログから入力 / 2:テキストエディットの選択部分から *)

set inputMode to 2


翻訳語は、ダイアログか、クリップボード。またはテキストエディットの選択部分に上書き

(* outputMode => 

1:クリップボードへ / 2:ダイアログで表示 / 3:テキストエディターの選択部分へ上書き *)

set outputMode to 3


を選べるようにした。

グーグル先生翻訳を自動化するにあたって、他のサイトでは無い特徴がある。

それは、アドレス部分に翻訳文字を送れば翻訳ができること。わざわざ、翻訳のテキストフィールドに文字を打ち込むための作業が不要であること。

これはデカイです。あそこに送り込むためのスクリプトが面倒だったのです。

ただ、アドレス部分に翻訳文字を送るのはこれはこれで面倒。

英語の単語だけならそんなに問題はないのかもしれないけど、英>日、日>英の両方に対応や記号やスペースがそのままでは使えない。

『Programming is difficult.』を日本語に訳したいのだけど


と入力しても翻訳が失敗する可能性がある。アドレスに入れる場合は、URLエンコード(%エンコードとも呼ばれる)の処理が必要になってしまう。でも、AppleScriptだけではエンコードができないので、PHPをブリッジして助けてもらわなければいけない。(他にもやり方があるかもしれないけど、一番手軽。)

do shell script ("php -r 'echo rawurlencode(" & quote & "Programming is difficult." & quote & ");'")

を使用する。


スペースの部分が『%20』と変更されている。(日本語だともっと複雑)

ちなみに『translate&sl=en&tl=ja』の部分を逆の『translate&sl=ja&tl=en』してやると、日本語から英語に翻訳するモードになる。

注意するところはそれだけでしたね。

あとは、どうしてもWebブラウザのSafariを開かなければいけないので、もろもろのプログラミングをしております。

最初に自動でSafariを起動して翻訳ページを準備しているのだけど、たまにページを開くのが遅延して失敗したりします。その辺はあらかじめSafariを起動しておくと失敗しにくいようです。

 

 
 

-----スクリプト(前半)-----

(* inputMode => 

1:ダイアログから入力 / 2:テキストエディットの選択部分から *)

set inputMode to 2

 

(* outputMode => 

1:クリップボードへ / 2:ダイアログで表示 / 3:テキストエディターの選択部分へ上書き *)

set outputMode to 3

 

 

 

(* 状況の表示 *)

log "翻訳開始"

if inputMode = 1 then log "入力モード : ダイアログ入力"

if inputMode = 2 then log "入力モード : テキストエディットで選択された部分から読み込み"

 

if outputMode = 1 then log "出力モード : クリップボードへ出力"

if outputMode = 2 then log "出力モード : ダイアログで表示 "

if outputMode = 3 then

if inputMode = 1 then log "出力モード : ダイアログで表示(に変更)"

if inputMode = 2 then log "出力モード : テキストエディットの選択部分に上書き"

end if

 

set {keyText, aText} to {false, ""}

if inputMode = 1 then

(* ダイアログを表示して入力を促す *)

try

set {text returned:aText, button returned:buttonText} to ¬

(display dialog "入力" default answer (return & return) buttons {"キャンセル", " > ", " > "})

if buttonText ≠ "キャンセル" then set keyText to buttonText

end try

else if inputMode = 2 then

(* テキストエディットで選択されている部分をクリップボードにコピー *)

set the clipboard to ""

activate application "TextEdit"

delay 0.2

tell application "System Events"

tell process "TextEdit"

keystroke "c" using command down

keystroke "c" using command down

end tell

end tell

delay 0.1

set copyText to the clipboard

 

(* テキストをプレーンテキストに最適化 *)

set aText to ""

if (class of copyText) = record then

try

set aText to text of copyText

on error

try

set aText to copyText as Unicode text

end try

end try

else

set aText to copyText as Unicode text

end if

if aText = "" then

set keyText to false

else

(* 日>英 か 英>日 を判断 *)

set keyText to " > "

-- | Characterコードに255を超える文字が含まれていた場合は、日本語だと判断 |

repeat with oneChara in (every character of aText)

if (id of oneChara) > 255 then

set keyText to " > "

exit repeat

end if

end repeat

end if

end if

if keyText = false then return "false : 入力キャンセル または エラーで終了"

 

(* 可能ならクリップボードデータの保護 *)

try

set clipboardData to the clipboard

on error

set clipboardData to ""

end try

 

(* WebBrowserSafariの起動の確認 *)

if application "Safari" is not running then

set flg to false

activate application "Safari"

delay 1

repeat 20 times

if application "Safari" is running then

set flg to true

if (count of every window) = 0 then

make new document

delay 0.5

end if

exit repeat

end if

delay 0.5

end repeat

if flg is false then

log "Safariが正常に起動しませんでした。最初からやり直してください。"

return "Safari : 不可"

end if

end if

log "Safari : "

 

(* 翻訳サイトが開いているかの確認 *)

tell application "Safari"

activate

set flg to false

set focusDocument to 0

set c to count of (every document)

set aUrl to ""

if c > 0 then

repeat with i from 1 to (count of (every document))

try

set aUrl to URL of document i

end try

if aUrl = "missing value" then

if focusDocument = 0 then set focusDocument to i

else

if aUrl contains "translate.google.co.jp" then

set focusDocument to i

set flg to true

exit repeat

end if

end if

end repeat

end if

 

(* 無ければ、新しいウインドウを開く *)

if flg = false then

try

set aUrl to (URL of document focusDocument) as text

if aUrl = "missing value" then

-- | 白ページを使用 |

set URL of document focusDocument to "http://translate.google.co.jp/"

else

-- | 白ページがなければ |

make new document

set focusDocument to 1

delay 0.5

set URL of document focusDocument to "http://translate.google.co.jp/"

end if

on error

-- | ページが1枚も開いていなければ新しく作成 |

make new document

set focusDocument to 1

delay 0.5

set URL of document focusDocument to "http://translate.google.co.jp/"

end try

delay 0.5

end if

 

(* 翻訳ページのリセットする *)

set aSource to ""

repeat 30 times

try

set aSource to source of document focusDocument

set aPageText to text of document focusDocument

if (aSource contains "</html") and ((count of (every paragraph of aPageText)) < 15) then exit repeat

--ページソースの読み込みが終わっているかを '</html' の有無で判断

--ちゃんとリセットされているかを、表示されたテキストの行数が15行未満になっているかどうかで判断

end try

delay 0.2

end repeat

 

 



nice!(1)  コメント(0) 
共通テーマ:パソコン・インターネット

nice! 1

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。