英日翻訳を便利に使いたい(スクリプトが入りきらず前半です) [AppleScript辞書はつくれるか?]
(* inputMode =>
1:ダイアログから入力 / 2:テキストエディットの選択部分から *)
set inputMode to 2
(* outputMode =>
1:クリップボードへ / 2:ダイアログで表示 / 3:テキストエディターの選択部分へ上書き *)
set outputMode to 3
(* 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
(* WebBrowserのSafariの起動の確認 *)
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
コメント 0