[Error]MacErrors.hの内容をplistにしてエラー番号から内容を検索する
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* com.cocolog-nifty.quicktimer.icefloe |
004 | MacErrors.hの値を参照します |
005 | 参照検索用に |
006 | /Users/ユーザー名/Documents/Apple/MacErrors/MacErrors.plist |
007 | を |
008 | 作成します |
009 | *) |
010 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
011 | use AppleScript version "2.8" |
012 | use framework "Foundation" |
013 | use framework "AppKit" |
014 | use framework "CoreServices" |
015 | use scripting additions |
016 | property refNSNotFound : a reference to 9.22337203685477E+18 + 5807 |
017 | property refMe : a reference to current application |
018 | |
019 | set strMes to ("エラー番号を半角数字で入力\rマイナスの場合はマイナス記号が必要です") as text |
020 | |
021 | ######################## |
022 | ## クリップボードの中身取り出し |
023 | ######################## |
024 | ###初期化 |
025 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
026 | ##格納されているタイプをリストにして |
027 | set ocidPastBoardTypeArray to appPasteboard's types |
028 | ###テキストがあれば |
029 | set boolContain to ocidPastBoardTypeArray's containsObject:("public.utf8-plain-text") |
030 | if (boolContain as boolean) is true then |
031 | ###値を格納する |
032 | tell application "Finder" |
033 | set strReadString to (the clipboard as text) as text |
034 | end tell |
035 | else |
036 | ###UTF8が無いなら |
037 | ##テキスト形式があるか?確認して |
038 | set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSPasteboardTypeString) |
039 | ##テキスト形式があるなら |
040 | if (boolContain as boolean) is true then |
041 | set ocidTypeClassArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
042 | ocidTypeClassArray's addObject:(refMe's NSString) |
043 | set ocidReadString to appPasteboard's readObjectsForClasses:(ocidTypeClassArray) options:(missing value) |
044 | set strReadString to ocidReadString as text |
045 | else |
046 | log "テキストなし" |
047 | set strReadString to strMes as text |
048 | end if |
049 | end if |
050 | ############################## |
051 | #####ダイアログ |
052 | ############################## |
053 | set boolCalculator to (missing value) |
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 aliasIconPath to POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns" as alias |
062 | try |
063 | 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 |
064 | on error |
065 | log "エラーしました" |
066 | return |
067 | end try |
068 | if "OK" is equal to (button returned of recordResult) then |
069 | set strReturnedText to (text returned of recordResult) as text |
070 | else if (gave up of recordResult) is true then |
071 | return "時間切れです" |
072 | else |
073 | return "キャンセル" |
074 | end if |
075 | ############################## |
076 | #####戻り値整形 |
077 | ############################## |
078 | set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText)) |
079 | set ocidResponseText to (ocidResponseText's stringByReplacingOccurrencesOfString:("\t") withString:("")) |
080 | set ocidResponseText to (ocidResponseText's stringByReplacingOccurrencesOfString:("\n") withString:("")) |
081 | set ocidResponseText to (ocidResponseText's stringByReplacingOccurrencesOfString:("\r") withString:("")) |
082 | |
083 | ####戻り値を半角にする |
084 | set ocidNSStringTransform to (refMe's NSStringTransformFullwidthToHalfwidth) |
085 | set ocidTextM to (ocidResponseText's stringByApplyingTransform:ocidNSStringTransform |reverse|:false) |
086 | |
087 | ###数字以外があれば中止する |
088 | set ocidDemSet to refMe's NSCharacterSet's characterSetWithCharactersInString:("0123456789-") |
089 | ##数字以外って意味で逆セット |
090 | set ocidCharSet to ocidDemSet's invertedSet() |
091 | set ocidOption to (refMe's NSLiteralSearch) |
092 | ##数字以外の文字を探して |
093 | set ocidRange to ocidResponseText's rangeOfCharacterFromSet:(ocidCharSet) options:(ocidOption) |
094 | set ocidLocation to ocidRange's location |
095 | ##なければOK |
096 | if ocidLocation = refNSNotFound then |
097 | log "処理開始" |
098 | else if ocidLocation ≥ 0 then |
099 | tell application "Finder" |
100 | set aliasPathToMe to (path to me) as alias |
101 | end tell |
102 | log "数値以外の値があったのでやりなし" |
103 | return run script aliasPathToMe |
104 | end if |
105 | #これが検索語句 |
106 | set strErrorNo to ocidTextM as string |
107 | |
108 | ################ |
109 | #パス |
110 | set strSubPathH to ("/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/Headers/MacErrors.h") as text |
111 | set strSubPathR to ("/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/Headers/MacErrors.r") as text |
112 | |
113 | #ベースパス |
114 | set strXcodePath to ("/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk") |
115 | set strCmmandToolPath to ("/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk") |
116 | |
117 | ################ |
118 | #Xcode |
119 | set strFilePath to (strXcodePath & strSubPathH) as text |
120 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
121 | set ocidFilePathR to ocidFilePathStr's stringByStandardizingPath() |
122 | #有無チェック |
123 | set appFileManager to refMe's NSFileManager's defaultManager() |
124 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidFilePathR) isDirectory:(false) |
125 | #Xcodeインストール済みならこちらのパス |
126 | if boolDirExists = true then |
127 | set ocidFilePath to ocidFilePathR |
128 | else if boolDirExists = false then |
129 | #CommandLineTools |
130 | set strFilePath to (strCmmandToolPath & strSubPathH) as text |
131 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
132 | set ocidFilePathR to ocidFilePathStr's stringByStandardizingPath() |
133 | #有無チェック |
134 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidFilePathR) isDirectory:(false) |
135 | if boolDirExists = true then |
136 | set ocidFilePath to ocidFilePathR |
137 | else if boolDirExists = false then |
138 | set strAlertMes to ("ファイルが見つかりません\rXcodeかCommandLineToolsを\rインストールしてください") as text |
139 | display alert strAlertMes buttons {"終了"} default button "終了" cancel button "終了" as informational giving up after 10 |
140 | return |
141 | end if |
142 | end if |
143 | #使用するパス |
144 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
145 | |
146 | ################ |
147 | #Plistの保存先 |
148 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask)) |
149 | set ocidDocumentDirPathURL to ocidURLsArray's firstObject() |
150 | set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:("Apple/MacErrors") isDirectory:(true) |
151 | #フォルダを作る |
152 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
153 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
154 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error| :(reference) |
155 | #PLISTパス |
156 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("MacErrors.plist") isDirectory:(false) |
157 | ################################ |
158 | #初回チェック |
159 | set ocidSaveFilePath to ocidSaveFilePathURL's |path|() |
160 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidSaveFilePath) isDirectory:(true) |
161 | if boolDirExists = true then |
162 | #すでにあるので作成しない |
163 | set ocidPlistArray to refMe's NSMutableArray's alloc()'s initWithContentsOfURL:(ocidSaveFilePathURL) |
164 | else if boolDirExists = false then |
165 | #PLISTを作成する |
166 | set ocidPlistArray to doMakePlist(ocidFilePathURL, ocidSaveFilePathURL) |
167 | end if |
168 | |
169 | ################################ |
170 | #検索 |
171 | set numCntArray to ocidPlistArray's |count|() |
172 | #出力用のテキスト |
173 | set ocidOutputString to refMe's NSMutableString's alloc()'s init() |
174 | |
175 | # |
176 | repeat with itemNo from 0 to (numCntArray - 1) by 1 |
177 | #順番に取り出して |
178 | set ocidItemDict to (ocidPlistArray's objectAtIndex:(itemNo)) |
179 | # |
180 | set ocidValueArray to (ocidItemDict's objectForKey:(strErrorNo)) |
181 | if ocidValueArray ≠ (missing value) then |
182 | (ocidOutputString's appendString:("ErrorNo: ")) |
183 | (ocidOutputString's appendString:(strErrorNo)) |
184 | (ocidOutputString's appendString:("\n")) |
185 | (ocidOutputString's appendString:("ErrorCode: ")) |
186 | (ocidOutputString's appendString:(ocidValueArray's firstObject())) |
187 | (ocidOutputString's appendString:("\n")) |
188 | (ocidOutputString's appendString:("Info: ")) |
189 | (ocidOutputString's appendString:(ocidValueArray's lastObject())) |
190 | (ocidOutputString's appendString:("\n")) |
191 | end if |
192 | |
193 | end repeat |
194 | |
195 | ################################ |
196 | #NSErrorの値も入れる |
197 | set ocidNSErrorData to refMe's NSError's errorWithDomain:(refMe's NSCocoaErrorDomain) code:(strErrorNo) userInfo:(missing value) |
198 | set ocidDiscription to ocidNSErrorData's localizedDescription() as text |
199 | (ocidOutputString's appendString:("Description: ")) |
200 | (ocidOutputString's appendString:(ocidDiscription)) |
201 | (ocidOutputString's appendString:("\n")) |
202 | |
203 | set strMes to ocidOutputString as text |
204 | |
205 | ############################## |
206 | #####ダイアログ |
207 | ############################## |
208 | tell current application |
209 | set strName to name as text |
210 | end tell |
211 | ####スクリプトメニューから実行したら |
212 | if strName is "osascript" then |
213 | tell application "Finder" |
214 | activate |
215 | end tell |
216 | else |
217 | tell current application |
218 | activate |
219 | end tell |
220 | end if |
221 | try |
222 | set recordResult to (display dialog strMes with title "戻り値です" default answer strMes buttons {"クリップボードにコピー", "終了", "再実行"} default button "再実行" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record |
223 | on error |
224 | return "エラーしました" |
225 | end try |
226 | if (gave up of recordResult) is true then |
227 | return "時間切れです" |
228 | end if |
229 | ############################## |
230 | #####自分自身を再実行 |
231 | ############################## |
232 | if button returned of recordResult is "再実行" then |
233 | tell application "Finder" |
234 | set aliasPathToMe to (path to me) as alias |
235 | end tell |
236 | run script aliasPathToMe with parameters "再実行" |
237 | return |
238 | end if |
239 | ############################## |
240 | #####値のコピー |
241 | ############################## |
242 | if button returned of recordResult is "クリップボードにコピー" then |
243 | try |
244 | set strText to text returned of recordResult as text |
245 | ####ペーストボード宣言 |
246 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
247 | set ocidText to (refMe's NSString's stringWithString:(strText)) |
248 | appPasteboard's clearContents() |
249 | appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
250 | on error |
251 | tell application "Finder" |
252 | set the clipboard to strText as text |
253 | end tell |
254 | end try |
255 | end if |
256 | |
257 | |
258 | |
259 | |
260 | ################################ |
261 | #PLIST作成 |
262 | to doMakePlist(ocidFilePathURL, ocidSaveFilePathURL) |
263 | #作成する |
264 | ################ |
265 | #ファイル読み込み |
266 | set listResponse to refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
267 | set ocidReadStrings to (item 1 of listResponse) |
268 | #UTF8でエラーならASCIIにする |
269 | if ocidReadStrings = (missing value) then |
270 | set listResponse to refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) encoding:(refMe's NSASCIIStringEncoding) |error| :(reference) |
271 | set ocidReadStrings to (item 1 of listResponse) |
272 | end if |
273 | #Arrayに |
274 | set ocidLineArray to ocidReadStrings's componentsSeparatedByString:("\n") |
275 | #次の工程に回すARRAY |
276 | set ocidRootArray to refMe's NSMutableArray's alloc()'s init() |
277 | #各行順処理 |
278 | repeat with itemLine in ocidLineArray |
279 | #行判定 |
280 | set boolStart to (itemLine's hasPrefix:("enum")) |
281 | set boolEnd to (itemLine's hasPrefix:("};")) |
282 | set boolContain to (itemLine's containsString:("=")) |
283 | #判定別処理 |
284 | if boolStart is true then |
285 | set ocidSubArray to refMe's NSMutableArray's alloc()'s init() |
286 | else if boolEnd is true then |
287 | (ocidRootArray's addObject:(ocidSubArray)) |
288 | else if boolContain is true then |
289 | (ocidSubArray's addObject:(itemLine)) |
290 | end if |
291 | end repeat |
292 | |
293 | ################ |
294 | #2時工程 |
295 | set numCntArray to ocidRootArray's |count|() |
296 | #出力用のARRAY これをPLISTにする |
297 | set ocidPlistArray to refMe's NSMutableArray's alloc()'s init() |
298 | #ここで正規表現初期化 |
299 | set strRegpattern to ("(?<=/\\*)\\s*(.*?)\\s*(?=\\*/)") as text |
300 | set listResponse to refMe's NSRegularExpression's regularExpressionWithPattern:(strRegpattern) options:(0) |error| :(reference) |
301 | set appRegA to (item 1 of listResponse) |
302 | # |
303 | set strRegpattern to ("^\\s*(-?\\d+)(?!=\\,)") as text |
304 | set listResponse to refMe's NSRegularExpression's regularExpressionWithPattern:(strRegpattern) options:(0) |error| :(reference) |
305 | set appRegB to (item 1 of listResponse) |
306 | |
307 | #順番に処理 |
308 | repeat with itemNo from 0 to (numCntArray - 1) by 1 |
309 | #行内容のDICT と Array |
310 | set ocidItemDict to refMe's NSMutableDictionary's alloc()'s init() |
311 | #ARRAY取り出し |
312 | set ocidLineArray to (ocidRootArray's objectAtIndex:(itemNo)) |
313 | repeat with itemLineArray in ocidLineArray |
314 | #=で前後に分けて |
315 | set ociditemLineSubArray to (itemLineArray's componentsSeparatedByString:("=")) |
316 | #エラー名 |
317 | set ocidErroeName to ociditemLineSubArray's firstObject() |
318 | set ocidErroeName to (ocidErroeName's stringByReplacingOccurrencesOfString:(" ") withString:("")) |
319 | #エラー番号と コメント |
320 | set ocidErroeNoStr to ociditemLineSubArray's lastObject() |
321 | #正規表現 エラーメッセージ |
322 | set ocidLength to ocidErroeNoStr's |length|() |
323 | set ocidRange to refMe's NSRange's NSMakeRange(0, ocidLength) |
324 | set ocidMatch to (appRegA's firstMatchInString:(ocidErroeNoStr) options:(0) range:(ocidRange)) |
325 | if ocidMatch = (missing value) then |
326 | set ocidErrorMes to ("") as text |
327 | else |
328 | set ocidErrorMes to (ocidErroeNoStr's substringWithRange:(ocidMatch's range())) |
329 | end if |
330 | #正規表現 エラー番号 |
331 | set ocidMatch to (appRegB's firstMatchInString:(ocidErroeNoStr) options:(0) range:(ocidRange)) |
332 | if ocidMatch = (missing value) then |
333 | set ocidErrorMes to ("") as text |
334 | else |
335 | set ocidErrorNO to (ocidErroeNoStr's substringWithRange:(ocidMatch's range())) |
336 | set ocidErrorNO to (ocidErrorNO's stringByReplacingOccurrencesOfString:(" ") withString:("")) |
337 | set ocidErrorNO to (ocidErrorNO's stringByReplacingOccurrencesOfString:("\t") withString:("")) |
338 | # |
339 | set ocidItemLineArray to refMe's NSMutableArray's alloc()'s init() |
340 | (ocidItemLineArray's addObject:(ocidErroeName)) |
341 | (ocidItemLineArray's addObject:(ocidErrorMes)) |
342 | # |
343 | (ocidItemDict's setObject:(ocidItemLineArray) forKey:(ocidErrorNO)) |
344 | end if |
345 | |
346 | end repeat |
347 | (ocidPlistArray's addObject:(ocidItemDict)) |
348 | |
349 | end repeat |
350 | |
351 | ################ |
352 | #とりあえず保存しておく |
353 | set ocidFormat to (current application's NSPropertyListBinaryFormat_v1_0) |
354 | set listResponse to current application's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistArray) format:(ocidFormat) options:0 |error| :(reference) |
355 | if (item 2 of listResponse) = (missing value) then |
356 | set ocidPlistData to (item 1 of listResponse) |
357 | else if (item 2 of listResponse) ≠ (missing value) then |
358 | set strErrorNo to (item 2 of listResponse)'s code() as text |
359 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
360 | current application's NSLog("■:" & strErrorNo & strErrorMes) |
361 | return "エラーしました" & strErrorNo & strErrorMes |
362 | end if |
363 | |
364 | #保存 |
365 | set ocidOption to (current application's NSDataWritingAtomic) |
366 | set listDone to ocidPlistData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference) |
367 | if (item 1 of listDone) is true then |
368 | log "正常終了" |
369 | else if (item 1 of listDone) is false then |
370 | set strErrorNo to (item 2 of listResponse)'s code() as text |
371 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
372 | current application's NSLog("■:" & strErrorNo & strErrorMes) |
373 | return "エラーしました" & strErrorNo & strErrorMes |
374 | end if |
375 | return ocidPlistArray |
376 | end doMakePlist |
377 | |
AppleScriptで生成しました |
| 固定リンク