« [Text2SRT]テキストファイルをDaVinci Resolve用のSRT字幕ファイルに変換(少し手直し) | トップページ | savedSearch=スマートフォルダのアイコン »

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

コンテントタイプのツリーも同時に取得します
20240523062517962x245
あくまでも参考にしてください

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

サンプルソース(参考)
行番号ソース
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))
054####UTIの取得
055set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference))
056if (item 1 of listResponse) = (true) then
057  log "getResourceValue 正常処理"
058  set ocidUTType to (item 2 of listResponse)
059else if (item 3 of listResponse) ≠ (missing value) then
060  log (item 3 of listResponse)'s code() as text
061  log (item 3 of listResponse)'s localizedDescription() as text
062  return "getResourceValue エラーしました"
063end if
064###UTIのツリー構造取得
065#出力用のテキスト
066set ocidOutPutstring to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
067#親構造から先に取得して
068set ocidParentUTIArray to (ocidUTType's supertypes())'s allObjects()
069repeat with itemUTI in ocidParentUTIArray
070  (ocidOutPutstring's appendString:(itemUTI's identifier()))
071  (ocidOutPutstring's appendString:("\n"))
072end repeat
073#最後にUTIを追加
074set ocidUTI to ocidUTType's identifier()
075set strUTI to ocidUTI as text
076(ocidOutPutstring's appendString:(ocidUTI))
077(ocidOutPutstring's appendString:("\n"))
078
079###missing value対策
080if strUTI is "" then
081  tell application "Finder"
082    set objInfo to info for aliasFilePath
083    set strUTI to type identifier of objInfo as text
084  end tell
085  log strUTI
086end if
087
088##############################
089###選択したファイルのデフォルトのアプリケーションを取得
090set ocidAppPathURL to appShardWorkspace's URLForApplicationToOpenContentType:(ocidUTType)
091###デフォルトのアプリケーションが見つからない場合はファインダーにする
092if ocidAppPathURL = (missing value) then
093  set strFinderPath to "/System/Library/CoreServices/Finder.app"
094  set ocidFinderPathStr to (refMe's NSString's stringWithString:(strFinderPath))
095  set ocidFinderPath to ocidFinderPathStr's stringByStandardizingPath()
096  set ocidAppPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFinderPath) isDirectory:false)
097else
098  set ocidAppPathURL to ocidAppPathURL
099  log ocidAppPathURL's absoluteString() as text
100end if
101###アイコン名をPLISTから取得
102set ocidPlistPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/Info.plist") isDirectory:false
103set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL)
104set strIconFileName to (ocidPlistDict's valueForKey:("CFBundleIconFile")) as text
105###ICONのURLにして
106set strPath to ("Contents/Resources/" & strIconFileName) as text
107set ocidIconFilePathURL to ocidAppPathURL's URLByAppendingPathComponent:(strPath) isDirectory:false
108###拡張子の有無チェック
109set strExtensionName to (ocidIconFilePathURL's pathExtension()) as text
110if strExtensionName is "" then
111  set ocidIconFilePathURL to ocidIconFilePathURL's URLByAppendingPathExtension:"icns"
112end if
113##-->これがアイコンパス
114log ocidIconFilePathURL's absoluteString() as text
115###ICONファイルが実際にあるか?チェック
116set boolExists to appFileManager's fileExistsAtPath:(ocidIconFilePathURL's |path|)
117###ICONがみつかない時用にデフォルトを用意する
118if boolExists is false then
119  set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns"
120else
121  set aliasIconPath to ocidIconFilePathURL's absoluteURL() as alias
122end if
123
124#####ダイアログを前面に
125tell current application
126  set strName to name as text
127end tell
128####スクリプトメニューから実行したら
129if strName is "osascript" then
130  tell application "Finder" to activate
131else
132  tell current application to activate
133end if
134set 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)
135##############################
136###クリップボードコピー
137##############################
138
139if button returned of recordResult is "クリップボードにコピー" then
140  set strText to text returned of recordResult as text
141  ####ペーストボード宣言
142  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
143  set ocidText to (refMe's NSString's stringWithString:(strText))
144  appPasteboard's clearContents()
145  appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
146end if
147##############################
148#####自分自身を再実行
149##############################
150if button returned of recordResult is "再実行" then
151  tell application "Finder"
152    set aliasPathToMe to (path to me) as alias
153  end tell
154  run script aliasPathToMe with parameters "再実行"
155end if
AppleScriptで生成しました

|

« [Text2SRT]テキストファイルをDaVinci Resolve用のSRT字幕ファイルに変換(少し手直し) | トップページ | savedSearch=スマートフォルダのアイコン »

UTType」カテゴリの記事