001 | #!/usr/bin/env osascript |
---|
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
003 | # |
---|
004 | (* |
---|
005 | こちらの |
---|
006 | https://www.w3.org/2003/entities/ |
---|
007 | XMLを利用して変換します |
---|
008 | https://www.w3.org/2003/entities/2007xml/html5ents.xml |
---|
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 "UniformTypeIdentifiers" |
---|
015 | use framework "AppKit" |
---|
016 | use scripting additions |
---|
017 | property refMe : a reference to current application |
---|
018 | |
---|
019 | |
---|
020 | |
---|
021 | set strMes to ("日本語入力改行無しでなるべく短めが良い") as text |
---|
022 | |
---|
023 | ######################## |
---|
024 | ## クリップボードの中身取り出し |
---|
025 | ######################## |
---|
026 | ###初期化 |
---|
027 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
---|
028 | ##格納されているタイプをリストにして |
---|
029 | set ocidPastBoardTypeArray to appPasteboard's types |
---|
030 | ###テキストがあれば |
---|
031 | set boolContain to ocidPastBoardTypeArray's containsObject:("public.utf8-plain-text") |
---|
032 | if (boolContain as boolean) is true then |
---|
033 | ###値を格納する |
---|
034 | tell application "Finder" |
---|
035 | set strReadString to (the clipboard as text) as text |
---|
036 | end tell |
---|
037 | else |
---|
038 | ###UTF8が無いなら |
---|
039 | ##テキスト形式があるか?確認して |
---|
040 | set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSPasteboardTypeString) |
---|
041 | ##テキスト形式があるなら |
---|
042 | if (boolContain as boolean) is true then |
---|
043 | set ocidTypeClassArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
044 | ocidTypeClassArray's addObject:(refMe's NSString) |
---|
045 | set ocidReadString to appPasteboard's readObjectsForClasses:(ocidTypeClassArray) options:(missing value) |
---|
046 | set strReadString to ocidReadString as text |
---|
047 | else |
---|
048 | log "テキストなし" |
---|
049 | set strReadString to strMes as text |
---|
050 | end if |
---|
051 | end if |
---|
052 | |
---|
053 | ############################## |
---|
054 | #####ダイアログ |
---|
055 | ############################## |
---|
056 | set strName to (name of current application) as text |
---|
057 | if strName is "osascript" then |
---|
058 | tell application "Finder" to activate |
---|
059 | else |
---|
060 | tell current application to activate |
---|
061 | end if |
---|
062 | set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ClippingUnknown.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 text |
---|
071 | else if (gave up of recordResult) is true then |
---|
072 | return "時間切れです" |
---|
073 | else |
---|
074 | return "キャンセル" |
---|
075 | end if |
---|
076 | |
---|
077 | ############################## |
---|
078 | #####戻り値整形 |
---|
079 | ############################## |
---|
080 | set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText)) |
---|
081 | ###タブと改行を除去しておく |
---|
082 | set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
---|
083 | ocidTextM's appendString:(ocidResponseText) |
---|
084 | ###除去する文字列リスト |
---|
085 | set listRemoveChar to {"\n", "\r", "\t"} as list |
---|
086 | ##置換 |
---|
087 | repeat with itemChar in listRemoveChar |
---|
088 | set strPattern to itemChar as text |
---|
089 | set strTemplate to ("") as text |
---|
090 | set ocidOption to (refMe's NSRegularExpressionCaseInsensitive) |
---|
091 | set listResponse to (refMe's NSRegularExpression's regularExpressionWithPattern:(strPattern) options:(ocidOption) |error| :(reference)) |
---|
092 | if (item 2 of listResponse) ≠ (missing value) then |
---|
093 | log (item 2 of listResponse)'s localizedDescription() as text |
---|
094 | return "正規表現パターンに誤りがあります" |
---|
095 | else |
---|
096 | set ocidRegex to (item 1 of listResponse) |
---|
097 | end if |
---|
098 | set numLength to ocidResponseText's |length|() |
---|
099 | set ocidRange to refMe's NSRange's NSMakeRange(0, numLength) |
---|
100 | set ocidResponseText to (ocidRegex's stringByReplacingMatchesInString:(ocidResponseText) options:0 range:(ocidRange) withTemplate:(strTemplate)) |
---|
101 | end repeat |
---|
102 | ############################## |
---|
103 | #####データ読み込み |
---|
104 | ############################## |
---|
105 | set aliasPathToMe to (path to me) as alias |
---|
106 | set strPathToMe to (POSIX path of aliasPathToMe) as text |
---|
107 | set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe) |
---|
108 | set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath() |
---|
109 | set ocidContainerDirPath to ocidPathToMe's stringByDeletingLastPathComponent() |
---|
110 | set ocidDataDirPath to ocidContainerDirPath's stringByAppendingPathComponent:("Data") |
---|
111 | set ocidPlistFilePath to ocidDataDirPath's stringByAppendingPathComponent:("html5ents.plist") |
---|
112 | set ocidPlistFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPlistFilePath) isDirectory:false) |
---|
113 | ## |
---|
114 | set listResponse to refMe's NSDictionary's dictionaryWithContentsOfURL:(ocidPlistFilePathURL) |error| :(reference) |
---|
115 | set ocidReadDict to (item 1 of listResponse) |
---|
116 | # |
---|
117 | ############################## |
---|
118 | #####処理部 |
---|
119 | ############################## |
---|
120 | #戻し用のテキスト |
---|
121 | set strReturnText to "" as text |
---|
122 | #整形済みの入力テキスト |
---|
123 | set strResponseText to ocidResponseText as «class utf8» |
---|
124 | #文字数数えて |
---|
125 | set numCntText to (count of every text item of strResponseText) as integer |
---|
126 | #文字数だけ繰り返し |
---|
127 | repeat with itemNo from 1 to numCntText by 1 |
---|
128 | #入力文字列からテキスト取り出して |
---|
129 | set strItemText to (text item itemNo of strResponseText) as «class utf8» |
---|
130 | set ocidItemDict to (ocidReadDict's objectForKey:(strItemText)) |
---|
131 | if ocidItemDict = (missing value) then |
---|
132 | set strEncdedText to doGetHtmlEnt(strItemText) |
---|
133 | set strReturnText to (strReturnText & strEncdedText) as «class utf8» |
---|
134 | else |
---|
135 | set strDictValue to (ocidItemDict's valueForKey:("entity")) as «class utf8» |
---|
136 | if strDictValue starts with "&" then |
---|
137 | #そのまま |
---|
138 | else |
---|
139 | set strDictValue to ("&" & strDictValue) |
---|
140 | end if |
---|
141 | if strDictValue ends with ";" then |
---|
142 | #そのまま |
---|
143 | else |
---|
144 | set strDictValue to (strDictValue & ";") |
---|
145 | end if |
---|
146 | set strReturnText to (strReturnText & strDictValue) as «class utf8» |
---|
147 | end if |
---|
148 | end repeat |
---|
149 | |
---|
150 | |
---|
151 | ############################## |
---|
152 | #####ダイアログ |
---|
153 | ############################## |
---|
154 | tell current application |
---|
155 | set strName to name as text |
---|
156 | end tell |
---|
157 | ####スクリプトメニューから実行したら |
---|
158 | if strName is "osascript" then |
---|
159 | tell application "Finder" |
---|
160 | activate |
---|
161 | end tell |
---|
162 | else |
---|
163 | tell current application |
---|
164 | activate |
---|
165 | end tell |
---|
166 | end if |
---|
167 | if strReturnText is "false" then |
---|
168 | set strMes to (strReturnedText & "\n変換に失敗しました") as text |
---|
169 | set strReturnText to ("失敗しました") as text |
---|
170 | else |
---|
171 | set strMes to ("変換結果です\n" & strReturnText) as text |
---|
172 | end if |
---|
173 | try |
---|
174 | set recordResult to (display dialog strMes with title "戻り値です" default answer strReturnText buttons {"クリップボードにコピー", "終了", "再実行"} default button "再実行" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record |
---|
175 | on error |
---|
176 | return "エラーしました" |
---|
177 | end try |
---|
178 | if (gave up of recordResult) is true then |
---|
179 | return "時間切れです" |
---|
180 | end if |
---|
181 | ############################## |
---|
182 | #####自分自身を再実行 |
---|
183 | ############################## |
---|
184 | if button returned of recordResult is "再実行" then |
---|
185 | tell application "Finder" |
---|
186 | set aliasPathToMe to (path to me) as alias |
---|
187 | end tell |
---|
188 | run script aliasPathToMe with parameters "再実行" |
---|
189 | end if |
---|
190 | ############################## |
---|
191 | #####値のコピー |
---|
192 | ############################## |
---|
193 | if button returned of recordResult is "クリップボードにコピー" then |
---|
194 | try |
---|
195 | set strText to text returned of recordResult as text |
---|
196 | ####ペーストボード宣言 |
---|
197 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
---|
198 | set ocidText to (refMe's NSString's stringWithString:(strText)) |
---|
199 | appPasteboard's clearContents() |
---|
200 | appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
---|
201 | on error |
---|
202 | tell application "Finder" |
---|
203 | set the clipboard to strText as text |
---|
204 | end tell |
---|
205 | end try |
---|
206 | end if |
---|
207 | |
---|
208 | log "処理終了" |
---|
209 | return "処理終了" |
---|
210 | |
---|
211 | |
---|
212 | ############################## |
---|
213 | #####HTML Entityサブ |
---|
214 | ############################## |
---|
215 | to doGetHtmlEnt(argText) |
---|
216 | set strSetValue to argText as Unicode text |
---|
217 | set ocidItemStr to refMe's NSString's stringWithString:(strSetValue) |
---|
218 | set ocidItemStr to (ocidItemStr's stringByReplacingOccurrencesOfString:("\\") withString:("\\\\\\")) |
---|
219 | set ocidItemStr to (ocidItemStr's stringByReplacingOccurrencesOfString:("'") withString:("\\'")) |
---|
220 | set ocidItemStr to (ocidItemStr's stringByReplacingOccurrencesOfString:("\"") withString:("\\\"")) |
---|
221 | set strSetValue to ocidItemStr as Unicode text |
---|
222 | |
---|
223 | set strCommandText to ("function doGetCharUni() {let strText = '" & strSetValue & "';let listOutputArray = [];let listCharArray = Array.from(strText);for (let itemChar of listCharArray) {let numCodePoint = itemChar.codePointAt(0);let strUnicodeNo = numCodePoint.toString(16).toUpperCase();let strFormattedUnicodeNo = strUnicodeNo.padStart(4, '0'); strUnicodeNo = '&#x' + strFormattedUnicodeNo; listOutputArray.push(strUnicodeNo);}let strOutput = listOutputArray.join(';');return strOutput;} doGetCharUni();") as text |
---|
224 | # log "\n" & strCommandText & "\n" |
---|
225 | set strExecCommand to ("/usr/bin/osascript -l JavaScript -e \"" & strCommandText & "\"") as text |
---|
226 | # log "\n" & strExecCommand & "\n" |
---|
227 | try |
---|
228 | set strResponse to (do shell script strExecCommand) as string |
---|
229 | on error |
---|
230 | log "osascript でエラーしました" |
---|
231 | return false |
---|
232 | end try |
---|
233 | |
---|
234 | if strResponse starts with "&#x" then |
---|
235 | set strReturnText to (strResponse & ";") as string |
---|
236 | else |
---|
237 | set strReturnText to ("&#x" & strResponse & ";") as string |
---|
238 | end if |
---|
239 | |
---|
240 | if strReturnText is " " then |
---|
241 | set strReturnText to (" ") as text |
---|
242 | end if |
---|
243 | return strReturnText |
---|
244 | end doGetHtmlEnt |
---|