UTI(uniform type identifier)の取得 ツリー構造付き 少し修正
UTIutilityのクローンを目指した
https://eclecticlight.co/taccy-signet-precize-alifix-utiutility-alisma/
https://eclecticlight.co/taccy-signet-precize-alifix-utiutility-alisma/
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | #com.cocolog-nifty.quicktimer.icefloe |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | use AppleScript version "2.8" |
007 | use framework "Foundation" |
008 | use framework "UniformTypeIdentifiers" |
009 | use framework "AppKit" |
010 | use scripting additions |
011 | property refMe : a reference to current application |
012 | ###初期化 |
013 | set appFileManager to refMe's NSFileManager's defaultManager() |
014 | set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace() |
015 | ############################## |
016 | ###デフォルトロケーション |
017 | set ocidUserDesktopPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
018 | set ocidUserDesktopPathURL to ocidUserDesktopPathArray's firstObject() |
019 | set aliasUserDesktopPath to ocidUserDesktopPathURL's absoluteURL() as alias |
020 | ############################## |
021 | #####ダイアログを前面に |
022 | tell current application |
023 | set strName to name as text |
024 | end tell |
025 | ####スクリプトメニューから実行したら |
026 | if strName is "osascript" then |
027 | tell application "Finder" to activate |
028 | else |
029 | tell current application to activate |
030 | end if |
031 | #####ファイルを選択 |
032 | set listUTI to {"public.item"} |
033 | set strPromptText to "ファイルを選んで下さい。" as text |
034 | set strMesText to "UTIを取得します" as text |
035 | |
036 | set aliasFilePath to (choose file strMesText default location aliasUserDesktopPath with prompt strPromptText of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
037 | |
038 | |
039 | try |
040 | set aliasFilePath to result as alias |
041 | on error |
042 | log "エラーしました" |
043 | return "エラーしました" |
044 | end try |
045 | if aliasFilePath is false then |
046 | return "エラーしました" |
047 | end if |
048 | ############################## |
049 | #####パス |
050 | set strFilePath to (POSIX path of aliasFilePath) as text |
051 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
052 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath |
053 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath)) |
054 | set strExtensionName to (ocidFilePathURL's pathExtension()) as text |
055 | ####UTIの取得 |
056 | set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference)) |
057 | if (item 1 of listResponse) = (true) then |
058 | log "getResourceValue 正常処理" |
059 | set ocidUTType to (item 2 of listResponse) |
060 | else if (item 3 of listResponse) ≠ (missing value) then |
061 | log (item 3 of listResponse)'s code() as text |
062 | log (item 3 of listResponse)'s localizedDescription() as text |
063 | return "getResourceValue エラーしました" |
064 | end if |
065 | ###UTIのツリー構造取得 |
066 | #出力用のテキスト |
067 | set ocidOutPutstring to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
068 | #親構造から先に取得して |
069 | set ocidParentUTIArray to (ocidUTType's supertypes())'s allObjects() |
070 | repeat with itemUTI in ocidParentUTIArray |
071 | (ocidOutPutstring's appendString:(itemUTI's identifier())) |
072 | (ocidOutPutstring's appendString:("\n")) |
073 | end repeat |
074 | #最後にUTIを追加 |
075 | set ocidUTI to ocidUTType's identifier() |
076 | set strUTI to ocidUTI as text |
077 | (ocidOutPutstring's appendString:(ocidUTI)) |
078 | (ocidOutPutstring's appendString:("\n")) |
079 | |
080 | ###missing value対策 |
081 | if strUTI is "" then |
082 | tell application "Finder" |
083 | set objInfo to info for aliasFilePath |
084 | set strUTI to type identifier of objInfo as text |
085 | end tell |
086 | log strUTI |
087 | end if |
088 | ##出力用テキストに拡張子を追加 |
089 | set ocidExtension to doGetExtensionType(ocidFilePathURL) |
090 | (ocidOutPutstring's appendString:("拡張子: " & ocidExtension & "\n")) |
091 | ##出力用テキストにMIMEを追加 |
092 | set strMimeType to doGetMimeType(ocidFilePathURL) |
093 | (ocidOutPutstring's appendString:("public.mime-type: " & strMimeType & "\n")) |
094 | ##FinderのKind |
095 | set aliasFilePath to (ocidFilePathURL's absoluteURL()) as alias |
096 | set recordFileInfo to (info for aliasFilePath) |
097 | set strKind to (kind of recordFileInfo) as text |
098 | (ocidOutPutstring's appendString:("種類: " & strKind & "\n")) |
099 | |
100 | ############################## |
101 | ###選択したファイルのデフォルトのアプリケーションを取得 |
102 | set ocidAppPathURL to appShardWorkspace's URLForApplicationToOpenContentType:(ocidUTType) |
103 | ###デフォルトのアプリケーションが見つからない場合はファインダーにする |
104 | if ocidAppPathURL = (missing value) then |
105 | set strFinderPath to "/System/Library/CoreServices/Finder.app" |
106 | set ocidFinderPathStr to (refMe's NSString's stringWithString:(strFinderPath)) |
107 | set ocidFinderPath to ocidFinderPathStr's stringByStandardizingPath() |
108 | set ocidAppPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFinderPath) isDirectory:false) |
109 | else |
110 | set ocidAppPathURL to ocidAppPathURL |
111 | log ocidAppPathURL's absoluteString() as text |
112 | end if |
113 | ###アイコン名をPLISTから取得 |
114 | set ocidPlistPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/Info.plist") isDirectory:false |
115 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL) |
116 | set strIconFileName to (ocidPlistDict's valueForKey:("CFBundleIconFile")) as text |
117 | ###ICONのURLにして |
118 | set strPath to ("Contents/Resources/" & strIconFileName) as text |
119 | set ocidIconFilePathURL to ocidAppPathURL's URLByAppendingPathComponent:(strPath) isDirectory:false |
120 | ###拡張子の有無チェック |
121 | set strExtensionName to (ocidIconFilePathURL's pathExtension()) as text |
122 | if strExtensionName is "" then |
123 | set ocidIconFilePathURL to ocidIconFilePathURL's URLByAppendingPathExtension:"icns" |
124 | end if |
125 | ##-->これがアイコンパス |
126 | log ocidIconFilePathURL's absoluteString() as text |
127 | ###ICONファイルが実際にあるか?チェック |
128 | set boolExists to appFileManager's fileExistsAtPath:(ocidIconFilePathURL's |path|) |
129 | ###ICONがみつかない時用にデフォルトを用意する |
130 | if boolExists is false then |
131 | set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns" |
132 | else |
133 | set aliasIconPath to ocidIconFilePathURL's absoluteURL() as alias |
134 | end if |
135 | |
136 | #####ダイアログを前面に |
137 | tell current application |
138 | set strName to name as text |
139 | end tell |
140 | ####スクリプトメニューから実行したら |
141 | if strName is "osascript" then |
142 | tell application "Finder" to activate |
143 | else |
144 | tell current application to activate |
145 | end if |
146 | set recordResult to (display dialog "type identifier 戻り値です" with title "uniform type identifier" default answer (ocidOutPutstring as text) buttons {"クリップボードにコピー", "終了", "再実行"} default button "再実行" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) |
147 | ############################## |
148 | ###クリップボードコピー |
149 | ############################## |
150 | |
151 | if button returned of recordResult is "クリップボードにコピー" then |
152 | set strText to text returned of recordResult as text |
153 | ####ペーストボード宣言 |
154 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
155 | set ocidText to (refMe's NSString's stringWithString:(strText)) |
156 | appPasteboard's clearContents() |
157 | appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
158 | end if |
159 | ############################## |
160 | #####自分自身を再実行 |
161 | ############################## |
162 | if button returned of recordResult is "再実行" then |
163 | tell application "Finder" |
164 | set aliasPathToMe to (path to me) as alias |
165 | end tell |
166 | run script aliasPathToMe with parameters "再実行" |
167 | end if |
168 | ############################## |
169 | #####public.filename-extension取得 |
170 | ############################## |
171 | to doGetExtensionType(argNSRUL) |
172 | set listResponse to (argNSRUL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference)) |
173 | if (item 3 of listResponse) = (missing value) then |
174 | #コンテントタイプUTIの取得 |
175 | set ocidContentType to (item 2 of listResponse) |
176 | #タグを収集して |
177 | set ocidTagDict to (ocidContentType's tags) |
178 | ##MIME-TYPEの取得 |
179 | set ocidExtensionType to ocidTagDict's valueForKey:("public.filename-extension") |
180 | ###tagにmimeが含まれていなかったら |
181 | if ocidExtensionType = (missing value) then |
182 | set strFilePath to (argNSRUL's |path|()) as text |
183 | set theCommandText to ("/usr/bin/file \"" & strFilePath & "\" | cut -f 2 -d ':'") as text |
184 | log theCommandText |
185 | set strMimeType to (do shell script theCommandText) as text |
186 | else |
187 | ###tagにEXTENSIONがあればそのまま値を利用する |
188 | # set strExtensionType to (ocidContentType's preferredFilenameExtension()) as text |
189 | #戻り値がARRAYなのでスペース区切りで繋げる |
190 | set ocidJoinText to ocidExtensionType's componentsJoinedByString:(" , ") |
191 | set strExtensionType to ocidJoinText as text |
192 | end if |
193 | else if (item 3 of listResponse) ≠ (missing value) then |
194 | log (item 3 of listResponse)'s code() as text |
195 | log (item 3 of listResponse)'s localizedDescription() as text |
196 | set strExtensionType to "不明" |
197 | end if |
198 | return strExtensionType |
199 | end doGetExtensionType |
200 | |
201 | ############################## |
202 | #####mime-type取得 |
203 | ############################## |
204 | to doGetMimeType(argNSRUL) |
205 | set listResponse to (argNSRUL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference)) |
206 | if (item 3 of listResponse) = (missing value) then |
207 | #コンテントタイプUTIの取得 |
208 | set ocidContentType to (item 2 of listResponse) |
209 | #タグを収集して |
210 | set ocidTagDict to (ocidContentType's tags) |
211 | ##MIME-TYPEの取得 |
212 | set ocidMimeType to ocidTagDict's valueForKey:"public.mime-type" |
213 | ###tagにmimeが含まれていなかったら |
214 | if ocidMimeType = (missing value) then |
215 | set strFilePath to (argNSRUL's |path|()) as text |
216 | set theCommandText to ("/usr/bin/file --mime-type \"" & strFilePath & "\" | cut -f 2 -d ':'") as text |
217 | log theCommandText |
218 | set strMimeType to (do shell script theCommandText) as text |
219 | else |
220 | ###tagにmimeがあればそのまま値を利用する |
221 | set strMimeType to (ocidContentType's preferredMIMEType()) as text |
222 | end if |
223 | |
224 | else if (item 3 of listResponse) ≠ (missing value) then |
225 | log (item 3 of listResponse)'s code() as text |
226 | log (item 3 of listResponse)'s localizedDescription() as text |
227 | set strMimeType to "不明" |
228 | end if |
229 | return strMimeType |
230 | end doGetMimeType |
AppleScriptで生成しました |
| 固定リンク
「UTType」カテゴリの記事
- シンボリックリンクのUTIの取得(2024.12.02)
- [UTType]処理対象のUTI(ファイルタイプ)か?チェックする 複数UTIでチェックする(2024.10.03)
- [UTType]処理対象のUTI(ファイルタイプ)か?チェックする(2024.09.30)
- UTI(uniform type identifier)の取得 ツリー構造付き 少し修正(2024.06.19)
- UTI(uniform type identifier)の取得 ツリー構造付き(2024.05.23)