| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 | GET Spotlight Schema.applescript |
| 006 |
|
| 007 | SpotlightのスキーマをMDテーブルに出力します |
| 008 | 出力結果見本はこちら |
| 009 | https://gist.github.com/force4u/2848fb489e8d013200ba46242648403d |
| 010 |
|
| 011 |
|
| 012 | レインボーカーソルになることがありますが |
| 013 | 少し待ってみてください |
| 014 | UIをロードするのに時間がかかっているだけの場面が多いです |
| 015 |
|
| 016 | 可能な限り、スキームの取得を試みたバージョン |
| 017 |
|
| 018 |
|
| 019 | com.cocolog-nifty.quicktimer.icefloe *) |
| 020 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 021 | use AppleScript version "2.8" |
| 022 | use framework "Foundation" |
| 023 | use framework "AppKit" |
| 024 | use scripting additions |
| 025 |
|
| 026 | property refMe : a reference to current application |
| 027 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 028 |
|
| 029 |
|
| 030 | #GET LANG CODE |
| 031 | set appLocale to refMe's NSLocale's currentLocale() |
| 032 | set strLangID to (appLocale's objectForKey:(refMe's NSLocaleLanguageCode)) as text |
| 033 |
|
| 034 |
|
| 035 |
|
| 036 | set strCmd to ("/usr/bin/mdimport -L") as text |
| 037 | set strStdOut to (do shell script strCmd) as text |
| 038 | set ocidStdOut to refMe's NSMutableString's stringWithString:(strStdOut) |
| 039 | set ocidStdOut to (ocidStdOut's stringByReplacingOccurrencesOfString:("\",") withString:("")) |
| 040 | set ocidStdOut to (ocidStdOut's stringByReplacingOccurrencesOfString:("\"") withString:("")) |
| 041 | set ocidStdOut to (ocidStdOut's stringByReplacingOccurrencesOfString:(space & space) withString:("")) |
| 042 | set ocidStdOutArray to ocidStdOut's componentsSeparatedByString:(return) |
| 043 | #最初と最後を削除 |
| 044 | ocidStdOutArray's removeLastObject() |
| 045 | ocidStdOutArray's removeObjectAtIndex:(0) |
| 046 |
|
| 047 | ######################## |
| 048 | #収集URLの格納 |
| 049 | set ocidLocURLArray to refMe's NSMutableArray's alloc()'s init() |
| 050 |
|
| 051 | #コンテンツの収集 |
| 052 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
| 053 | set ocidKeyArray to refMe's NSMutableArray's alloc()'s init() |
| 054 | (ocidKeyArray's addObject:(refMe's NSURLPathKey)) |
| 055 | (ocidKeyArray's addObject:(refMe's NSURLIsDirectoryKey)) |
| 056 | (ocidKeyArray's addObject:(refMe's NSURLIsSymbolicLinkKey)) |
| 057 |
|
| 058 | repeat with itemFilePath in ocidStdOutArray |
| 059 | set ocidFilePath to itemFilePath's stringByStandardizingPath() |
| 060 | set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)) |
| 061 | set ocidResourcesDirURL to (ocidFilePathURL's URLByAppendingPathComponent:("Contents/Resources") isDirectory:(true)) |
| 062 | set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidResourcesDirURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOption) |error|:(reference)) |
| 063 | set ocidSubPathURLArray to (first item of listResponse) |
| 064 | if ocidSubPathURLArray ≠ (missing value) then |
| 065 | repeat with itemSubPathURL in ocidSubPathURLArray |
| 066 | set ocidExtensionName to itemSubPathURL's pathExtension() |
| 067 | if (ocidExtensionName as text) is "loctable" then |
| 068 | (ocidLocURLArray's addObject:(itemSubPathURL)) |
| 069 | end if |
| 070 | end repeat |
| 071 | end if |
| 072 | end repeat |
| 073 |
|
| 074 | ######################## |
| 075 | #格納用DICT |
| 076 | set ocidSchemaDict to refMe's NSMutableDictionary's alloc()'s init() |
| 077 |
|
| 078 | ######################## |
| 079 | # |
| 080 | repeat with itemFilePath in ocidStdOutArray |
| 081 | set ocidFilePath to itemFilePath's stringByStandardizingPath() |
| 082 | set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)) |
| 083 | set ocidXMLFilePathURL to (ocidFilePathURL's URLByAppendingPathComponent:("Contents/Resources/schema.xml") isDirectory:(false)) |
| 084 | set ocidXMLFilePath to ocidXMLFilePathURL's |path|() |
| 085 | set boolDirExists to (appFileManager's fileExistsAtPath:(ocidXMLFilePath) isDirectory:(false)) |
| 086 | if boolDirExists is true then |
| 087 | #NSDATA |
| 088 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
| 089 | set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidXMLFilePathURL) options:(ocidOption) |error|:(reference)) |
| 090 | set ocidReadData to (first item of listResponse) |
| 091 | #XML |
| 092 | set ocidOption to (refMe's NSXMLNodePreserveAll) + (refMe's NSXMLDocumentTidyXML) |
| 093 | set listResponse to (refMe's NSXMLDocument's alloc()'s initWithData:(ocidReadData) options:(ocidOption) |error|:(reference)) |
| 094 | set ocidReadXML to (first item of listResponse) |
| 095 | # |
| 096 | set listResponse to (ocidReadXML's objectsForXQuery:("//attribute/@name/string()") |error|:(reference)) |
| 097 | set ocidItemSchemaArray to (first item of listResponse) |
| 098 | repeat with itemKey in ocidItemSchemaArray |
| 099 | |
| 100 | (ocidSchemaDict's setObject:("") forKey:(itemKey)) |
| 101 | end repeat |
| 102 | end if |
| 103 | end repeat |
| 104 |
|
| 105 | ######################## |
| 106 | # |
| 107 | repeat with itemFilePath in ocidStdOutArray |
| 108 | set ocidFilePath to itemFilePath's stringByStandardizingPath() |
| 109 | set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)) |
| 110 | set ocidStringsURL to (ocidFilePathURL's URLByAppendingPathComponent:("Contents/Resources/en.lproj/schema.strings") isDirectory:(false)) |
| 111 | set ocidStringsPath to ocidStringsURL's |path|() |
| 112 | set boolDirExists to (appFileManager's fileExistsAtPath:(ocidStringsPath) isDirectory:(false)) |
| 113 | if boolDirExists is true then |
| 114 | set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidStringsURL) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)) |
| 115 | set ocidReadStrings to (first item of listResponse) |
| 116 | if ocidReadStrings ≠ (missing value) then |
| 117 | set ocidStringsArray to (ocidReadStrings's componentsSeparatedByString:(linefeed)) |
| 118 | set ocidItemDict to refMe's NSMutableDictionary's alloc()'s init() |
| 119 | repeat with itemLineString in ocidStringsArray |
| 120 | set itemLineString to (itemLineString's stringByReplacingOccurrencesOfString:("\";") withString:("")) |
| 121 | set itemLineString to (itemLineString's stringByReplacingOccurrencesOfString:("\" = \"") withString:(tab)) |
| 122 | set itemLineString to (itemLineString's stringByReplacingOccurrencesOfString:("\"") withString:("")) |
| 123 | set ocidLineArray to (itemLineString's componentsSeparatedByString:(tab)) |
| 124 | set itemKey to ocidLineArray's firstObject() |
| 125 | set itemValue to ocidLineArray's lastObject() |
| 126 | (ocidItemDict's setObject:(itemValue) forKey:(itemKey)) |
| 127 | end repeat |
| 128 | (ocidSchemaDict's addEntriesFromDictionary:(ocidItemDict)) |
| 129 | end if |
| 130 | end if |
| 131 | end repeat |
| 132 |
|
| 133 | ######################## |
| 134 | # |
| 135 | repeat with itemFileURL in ocidLocURLArray |
| 136 | # |
| 137 | set listResponse to (refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(itemFileURL) |error|:(reference)) |
| 138 | set ocidItemDict to (first item of listResponse) |
| 139 | set ocidAllKeys to ocidItemDict's allKeys() |
| 140 | set boolContain to (ocidAllKeys's containsObject:(strLangID)) |
| 141 | if boolContain is false then |
| 142 | set ocidItemLangDict to (ocidItemDict's objectForKey:("en")) |
| 143 | else |
| 144 | set ocidItemLangDict to (ocidItemDict's objectForKey:(strLangID)) |
| 145 | end if |
| 146 | (ocidSchemaDict's addEntriesFromDictionary:(ocidItemLangDict)) |
| 147 | |
| 148 | end repeat |
| 149 |
|
| 150 | ######################## |
| 151 | #schema plist |
| 152 |
|
| 153 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSSystemDomainMask)) |
| 154 | set ocidLibraryDirPathURL to ocidURLsArray's firstObject() |
| 155 | set ocidFilePathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Frameworks/CoreSpotlight.framework/Versions/A/Resources/schema.loctable") isDirectory:(false) |
| 156 | set listResponse to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL) |error|:(reference) |
| 157 | set ocidPlistDict to (first item of listResponse) |
| 158 |
|
| 159 | #AllKeys=LangIDs |
| 160 | set ocidAllKeys to ocidPlistDict's allKeys() |
| 161 | set boolContain to ocidAllKeys's containsObject:(strLangID) |
| 162 | if boolContain is false then |
| 163 | set strLangID to ("en") as text |
| 164 | end if |
| 165 |
|
| 166 | #Core |
| 167 | set ocidCoreDict to ocidPlistDict's objectForKey:(strLangID) |
| 168 | set ocidAllKeys to ocidCoreDict's allKeys() |
| 169 | repeat with itemKey in ocidAllKeys |
| 170 | set ocidItemValue to (ocidCoreDict's objectForKey:(itemKey)) |
| 171 | (ocidSchemaDict's setObject:(ocidItemValue) forKey:(itemKey)) |
| 172 | end repeat |
| 173 | ocidSchemaDict's addEntriesFromDictionary:(ocidCoreDict) |
| 174 |
|
| 175 | ######################## |
| 176 | # |
| 177 | set ocidAllKeys to ocidSchemaDict's allKeys() |
| 178 | set ocidSortedAllKeys to ocidAllKeys's sortedArrayUsingSelector:("localizedStandardCompare:") |
| 179 |
|
| 180 | #NSBANDLE |
| 181 | set strBundleID to "com.apple.finder" |
| 182 | set appBundle to refMe's NSBundle's bundleWithIdentifier:("com.apple.CoreSpotlight") |
| 183 | if appBundle = (missing value) then |
| 184 | set strAppPath to ("/System/Library/Frameworks/CoreSpotlight.framework") as text |
| 185 | set ocidAppPathStr to refMe's NSString's stringWithString:(strAppPath) |
| 186 | set ocidAppPath to ocidAppPathStr's stringByStandardizingPath() |
| 187 | set ocidAppPathURL to refMe's NSURL's fileURLWithPath:(ocidAppPath) isDirectory:(false) |
| 188 | set appBundle to refMe's NSBundle's bundleWithURL:(ocidAppPathURL) |
| 189 | end if |
| 190 |
|
| 191 |
|
| 192 | #FOR OUTPUTSTRING |
| 193 | set ocidMDString to refMe's NSMutableString's alloc()'s init() |
| 194 | ocidMDString's appendString:("| NO | KEY | VALUE|") |
| 195 | ocidMDString's appendString:(linefeed) |
| 196 | ocidMDString's appendString:("|:-----|:-----|:-----|") |
| 197 | ocidMDString's appendString:(linefeed) |
| 198 |
|
| 199 | #Line COUNTER |
| 200 | set numCntLine to 1 as integer |
| 201 | #REPEAT |
| 202 | repeat with itemKey in ocidSortedAllKeys |
| 203 | # set ocidValue to (appBundle's localizedStringForKey:(itemKey) value:(itemKey) table:("schema")) |
| 204 | |
| 205 | set ocidValue to (ocidSchemaDict's objectForKey:(itemKey)) |
| 206 | |
| 207 | set ocidValue to (ocidValue's stringByReplacingOccurrencesOfString:(linefeed) withString:("")) |
| 208 | set ocidValue to (ocidValue's stringByReplacingOccurrencesOfString:(return) withString:("")) |
| 209 | |
| 210 | |
| 211 | (ocidMDString's appendString:("| " & (numCntLine as text) & " | " & itemKey & " | " & ocidValue & " |")) |
| 212 | (ocidMDString's appendString:(linefeed)) |
| 213 | set numCntLine to (numCntLine + 1) as integer |
| 214 | end repeat |
| 215 |
|
| 216 | set strDefaultAnswer to ocidMDString as text |
| 217 |
|
| 218 |
|
| 219 | ############################## |
| 220 | # ダイアログ |
| 221 | set strIconFilePath to ("/System/Library/UserNotifications/Bundles/com.apple.identityservicesd.firewall.bundle/Contents/Resources/Apple Care_Mac.icns") as text |
| 222 | set aliasIconFilePath to (POSIX file strIconFilePath) as alias |
| 223 | set strMsg to ("テキストの戻り値です" & return & "コピーできます") as text |
| 224 | set strTitle to ("処理の戻り値です") as text |
| 225 | set strOK to ("ファイルに保存する") as text |
| 226 | set strCancel to ("キャンセル") as text |
| 227 | set strCopyToClipboard to ("クリップボードにコピー") as text |
| 228 | set listBotton to {strCopyToClipboard, strCancel, strOK} as list |
| 229 | try |
| 230 | tell application "SystemUIServer" |
| 231 | activate |
| 232 | set recordResult to (display dialog strMsg with title strTitle default answer strDefaultAnswer buttons listBotton default button strCopyToClipboard cancel button strCancel with icon aliasIconFilePath giving up after 20 without hidden answer) as record |
| 233 | end tell |
| 234 | on error strErrMsg number numErrNo |
| 235 | log strErrMsg & numErrNo |
| 236 | return false |
| 237 | end try |
| 238 |
|
| 239 | set strResponseButton to (button returned of recordResult) as text |
| 240 | set boolGaveUp to (gave up of recordResult) as boolean |
| 241 | if boolGaveUp is true then |
| 242 | log "時間切れ" |
| 243 | return false |
| 244 | end if |
| 245 |
|
| 246 | set strResponseText to (text returned of recordResult) as text |
| 247 | if strResponseButton is strOK then |
| 248 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 249 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 250 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
| 251 | set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias |
| 252 | set strIconFilePath to ("/System/Library/UserNotifications/Bundles/com.apple.identityservicesd.firewall.bundle/Contents/Resources/Apple Care_Mac.icns") as text |
| 253 | set aliasIconFilePath to (POSIX file strIconFilePath) as alias |
| 254 | # |
| 255 | set strBaseFileName to ("schema") as text |
| 256 | set strSaveExtension to ("md") as text |
| 257 | set strDefaultFileName to ("" & strBaseFileName & "." & strSaveExtension & "") as text |
| 258 | set strMsg to ("名前を決めてください" & return & "拡張子は" & strSaveExtension & "です") as text |
| 259 | set strPromptText to "名前を決めてください" as text |
| 260 | |
| 261 | |
| 262 | #ファイル名 ダイアログ |
| 263 | tell application "SystemUIServer" |
| 264 | activate |
| 265 | set aliasSaveFilePath to (choose file name strMsg default location aliasDesktopDirPath default name strDefaultFileName with prompt strPromptText) as «class furl» |
| 266 | end tell |
| 267 | #出力パス |
| 268 | set strSaveFilePath to (POSIX path of aliasSaveFilePath) as text |
| 269 | set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath) |
| 270 | set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath() |
| 271 | set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false) |
| 272 | set ocidContainerDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent() |
| 273 | #拡張子 |
| 274 | set strExtension to (ocidSaveFilePathURL's pathExtension()) as text |
| 275 | #最後のアイテムがファイル名 |
| 276 | set strFileName to (ocidSaveFilePathURL's lastPathComponent()) as text |
| 277 | #拡張子のつけ忘れ対策 |
| 278 | if strFileName does not contain strSaveExtension then |
| 279 | set strFileName to (strFileName & "." & strSaveExtension) as text |
| 280 | set ocidSaveFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName) |
| 281 | end if |
| 282 | #保存処理 |
| 283 | set ocidSaveString to refMe's NSMutableString's stringWithString:(strResponseText) |
| 284 | set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(return & linefeed) withString:(linefeed)) |
| 285 | set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(return) withString:(linefeed)) |
| 286 | set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(linefeed & linefeed) withString:(linefeed)) |
| 287 | set listDone to ocidSaveString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference) |
| 288 | return (first item of listDone) |
| 289 | else if strResponseButton is strCopyToClipboard then |
| 290 | #ペーストボードに格納する |
| 291 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
| 292 | set ocidText to (refMe's NSString's stringWithString:(strResponseText)) |
| 293 | appPasteboard's clearContents() |
| 294 | set boolDone to appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
| 295 | return boolDone |
| 296 | end if |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 | return (linefeed & ocidMDString & linefeed) as text |