翻訳の機能については、自前で何とかできるぐらいなら、そもそも翻訳ソフトなんか必要じゃないと思いますので、Googleの翻訳を利用させていただくこととしました。
また、改造も含め手軽にソースを見ていただくことも考え、今回はAppleScriptを使って実現しました。
AppleScriptで作成されています。MacOS専用です。
一応OS X 10.6.6から動くはずですが、試していません。
開発は、MacOS 10.14およびMacOS 10.15で行いました。
スクリプトエディタ.appでソースを開くことができます。
仕様
- 翻訳機能はGoogleの翻訳機能を利用する
- GoogleのApps Scriptを使い、翻訳のAPIとする
- AppleScriptでテキストのコピーから表示までを制御する
- できるだけ汎用性を持たす
翻訳の手順
- 翻訳をしたいテキストを選択する
- 翻訳ボタンを押す
- 選択されたテキストをクリップボードにコピー
- コピーしたテキストをURLエンコードする
- サファリを使い、Google Apps ScriptにURLを送信
- サファリに翻訳された文字をコピーする
- 翻訳文を表示する
まずはGoogleのApps Scriptについて
Googleが提供する無償のサービスで、Google スプレッドなどで利用することができます。
そのほかURLを叩くだけでAPIとして色々な活用ができます。
翻訳の詳細につきましては以下のサイトを参照させていただきました。
3 分で作る無料の翻訳 API with Google Apps Script
https://qiita.com/tanabee/items/c79c5c28ba0537112922
こちらはGoogleより提供されているものですので、未来永劫このままで利用できると言う保証はありません。ご理解の上ご利用ください。
またTranslatePDFに使われているGoogle Apps Scriptは筆者のアカウントで設置されたものです。何の気まぐれで消してしまうこともあり得ることをご了承ください。
また、アプリケーションはスクリプトエディタ.appでソースを開くことができます。自由にカスタマイズすることができます。
Google App Scriptなどもご自身で作成したものに変更していただいても結構です。
ただし、再保存をされた時点でオリジナルとは違うアプリケーションとなりますので、配布に関してはAppleからの公証を受けていただくことが必要となります。
ではスクリプトの全容を紹介いたします。(シンタックスコードカラーが変になっているのはご愛嬌ということでお願いします。直す方法を現在模索中です。)
(********************************
translatePDF
2020/02/17
Ver.1.0.1
product by tu-kazu
Free Ware
********************************)
(* 初期設定 *)
property prefPath_ : path to preferences folder as string
property prefFile_ : "TranslatePDF.pref"
property prefFilePath_ : prefPath_ & prefFile_
property defaultApplication_ : "Adobe Acrobat"
property sourceLanguage_ : "英語"
property targetLanguage_ : "日本語"
property sourceLanguageCode_ : "en"
property targetLanguageCode_ : "ja"
property prefContents_ : {"Adobe Acrobat", "英語", "日本語", "en", "ja"}
property APPLILIST_ : {"Adobe Acrobat", "Adobe Acrobat Reader DC", "Preview"}
property LANGUAGETYPE_ : {"アイスランド語", "アイルランド語", "アゼルバイジャン語", "アフリカーンス語", "アムハラ語", "アラビア語", "アルバニア語", "アルメニア語", "イタリア語", "イディッシュ語", "イボ語", "インドネシア語", "ウェールズ語", "ウクライナ語", "ウズベク語", "ウルドゥ語", "エストニア語", "エスペラント語", "オランダ語", "カザフ語", "カタルーニャ語", "ガリシア語", "カンナダ語", "ギリシャ語", "キルギス語", "グジャラト語", "クメール語", "クルド語", "クロアチア語", "コーサ語", "コルシカ語", "サモア語", "ジャワ語", "ジョージア(グルジア)語", "ショナ語", "シンド語", "シンハラ語", "スウェーデン語", "ズールー語", "スコットランドゲール語", "スペイン語", "スロバキア語", "スロベニア語", "スワヒリ語", "スンダ語", "セブアノ語", "セルビア語", "ソト語", "ソマリ語", "タイ語", "タガログ語", "タジク語", "タミル語", "チェコ語", "チェワ語", "テルグ語", "デンマーク語", "ドイツ語", "トルコ語", "ネパール語", "ノルウェー語", "ハイチ語", "ハウサ語", "パシュト語", "バスク語", "ハワイ語", "ハンガリー語", "パンジャブ語", "ヒンディー語", "フィンランド語", "フランス語", "フリジア語", "ブルガリア語", "ベトナム語", "ヘブライ語", "ベラルーシ語", "ペルシャ語", "ベンガル語", "ポーランド語", "ボスニア語", "ポルトガル語", "マオリ語", "マケドニア語", "マラーティー語", "マラガシ語", "マラヤーラム語", "マルタ語", "マレー語", "ミャンマー語", "モンゴル語", "モン語", "ヨルバ語", "ラオ語", "ラテン語", "ラトビア語", "リトアニア語", "ルーマニア語", "ルクセンブルク語", "ロシア語", "英語", "韓国語", "中国語", "日本語"}
property LANGUAGECODE_ : {"is", "ga", "az", "af", "am", "ar", "sq", "hy", "it", "yi", "ig", "id", "cy", "uk", "uz", "ur", "et", "eo", "nl", "kk", "ca", "gl", "kn", "el", "ky", "gu", "km", "ku", "hr", "xh", "co", "sm", "jw", "ka", "sn", "sd", "si", "sv", "zu", "gd", "es", "sk", "sl", "sw", "su", "ceb", "sr", "st", "so", "th", "tl", "tg", "ta", "cs", "ny", "te", "da", "de", "tr", "ne", "no", "ht", "ha", "ps", "eu", "haw", "hu", "pa", "hi", "fi", "fr", "fy", "bg", "vi", "iw", "be", "fa", "bn", "pl", "bs", "pt", "mi", "mk", "mr", "mg", "ml", "mt", "ms", "my", "mn", "hmn", "yo", "lo", "la", "lv", "lt", "ro", "lb", "ru", "en", "ko", "zh-CN", "ja"}
(* Main *)
on run
-- 初期設定
setPreference()
repeat
activate of me
display dialog "現在の設定" & return & "対象アプリ:" & defaultApplication_ & return & "ソース言語:" & sourceLanguage_ & return & "ターゲット言語:" & targetLanguage_ buttons {"終了", "初期設定", "翻訳"} default button 3
set ans to button returned of result
if ans is "翻訳" then
translate() of me
else if ans is "初期設定" then
chengePreference() of me
else if ans is "終了" then
exit repeat
end if
end repeat
-- 初期設定セーブ
set item 1 of prefContents_ to defaultApplication_
set item 2 of prefContents_ to sourceLanguage_
set item 3 of prefContents_ to targetLanguage_
set item 4 of prefContents_ to sourceLanguageCode_
set item 5 of prefContents_ to targetLanguageCode_
savePref((prefFilePath_ as alias), prefContents_) of me
end run
(* 翻訳 *)
on translate()
tell application defaultApplication_
-- クリップボードを初期化
set the clipboard to ""
activate
-- コマンドCを押下
tell application "System Events"
keystroke "c" using command down
end tell
delay 1
-- クリップボードからテキストを取得
if (count of (clipboard info for string)) > 0 then
set copyText to the clipboard
else
set copyText to ""
end if
if copyText is "" then
return
end if
-- テキストをURLエンコードする
tell application "System Events"
set scpt to "php -r 'echo rawurlencode(" & quote & copyText & quote & ");'"
set aString to (do shell script scpt) as string
end tell
-- URLを設定 Google Apps Scriptへ情報をGET送信
set theURL to "https://script.google.com/macros/s/AKfycbzXJK7esmNe4E0c8EU4YruColyMabARZqOlHQ08LjITgs0-stC_/exec?text=" & aString & "&source=" & sourceLanguageCode_ & "&target=" & targetLanguageCode_
-- SafariでURLを開きテキストを取得する
tell application "Safari"
-- 翻訳を取得できるまで繰り返し待つ
make new document
set theID to id of window 1
open location theURL
repeat
delay 1
tell document 1 to set safariTxt to every text
if not safariTxt = "" then
exit repeat
end if
end repeat
tell the window id theID to close
end tell
-- 翻訳を表示
display dialog safariTxt buttons {"閉じる"} default button 1
set the clipboard to ""
end tell
end translate
(* 初期設定 *)
-- 初期設定ファイル
on setPreference()
tell application "Finder"
set aBoo to exists of (prefPath_ & prefFile_)
if not aBoo then
set prefFilePath_ to make new file with properties {name:prefFile_} at alias prefPath_
savePref((prefFilePath_ as alias), prefContents_) of me
end if
set theFilePath to a reference to prefFilePath_
set prefContents_ to loadPref(theFilePath as alias) of me
set defaultApplication_ to item 1 of prefContents_
set sourceLanguage_ to item 2 of prefContents_
set targetLanguage_ to item 3 of prefContents_
set sourceLanguageCode_ to item 4 of prefContents_
set targetLanguageCode_ to item 5 of prefContents_
end tell
end setPreference
-- 初期設定書き込み
on savePref(theFile, theContents)
try
set defaultDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ASCII character 13
set prefTXT to theContents as string
set AppleScript's text item delimiters to defaultDelimiters
set fPointer to open for access theFile with write permission
set eof fPointer to 0
write prefTXT to fPointer
on error
return
end try
close access fPointer
end savePref
-- 初期設定読み込み
on loadPref(theFile)
try
open for access theFile
set theContents to read theFile as text using delimiter {return}
on error
set theContents to false
end try
close access theFile
return theContents
end loadPref
on chengePreference()
activate of me
display dialog "設定を変更する" buttons {"閉じる", "対象アプリ", "翻訳言語"} default button 1
set ans to button returned of result
if ans is "翻訳言語" then
display dialog "ソース言語を変更します" & return & "現在:" & sourceLanguage_ buttons {"OK", "Cancel"} default button 1
set ans to button returned of result
if ans is "OK" then
set res to selectLanguage(sourceLanguage_, "ソース言語を選択します")
if not res = "" then
set sourceLanguage_ to res as string
set sourceLanguageCode_ to selectCode(sourceLanguage_)
end if
end if
display dialog "ターゲット言語を変更します" & return & "現在:" & targetLanguage_ buttons {"OK", "Cancel"} default button 1
set ans to button returned of result
if ans is "OK" then
set res to selectLanguage(targetLanguage_, "ターゲット言語を選択します")
if not res = "" then
set targetLanguage_ to res as string
set targetLanguageCode_ to selectCode(targetLanguage_)
end if
end if
else if ans is "対象アプリ" then
defaultApplication() of me
end if
return
end chengePreference
(* 言語設定リスト選択 *)
on selectLanguage(defaultLanguage, withPrompt)
choose from list LANGUAGETYPE_ default items defaultLanguage with prompt withPrompt
return result
end selectLanguage
(* 言語タイプ設定 *)
on selectCode(defaultLanguage)
set aCount to 1
repeat with i in LANGUAGETYPE_
set j to contents of i as string
if defaultLanguage as string = j then
exit repeat
end if
set aCount to aCount + 1
end repeat
set defaultCode to item aCount of LANGUAGECODE_
return defaultCode as string
end selectCode
(* 対象アプリ *)
on defaultApplication()
activate of me
choose from list APPLILIST_ default items defaultApplication_ with prompt "対象アプリ"
set defaultApplication_ to result as string
end defaultApplication
だらだらと長ったらしく綴りましたが、翻訳の部分は数行です。
選択したテキストをクリップボードにコピーし、URLエンコードを施したのちSafariでGoogle App Scriptに渡し、帰ってきた翻訳文をダイアログに表示します。
(* 翻訳 *)
on translate()
tell application defaultApplication_
-- クリップボードを初期化
set the clipboard to ""
activate
-- コマンドCを押下
tell application "System Events"
keystroke "c" using command down
end tell
delay 1
-- クリップボードからテキストを取得
if (count of (clipboard info for string)) > 0 then
set copyText to the clipboard
else
set copyText to ""
end if
if copyText is "" then
return
end if
-- テキストをURLエンコードする
tell application "System Events"
set scpt to "php -r 'echo rawurlencode(" & quote & copyText & quote & ");'"
set aString to (do shell script scpt) as string
end tell
-- URLを設定 Google Apps Scriptへ情報をGET送信
set theURL to "https://script.google.com/macros/s/AKfycbzXJK7esmNe4E0c8EU4YruColyMabARZqOlHQ08LjITgs0-stC_/exec?text=" & aString & "&source=" & sourceLanguageCode_ & "&target=" & targetLanguageCode_
-- SafariでURLを開きテキストを取得する
tell application "Safari"
-- 翻訳を取得できるまで繰り返し待つ
make new document
set theID to id of window 1
open location theURL
repeat
delay 1
tell document 1 to set safariTxt to every text
if not safariTxt = "" then
exit repeat
end if
end repeat
tell the window id theID to close
end tell
-- 翻訳を表示
display dialog safariTxt buttons {"閉じる"} default button 1
set the clipboard to ""
end tell
end translate
残りは初期設定ファイルの読み書きと、初期設定のGUI部分です。
実は、propertyで初期設定値は自動で保存されるはずなのですが、長いことAppleScriptでアプリケーションを作成していなかったため、(部分的な機能をAppleScriptを使い、全体の構成はXojoで作っていた)アプリケーションにした時にpropertyが効かないことを知らずに手間を食ってしまった。
preferencesフォルダに初期設定ファイルを書き込むことで対処しました。