UTF8エンコード
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # python3が必要です |
004 | # macOS12以降の場合Xcodeかコマンドラインツールが必要です |
005 | # |
006 | # com.cocolog-nifty.quicktimer.icefloe |
007 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
008 | use AppleScript version "2.8" |
009 | use framework "Foundation" |
010 | use framework "AppKit" |
011 | use scripting additions |
012 | property refMe : a reference to current application |
013 | property refNSNotFound : a reference to 9.22337203685477E+18 + 5807 |
014 | |
015 | |
016 | set strText to ("文字列を入力") as «class utf8» |
017 | |
018 | try |
019 | do shell script ("/usr/bin/which python3") |
020 | on error |
021 | do shell script ("/usr/bin/xcode-select --install") |
022 | end try |
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 «class utf8» |
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 «class utf8» |
047 | else |
048 | log "テキストなし" |
049 | set strReadString to strMes as «class utf8» |
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 strMes to "変換するテキストを入力してください" |
063 | set aliasIconPath to POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns" as alias |
064 | try |
065 | 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 |
066 | on error |
067 | log "エラーしました" |
068 | return |
069 | end try |
070 | if "OK" is equal to (button returned of recordResult) then |
071 | set strReturnedText to (text returned of recordResult) as «class utf8» |
072 | else if (gave up of recordResult) is true then |
073 | return "時間切れです" |
074 | else |
075 | return "キャンセル" |
076 | end if |
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 | set ocidReplacedStrings to (ocidResponseText's stringByReplacingOccurrencesOfString:("\\") withString:("\\\\")) |
105 | set ocidReplacedStrings to (ocidReplacedStrings's stringByReplacingOccurrencesOfString:("'") withString:("\\'")) |
106 | set ocidReplacedStrings to (ocidReplacedStrings's stringByReplacingOccurrencesOfString:("\"") withString:("\\\"")) |
107 | set strText to ocidReplacedStrings as «class utf8» |
108 | |
109 | ###戻り値用テキスト |
110 | set strOutPutText to (strText & "\n") as text |
111 | |
112 | ##文字列全体 |
113 | set strCommandText to ("/usr/bin/python3 -c 'dectext=\"" & strText & "\"; print(f\"{dectext.encode().hex().upper()}\")'") as «class utf8» |
114 | log strCommandText |
115 | set strResponseTextAll to (do shell script strCommandText) as «class utf8» |
116 | log strResponseTextAll |
117 | set strOutPutText to (strOutPutText & strResponseTextAll & "\n\n") as text |
118 | |
119 | ##文字単位 一文字づつ |
120 | set numCntChar to (count of character of strText) as integer |
121 | |
122 | repeat with itemCharNo from 1 to (numCntChar) by 1 |
123 | set strSetStr to (character itemCharNo of strText) as «class utf8» |
124 | set strCommandText to ("/usr/bin/python3 -c 'dectext=\"" & strSetStr & "\"; print(f\"{dectext.encode().hex().upper()}\")'") as «class utf8» |
125 | log strCommandText |
126 | set strResponseText to (do shell script strCommandText) as «class utf8» |
127 | log strResponseText |
128 | set strOutPutText to (strOutPutText & strSetStr & " = " & strResponseText & "\n") as text |
129 | |
130 | end repeat |
131 | log strOutPutText |
132 | set strOutPutText to (strOutPutText & "\n\n") as text |
133 | |
134 | #デコード |
135 | set strCommandText to ("/usr/bin/python3 -c 'hex_text =\"" & strResponseTextAll & "\"; print(bytes.fromhex(hex_text).decode(\"utf-8\"))'") as «class utf8» |
136 | log strCommandText |
137 | set strResponseText to (do shell script strCommandText) as «class utf8» |
138 | log strResponseText |
139 | set strOutPutText to (strOutPutText & strResponseText & "\n\n") as text |
140 | |
141 | |
142 | ############################## |
143 | #####ダイアログ |
144 | ############################## |
145 | tell current application |
146 | set strName to name as text |
147 | end tell |
148 | ####スクリプトメニューから実行したら |
149 | if strName is "osascript" then |
150 | tell application "Finder" |
151 | activate |
152 | end tell |
153 | else |
154 | tell current application |
155 | activate |
156 | end tell |
157 | end if |
158 | try |
159 | 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 |
160 | on error |
161 | return "エラーしました" |
162 | end try |
163 | if (gave up of recordResult) is true then |
164 | return "時間切れです" |
165 | end if |
166 | ############################## |
167 | #####自分自身を再実行 |
168 | ############################## |
169 | if button returned of recordResult is "再実行" then |
170 | tell application "Finder" |
171 | set aliasPathToMe to (path to me) as alias |
172 | end tell |
173 | run script aliasPathToMe with parameters "再実行" |
174 | end if |
175 | ############################## |
176 | #####値のコピー |
177 | ############################## |
178 | if button returned of recordResult is "クリップボードにコピー" then |
179 | try |
180 | set strText to text returned of recordResult as text |
181 | ####ペーストボード宣言 |
182 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
183 | set ocidText to (refMe's NSString's stringWithString:(strText)) |
184 | appPasteboard's clearContents() |
185 | appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
186 | on error |
187 | tell application "Finder" |
188 | set the clipboard to strText as text |
189 | end tell |
190 | end try |
191 | end if |
192 | |
193 | |
194 | return 0 |
AppleScriptで生成しました |
| 固定リンク