20260404

【Spotlight】SlicesRootAttributes 一覧の取得


SlicesRootAttributes 一覧の取得

NOTE記事一覧ですnote.com

【スクリプトエディタで開く】 |

GET Spotlight Schema_2.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005GET Spotlight Schema.applescript
006
007SpotlightのスキーマをMDテーブルに出力します
008出力結果見本はこちら
009https://gist.github.com/force4u/2848fb489e8d013200ba46242648403d
010
011
012レインボーカーソルになることがありますが
013少し待ってみてください
014UIをロードするのに時間がかかっているだけの場面が多いです
015
016可能な限り、スキームの取得を試みたバージョン
017
018
019com.cocolog-nifty.quicktimer.icefloe *)
020----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
021use AppleScript version "2.8"
022use framework "Foundation"
023use framework "AppKit"
024use scripting additions
025
026property refMe : a reference to current application
027set appFileManager to refMe's NSFileManager's defaultManager()
028
029
030#GET LANG CODE
031set appLocale to refMe's NSLocale's currentLocale()
032set strLangID to (appLocale's objectForKey:(refMe's NSLocaleLanguageCode)) as text
033
034
035
036set strCmd to ("/usr/bin/mdimport -L") as text
037set strStdOut to (do shell script strCmd) as text
038set ocidStdOut to refMe's NSMutableString's stringWithString:(strStdOut)
039set ocidStdOut to (ocidStdOut's stringByReplacingOccurrencesOfString:("\",") withString:(""))
040set ocidStdOut to (ocidStdOut's stringByReplacingOccurrencesOfString:("\"") withString:(""))
041set ocidStdOut to (ocidStdOut's stringByReplacingOccurrencesOfString:(space & space) withString:(""))
042set ocidStdOutArray to ocidStdOut's componentsSeparatedByString:(return)
043#最初と最後を削除
044ocidStdOutArray's removeLastObject()
045ocidStdOutArray's removeObjectAtIndex:(0)
046
047########################
048#収集URLの格納
049set ocidLocURLArray to refMe's NSMutableArray's alloc()'s init()
050
051#コンテンツの収集
052set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
053set 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
058repeat 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
072end repeat
073
074########################
075#格納用DICT
076set ocidSchemaDict to refMe's NSMutableDictionary's alloc()'s init()
077
078########################
079#
080repeat 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
103end repeat
104
105########################
106#
107repeat 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
131end repeat
132
133########################
134#
135repeat 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   
148end repeat
149
150########################
151#schema plist
152
153set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSSystemDomainMask))
154set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
155set ocidFilePathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Frameworks/CoreSpotlight.framework/Versions/A/Resources/schema.loctable") isDirectory:(false)
156set listResponse to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL) |error|:(reference)
157set ocidPlistDict to (first item of listResponse)
158
159#AllKeys=LangIDs
160set ocidAllKeys to ocidPlistDict's allKeys()
161set boolContain to ocidAllKeys's containsObject:(strLangID)
162if boolContain is false then
163   set strLangID to ("en") as text
164end if
165
166#Core
167set ocidCoreDict to ocidPlistDict's objectForKey:(strLangID)
168set ocidAllKeys to ocidCoreDict's allKeys()
169repeat with itemKey in ocidAllKeys
170   set ocidItemValue to (ocidCoreDict's objectForKey:(itemKey))
171   (ocidSchemaDict's setObject:(ocidItemValue) forKey:(itemKey))
172end repeat
173ocidSchemaDict's addEntriesFromDictionary:(ocidCoreDict)
174
175########################
176#
177set ocidAllKeys to ocidSchemaDict's allKeys()
178set ocidSortedAllKeys to ocidAllKeys's sortedArrayUsingSelector:("localizedStandardCompare:")
179
180#NSBANDLE
181set strBundleID to "com.apple.finder"
182set appBundle to refMe's NSBundle's bundleWithIdentifier:("com.apple.CoreSpotlight")
183if 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)
189end if
190
191
192#FOR OUTPUTSTRING
193set ocidMDString to refMe's NSMutableString's alloc()'s init()
194ocidMDString's appendString:("| NO | KEY | VALUE|")
195ocidMDString's appendString:(linefeed)
196ocidMDString's appendString:("|:-----|:-----|:-----|")
197ocidMDString's appendString:(linefeed)
198
199#Line COUNTER
200set numCntLine to 1 as integer
201#REPEAT
202repeat 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
214end repeat
215
216set strDefaultAnswer to ocidMDString as text
217
218
219##############################
220#   ダイアログ
221set strIconFilePath to ("/System/Library/UserNotifications/Bundles/com.apple.identityservicesd.firewall.bundle/Contents/Resources/Apple Care_Mac.icns") as text
222set aliasIconFilePath to (POSIX file strIconFilePath) as alias
223set strMsg to ("テキストの戻り値です" & return & "コピーできます") as text
224set strTitle to ("処理の戻り値です") as text
225set strOK to ("ファイルに保存する") as text
226set strCancel to ("キャンセル") as text
227set strCopyToClipboard to ("クリップボードにコピー") as text
228set listBotton to {strCopyToClipboard, strCancel, strOK} as list
229try
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
234on error strErrMsg number numErrNo
235   log strErrMsg & numErrNo
236   return false
237end try
238
239set strResponseButton to (button returned of recordResult) as text
240set boolGaveUp to (gave up of recordResult) as boolean
241if boolGaveUp is true then
242   log "時間切れ"
243   return false
244end if
245
246set strResponseText to (text returned of recordResult) as text
247if 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)
289else 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
296end if
297
298
299
300return (linefeed & ocidMDString & linefeed) as text
AppleScriptで生成しました