« [ビギナー向] ファイルの移動とリネーム | トップページ | [Windows] Acrobat のアップデーターのURLの取得(修正版) »

UTI(uniform type identifier)の取得 ツリー構造付き 少し修正

UTIutilityのクローンを目指した
https://eclecticlight.co/taccy-signet-precize-alifix-utiutility-alisma/

Gqzx4yfbqaafjar
AppleScript サンプルコード

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

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
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "UniformTypeIdentifiers"
009use framework "AppKit"
010use scripting additions
011property refMe : a reference to current application
012###初期化
013set appFileManager to refMe's NSFileManager's defaultManager()
014set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
015##############################
016###デフォルトロケーション
017set ocidUserDesktopPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
018set ocidUserDesktopPathURL to ocidUserDesktopPathArray's firstObject()
019set aliasUserDesktopPath to ocidUserDesktopPathURL's absoluteURL() as alias
020##############################
021#####ダイアログを前面に
022tell current application
023  set strName to name as text
024end tell
025####スクリプトメニューから実行したら
026if strName is "osascript" then
027  tell application "Finder" to activate
028else
029  tell current application to activate
030end if
031#####ファイルを選択
032set listUTI to {"public.item"}
033set strPromptText to "ファイルを選んで下さい。" as text
034set strMesText to "UTIを取得します" as text
035
036set 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
039try
040  set aliasFilePath to result as alias
041on error
042  log "エラーしました"
043  return "エラーしました"
044end try
045if aliasFilePath is false then
046  return "エラーしました"
047end if
048##############################
049#####パス
050set strFilePath to (POSIX path of aliasFilePath) as text
051set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
052set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
053set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath))
054set strExtensionName to (ocidFilePathURL's pathExtension()) as text
055####UTIの取得
056set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference))
057if (item 1 of listResponse) = (true) then
058  log "getResourceValue 正常処理"
059  set ocidUTType to (item 2 of listResponse)
060else 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 エラーしました"
064end if
065###UTIのツリー構造取得
066#出力用のテキスト
067set ocidOutPutstring to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
068#親構造から先に取得して
069set ocidParentUTIArray to (ocidUTType's supertypes())'s allObjects()
070repeat with itemUTI in ocidParentUTIArray
071  (ocidOutPutstring's appendString:(itemUTI's identifier()))
072  (ocidOutPutstring's appendString:("\n"))
073end repeat
074#最後にUTIを追加
075set ocidUTI to ocidUTType's identifier()
076set strUTI to ocidUTI as text
077(ocidOutPutstring's appendString:(ocidUTI))
078(ocidOutPutstring's appendString:("\n"))
079
080###missing value対策
081if 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
087end if
088##出力用テキストに拡張子を追加
089set ocidExtension to doGetExtensionType(ocidFilePathURL)
090(ocidOutPutstring's appendString:("拡張子: " & ocidExtension & "\n"))
091##出力用テキストにMIMEを追加
092set strMimeType to doGetMimeType(ocidFilePathURL)
093(ocidOutPutstring's appendString:("public.mime-type: " & strMimeType & "\n"))
094##FinderのKind
095set aliasFilePath to (ocidFilePathURL's absoluteURL()) as alias
096set recordFileInfo to (info for aliasFilePath)
097set strKind to (kind of recordFileInfo) as text
098(ocidOutPutstring's appendString:("種類: " & strKind & "\n"))
099
100##############################
101###選択したファイルのデフォルトのアプリケーションを取得
102set ocidAppPathURL to appShardWorkspace's URLForApplicationToOpenContentType:(ocidUTType)
103###デフォルトのアプリケーションが見つからない場合はファインダーにする
104if 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)
109else
110  set ocidAppPathURL to ocidAppPathURL
111  log ocidAppPathURL's absoluteString() as text
112end if
113###アイコン名をPLISTから取得
114set ocidPlistPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/Info.plist") isDirectory:false
115set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL)
116set strIconFileName to (ocidPlistDict's valueForKey:("CFBundleIconFile")) as text
117###ICONのURLにして
118set strPath to ("Contents/Resources/" & strIconFileName) as text
119set ocidIconFilePathURL to ocidAppPathURL's URLByAppendingPathComponent:(strPath) isDirectory:false
120###拡張子の有無チェック
121set strExtensionName to (ocidIconFilePathURL's pathExtension()) as text
122if strExtensionName is "" then
123  set ocidIconFilePathURL to ocidIconFilePathURL's URLByAppendingPathExtension:"icns"
124end if
125##-->これがアイコンパス
126log ocidIconFilePathURL's absoluteString() as text
127###ICONファイルが実際にあるか?チェック
128set boolExists to appFileManager's fileExistsAtPath:(ocidIconFilePathURL's |path|)
129###ICONがみつかない時用にデフォルトを用意する
130if boolExists is false then
131  set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns"
132else
133  set aliasIconPath to ocidIconFilePathURL's absoluteURL() as alias
134end if
135
136#####ダイアログを前面に
137tell current application
138  set strName to name as text
139end tell
140####スクリプトメニューから実行したら
141if strName is "osascript" then
142  tell application "Finder" to activate
143else
144  tell current application to activate
145end if
146set 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
151if 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)
158end if
159##############################
160#####自分自身を再実行
161##############################
162if 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 "再実行"
167end if
168##############################
169#####public.filename-extension取得
170##############################
171to 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
199end doGetExtensionType
200
201##############################
202#####mime-type取得
203##############################
204to 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
230end doGetMimeType
AppleScriptで生成しました

|

« [ビギナー向] ファイルの移動とリネーム | トップページ | [Windows] Acrobat のアップデーターのURLの取得(修正版) »

UTType」カテゴリの記事