001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 |
|
005 | 日本語のひらがなをローマ字読みで書かれた英字に変換します |
006 | romkanを利用します |
007 | python3が必須ですのでXcodeがインストール済みの必要があります |
008 | 同封のセットアップを実施してください |
009 |
|
010 | com.cocolog-nifty.quicktimer.icefloe *) |
011 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
012 | use AppleScript version "2.8" |
013 | use framework "Foundation" |
014 | use framework "AppKit" |
015 | use scripting additions |
016 | property refMe : a reference to current application |
017 | property refNSNotFound : a reference to 9.22337203685477E+18 + 5807 |
018 |
|
019 |
|
020 | set strText to ("もじをにゅうりょくしてください") as «class utf8» |
021 |
|
022 | ######################## |
023 | ## クリップボードの中身取り出し |
024 | ######################## |
025 | ###初期化 |
026 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
027 | ##格納されているタイプをリストにして |
028 | set ocidPastBoardTypeArray to appPasteboard's types |
029 | ###テキストがあれば |
030 | set boolContain to ocidPastBoardTypeArray's containsObject:("public.utf8-plain-text") |
031 | if (boolContain as boolean) is true then |
032 | ###値を格納する |
033 | tell application "Finder" |
034 | set strReadString to (the clipboard as text) as «class utf8» |
035 | end tell |
036 | else |
037 | ###UTF8が無いなら |
038 | ##テキスト形式があるか?確認して |
039 | set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSPasteboardTypeString) |
040 | ##テキスト形式があるなら |
041 | if (boolContain as boolean) is true then |
042 | set ocidTypeClassArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
043 | ocidTypeClassArray's addObject:(refMe's NSString) |
044 | set ocidReadString to appPasteboard's readObjectsForClasses:(ocidTypeClassArray) options:(missing value) |
045 | set strReadString to ocidReadString as «class utf8» |
046 | else |
047 | log "テキストなし" |
048 | set strReadString to strMes as «class utf8» |
049 | end if |
050 | end if |
051 | ############################## |
052 | #####ダイアログ |
053 | ############################## |
054 | ###ダイアログを前面に出す |
055 | set strName to (name of current application) as text |
056 | if strName is "osascript" then |
057 | tell application "Finder" to activate |
058 | else |
059 | tell current application to activate |
060 | end if |
061 | set strMes to "変換するテキストを入力してください" |
062 | set aliasIconPath to POSIX file "/System/Applications/Automator.app/Contents/Resources/AppIcon.icns" as alias |
063 | try |
064 | set recordResult to (display dialog strMes with title "入力してください" default answer strReadString buttons {"OK", "キャンセル"} default button "OK" with icon aliasIconPath giving up after 20 without hidden answer) as record |
065 | on error |
066 | log "エラーしました" |
067 | return |
068 | end try |
069 | if "OK" is equal to (button returned of recordResult) then |
070 | set strReturnedText to (text returned of recordResult) as «class utf8» |
071 | else if (gave up of recordResult) is true then |
072 | return "時間切れです" |
073 | else |
074 | return "キャンセル" |
075 | end if |
076 | ############################## |
077 | #####戻り値整形 |
078 | ############################## |
079 | set ocidReturnedText to (refMe's NSString's stringWithString:(strReturnedText)) |
080 | #改行を置換しておく |
081 | set ocidReturnedText to (ocidReturnedText's stringByReplacingOccurrencesOfString:("\r\n") withString:("⏎")) |
082 | set ocidReturnedText to (ocidReturnedText's stringByReplacingOccurrencesOfString:("\r") withString:("⏎")) |
083 | set ocidResponseText to (ocidReturnedText's stringByReplacingOccurrencesOfString:("\n") withString:("⏎")) |
084 |
|
085 | ###最低限のエスケープ処理 |
086 | set ocidReturnedText to (ocidReturnedText's stringByReplacingOccurrencesOfString:("\\") withString:("\\\\\\")) |
087 | set ocidReturnedText to (ocidReturnedText's stringByReplacingOccurrencesOfString:("'") withString:("\\'")) |
088 | set ocidReturnedText to (ocidReturnedText's stringByReplacingOccurrencesOfString:("\"") withString:("\\\"")) |
089 | set strText to ocidReturnedText as «class utf8» |
090 | ############################## |
091 | #####処理実行 |
092 | ############################## |
093 | set strCommandText to ("/usr/bin/which python3") as text |
094 | set strPyBinPath to doZshShellScript(strCommandText) as text |
095 |
|
096 | set strCommandText to ("") as text |
097 | set strCommandText to ("\"" & strPyBinPath & "\" -c 'import romkan; print(romkan.to_roma(\"" & strText & "\"))'") as text |
098 | set strReturnedText to (do shell script strCommandText) as text |
099 | ############################## |
100 | #####戻り値整形 |
101 | ############################## |
102 | set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText)) |
103 |
|
104 | set ocidResponseText to (ocidResponseText's stringByReplacingOccurrencesOfString:("⏎") withString:("\n")) |
105 |
|
106 | set strOutPutText to ocidResponseText as «class utf8» |
107 |
|
108 | ############################## |
109 | #####ダイアログ |
110 | ############################## |
111 | tell current application |
112 | set strName to name as text |
113 | end tell |
114 | ####スクリプトメニューから実行したら |
115 | if strName is "osascript" then |
116 | tell application "Finder" |
117 | activate |
118 | end tell |
119 | else |
120 | tell current application |
121 | activate |
122 | end tell |
123 | end if |
124 | set strMes to "変換しましたコピーして使ってください" |
125 | try |
126 | set recordResult to (display dialog strMes with title "戻り値です" default answer strOutPutText buttons {"クリップボードにコピー", "終了", "再実行"} default button "再実行" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record |
127 | on error |
128 | return "エラーしました" |
129 | end try |
130 | if (gave up of recordResult) is true then |
131 | return "時間切れです" |
132 | end if |
133 | ############################## |
134 | #####自分自身を再実行 |
135 | ############################## |
136 | if button returned of recordResult is "再実行" then |
137 | tell application "Finder" |
138 | set aliasPathToMe to (path to me) as alias |
139 | end tell |
140 | run script aliasPathToMe with parameters "再実行" |
141 | end if |
142 | ############################## |
143 | #####値のコピー |
144 | ############################## |
145 | if button returned of recordResult is "クリップボードにコピー" then |
146 | try |
147 | set strText to text returned of recordResult as text |
148 | ####ペーストボード宣言 |
149 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
150 | set ocidText to (refMe's NSString's stringWithString:(strText)) |
151 | appPasteboard's clearContents() |
152 | appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
153 | on error |
154 | tell application "Finder" |
155 | set the clipboard to strText as text |
156 | end tell |
157 | end try |
158 | end if |
159 |
|
160 |
|
161 | return 0 |
162 |
|
163 |
|
164 |
|
165 | ########################## |
166 | # ZSH 実行 |
167 | to doZshShellScript(argCommandText) |
168 | set strCommandText to argCommandText as text |
169 | log "コマンド開始\r" & strCommandText & "\r" |
170 | set strExec to ("/bin/zsh -c '" & strCommandText & "'") as text |
171 | ########## |
172 | #コマンド実行 |
173 | try |
174 | set strResnponse to (do shell script strExec) as text |
175 | log "コマンド終了" |
176 | on error |
177 | return false |
178 | end try |
179 | return strResnponse |
180 | end doZshShellScript |