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