UTType

シンボリックリンクのUTIの取得


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
013
014#シンボリックリンク
015set strFilePath to ("/Library/Fonts/Arial Unicode.ttf") as text
016#パスURL
017set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
018set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
019set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
020
021#UTIの取得
022set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference))
023set ocidUTType to (item 2 of listResponse)
024set strSymLinkUTI to ocidUTType's identifier() as text
025
026return strSymLinkUTI
AppleScriptで生成しました

|

[UTType]処理対象のUTI(ファイルタイプ)か?チェックする 複数UTIでチェックする


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 "AppKit"
009use scripting additions
010property refMe : a reference to current application
011
012
013
014set listUTI to {"public.png", "com.adobe.pdf", "public.jpeg"} as list
015
016
017##チェックする画像
018set strFilePath to ("/Library/Documentation/License.lpdf/Contents/Resources/Japanese.lproj/License.pdf") as text
019set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
020set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
021set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
022#URLのUTTypeを取得
023set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference))
024set ocidUTType to (item 2 of listResponse)
025##
026set boolUTIContain to false as boolean
027repeat with itemUTI in listUTI
028  #親要素(この属性なら処理する)のUTType
029  set ocidChkUTType to (refMe's UTType's typeWithIdentifier:(itemUTI))
030  if ocidChkUTType = ocidUTType then
031    log "含まれているので処理対象=画像ファイル"
032    #初期値をTRURに変更
033    set boolUTIContain to true as boolean
034  end if
035end repeat
036#結果TRUEに変わっていれば画像かPDFと判断
037if boolUTIContain is true then
038  log "指定したUTIに含まれるファイルでした"
039end if
AppleScriptで生成しました

|

[UTType]処理対象のUTI(ファイルタイプ)か?チェックする


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#  ファイルが画像か?のチェック
005#
006# com.cocolog-nifty.quicktimer.icefloe
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008use AppleScript version "2.6"
009use framework "Foundation"
010use framework "AppKit"
011use scripting additions
012property refMe : a reference to current application
013
014
015#画像か?のチェック用
016property strUTI : ("public.image") as text
017
018##チェックする画像
019set strFilePath to ("/System/Library/Desktop Pictures/Solid Colors/Teal.png") as text
020set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
021set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
022set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
023#URLのUTTypeを取得
024set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference))
025set ocidUTType to (item 2 of listResponse)
026#URLのUTTyepの親要素を取得して
027set ocidParentUTIArray to (ocidUTType's supertypes())'s allObjects()
028#親要素(この属性なら処理する)のUTType
029set ocidChkUTType to refMe's UTType's typeWithIdentifier:(strUTI)
030#含まれているか?チェックする
031set boolContain to ocidParentUTIArray's containsObject:(ocidChkUTType)
032if boolContain is true then
033  log "含まれているので処理対象=画像ファイル"
034else if boolContain is false then
035  log "含まれてない=画像ではない"
036end if
AppleScriptで生成しました

AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#  ファイルが画像か?のチェック
005#
006# com.cocolog-nifty.quicktimer.icefloe
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008use AppleScript version "2.6"
009use framework "Foundation"
010use framework "AppKit"
011use scripting additions
012property refMe : a reference to current application
013
014
015#画像か?のチェック用
016property strUTI : ("public.image") as text
017
018##チェックする画像
019set strFilePath to ("/System/Library/Desktop Pictures/Solid Colors/Teal.png") as text
020set aliasFilePath to (POSIX file strFilePath) as alias
021#Finderを使ってUTIを取得する
022set strName to (name of current application) as text
023if strName is "osascript" then
024  tell application "Finder"
025    #Finder情報を取得して
026    set recordInfoFor to info for aliasFilePath
027  end tell
028else
029  tell current application
030    set recordInfoFor to info for aliasFilePath
031  end tell
032end if
033#UTIを取得
034set strItemUIT to (type identifier of recordInfoFor) as text
035set ocidUTType to (refMe's UTType's typeWithIdentifier:(strItemUIT))
036set ocidParentUTIArray to (ocidUTType's supertypes())'s allObjects()
037#親要素(この属性なら処理する)のUTType
038set ocidChkUTType to refMe's UTType's typeWithIdentifier:(strUTI)
039#含まれているか?チェックする
040set boolContain to ocidParentUTIArray's containsObject:(ocidChkUTType)
041if boolContain is true then
042  log "含まれているので処理対象=画像ファイル"
043else if boolContain is false then
044  log "含まれてない=画像ではない"
045end if
AppleScriptで生成しました

|

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で生成しました

|

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で生成しました

|

getBundleDocumentTypes

対象アプリケーションがオープンする事が出来るUTIを取得します

ダウンロード - getbundledocumenttypes.zip


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
###初期化
set appFileManager to refMe's NSFileManager's defaultManager()
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
##############################
###デフォルトロケーション
set ocidApplicationDirPathArray to (appFileManager's URLsForDirectory:(refMe's NSApplicationDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidApplicationDirPathURL to ocidApplicationDirPathArray's firstObject()
set aliasDefaultLocation to ocidApplicationDirPathURL's absoluteURL() as alias
##############################
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
##############################
#####ファイルを選択
set listUTI to {"public.item"}
set strPromptText to "アプリケーションを選んで下さい。" as text
set strMesText to "アプリケーションを選んで下さい。" as text
set aliasFilePath to (choose file strMesText default location aliasDefaultLocation with prompt strPromptText of type listUTI with invisibles without multiple selections allowed and showing package contents) as alias
try
  set aliasFilePath to result as alias
on error
  log "エラーしました"
return "エラーしました"
end try
if aliasFilePath is false then
return "エラーしました"
end if
##############################
#####アプリケーションのパス
set strFilePath to POSIX path of aliasFilePath as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:true
##PLISTのパス
set ocidPlistPathURL to ocidFilePathURL's URLByAppendingPathComponent:("Contents/Info.plist") isDirectory:false
##############################
#####CFBundleDocumentTypesの取得
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL)
set ocidDocTypeArray to ocidPlistDict's objectForKey:"CFBundleDocumentTypes"
if ocidDocTypeArray = (missing value) then
  set strOutPutText to "missing value" as text
else
  ####リストにする
  set listUTl to {} as list
  repeat with itemDocTypeArray in ocidDocTypeArray
    set listContentTypes to (itemDocTypeArray's objectForKey:"LSItemContentTypes")
    if listContentTypes = (missing value) then
      set ocidExtension to (itemDocTypeArray's objectForKey:"CFBundleTypeExtensions")
      set strClassName to ocidExtension's className() as text
      repeat with itemExtension in ocidExtension
set strExtension to itemExtension as text
set ocidContentTypes to (refMe's UTType's typeWithFilenameExtension:strExtension)
set strContentTypes to ocidContentTypes's identifier() as text
set strContentTypes to ("" & strContentTypes & "") as text
set end of listUTl to (strContentTypes)
      end repeat
    else
      repeat with itemContentTypes in listContentTypes
set strContentTypes to ("" & itemContentTypes & "") as text
set end of listUTl to (strContentTypes)
      end repeat
    end if
  end repeat
  ##############################
  set numCntUTI to (count of listUTl) as integer
  repeat with numCnt from 1 to numCntUTI
    if numCnt = 1 then
      set strOutPutText to ("{\"" & (item numCnt of listUTl as text) & "\",") as text
    else if numCnt = numCntUTI then
      set strOutPutText to strOutPutText & ("\"" & (item numCnt of listUTl as text) & "\"}") as text
    else
      set strOutPutText to strOutPutText & "\"" & (item numCnt of listUTl as text) & "\"," as text
    end if
  end repeat
end if
##set strText to listUTl as text
#####ダイアログに戻す
set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns" as alias
###ダイアログ
set recordResult to (display dialog "type identifier 戻り値です" with title "uniform type identifier" default answer strOutPutText buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" giving up after 20 with icon aliasIconPath without hidden answer)
###クリップボードコピー
if button returned of recordResult is "クリップボードにコピー" then
  set strText to text returned of recordResult as text
  ####ペーストボード宣言
  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
  set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
end if



|

[CFBundleDocumentTypes]アプリケーションの対応DocumentTypesを取得する


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKIt"
use framework "UniformTypeIdentifiers"
use scripting additions
property refMe : a reference to current application
property strBundleID : "com.apple.QuickTimePlayerX"

set listUTI to doGetUTI()
log listUTI

to doGetUTI()
  ###アプリケーションのURLを取得
  ###NSバンドルをUTIから取得
  set ocidAppBundle to refMe's NSBundle's bundleWithIdentifier:(strBundleID)
  if ocidAppBundle = (missing value) then
    ###NSバンドル取得できなかった場合
    set appNSWorkspace to refMe's NSWorkspace's sharedWorkspace()
    set ocidAppPathURL to appNSWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID)
  else
    set ocidAppPathURL to ocidAppBundle's bundleURL()
  end if
  ###Plistのパス
  set ocidPlistPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/Info.plist") isDirectory:false
  set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL)
  set ocidDocTypeArray to ocidPlistDict's objectForKey:"CFBundleDocumentTypes"
  if ocidDocTypeArray = (missing value) then
    set strOutPutText to "missing value" as text
  else
    ####リストにする
    set listUTl to {} as list
    ###対応ドキュメントタイプをリストにしていく
    repeat with itemDocTypeArray in ocidDocTypeArray
      set listContentTypes to (itemDocTypeArray's objectForKey:"LSItemContentTypes")
      if listContentTypes = (missing value) then
###拡張子の指定のみの場合
set ocidExtension to (itemDocTypeArray's objectForKey:"CFBundleTypeExtensions")
set strClassName to ocidExtension's className() as text
repeat with itemExtension in ocidExtension
set strExtension to itemExtension as text
set ocidContentTypes to (refMe's UTType's typeWithFilenameExtension:(strExtension))
set strContentTypes to ocidContentTypes's identifier() as text
set strContentTypes to ("" & strContentTypes & "") as text
set end of listUTl to (strContentTypes)
end repeat
      else
repeat with itemContentTypes in listContentTypes
set strContentTypes to ("" & itemContentTypes & "") as text
set end of listUTl to (strContentTypes)
end repeat
      end if
    end repeat
  end if
return listUTl
end doGetUTI

|

[CFBundleDocumentTypes]アプリケーションの対応DocumentTypesを取得する


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
###初期化
set appFileManager to refMe's NSFileManager's defaultManager()
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
##############################
###デフォルトロケーション
set ocidApplicationDirPathArray to (appFileManager's URLsForDirectory:(refMe's NSApplicationDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidApplicationDirPathURL to ocidApplicationDirPathArray's firstObject()
set aliasDefaultLocation to ocidApplicationDirPathURL's absoluteURL() as alias
##############################
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
##############################
#####ファイルを選択
set listUTI to {"public.item"}
set strPromptText to "アプリケーションを選んで下さい。" as text
set strMesText to "アプリケーションを選んで下さい。" as text
set aliasFilePath to (choose file strMesText default location aliasDefaultLocation with prompt strPromptText of type listUTI with invisibles without multiple selections allowed and showing package contents) as alias
try
  set aliasFilePath to result as alias
on error
  log "エラーしました"
return "エラーしました"
end try
if aliasFilePath is false then
return "エラーしました"
end if
##############################
#####アプリケーションのパス
set strFilePath to POSIX path of aliasFilePath as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:true
set ocidPlistPathURL to ocidFilePathURL's URLByAppendingPathComponent:("Contents/Info.plist") isDirectory:false
##############################
#####CFBundleDocumentTypesの取得
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL)
set ocidDocTypeArray to ocidPlistDict's objectForKey:"CFBundleDocumentTypes"
if ocidDocTypeArray = (missing value) then
  set strOutPutText to "missing value" as text
else
  ####リストにする
  set listUTl to {} as list
  repeat with itemDocTypeArray in ocidDocTypeArray
    set listContentTypes to (itemDocTypeArray's objectForKey:"LSItemContentTypes")
    if listContentTypes = (missing value) then
      set ocidExtension to (itemDocTypeArray's objectForKey:"CFBundleTypeExtensions")
      set strClassName to ocidExtension's className() as text
      repeat with itemExtension in ocidExtension
set strExtension to itemExtension as text
set ocidContentTypes to (refMe's UTType's typeWithFilenameExtension:strExtension)
set strContentTypes to ocidContentTypes's identifier() as text
set strContentTypes to ("" & strContentTypes & "") as text
set end of listUTl to (strContentTypes)
      end repeat
    else
      repeat with itemContentTypes in listContentTypes
set strContentTypes to ("" & itemContentTypes & "") as text
set end of listUTl to (strContentTypes)
      end repeat
    end if
  end repeat
  ##############################
  set numCntUTI to (count of listUTl) as integer
  repeat with numCnt from 1 to numCntUTI
    if numCnt = 1 then
      set strOutPutText to ("{\"" & (item numCnt of listUTl as text) & "\",") as text
    else if numCnt = numCntUTI then
      set strOutPutText to strOutPutText & ("\"" & (item numCnt of listUTl as text) & "\"}") as text
    else
      set strOutPutText to strOutPutText & "\"" & (item numCnt of listUTl as text) & "\"," as text
    end if
  end repeat
end if
##set strText to listUTl as text
#####ダイアログに戻す
set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns" as alias
###ダイアログ
set recordResult to (display dialog "type identifier 戻り値です" with title "uniform type identifier" default answer strOutPutText buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" giving up after 20 with icon aliasIconPath without hidden answer)
###クリップボードコピー
if button returned of recordResult is "クリップボードにコピー" then
  set strText to text returned of recordResult as text
  ####ペーストボード宣言
  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
  set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
end if



|

[UTType]拡張子からUTIを求める


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use scripting additions
property refMe : a reference to current application

log (refMe's UTType's typeWithFilenameExtension:"PDF")'s identifier() as text
-->(*com.adobe.pdf*)

|

[NSURLContentTypeKey]ファイルのUTIを求める(修正)


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
###初期化
set appFileManager to refMe's NSFileManager's defaultManager()
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
##############################
###デフォルトロケーション
set ocidUserDesktopPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidUserDesktopPathURL to ocidUserDesktopPathArray's firstObject()
set aliasUserDesktopPath to ocidUserDesktopPathURL's absoluteURL() as alias
##############################
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
#####ファイルを選択
set listUTI to {"public.item"}
set strPromptText to "ファイルを選んで下さい。" as text
set strMesText to "UTIを取得します" as text

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


try
  set aliasFilePath to result as alias
on error
  log "エラーしました"
return "エラーしました"
end try
if aliasFilePath is false then
return "エラーしました"
end if
##############################
#####パス
set strFilePath to (POSIX path of aliasFilePath) as text
set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:true)
####UTIの取得
set listResourceValue to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error|:(reference))
set ocidContentType to (item 2 of listResourceValue)
set strUTI to (ocidContentType's identifier) as text
log strUTI
###missing value対策
if strUTI is "" then
  tell application "Finder"
    set objInfo to info for aliasFilePath
    set strUTI to type identifier of objInfo as text
  end tell
  log strUTI
end if

##############################
#####UTIからデフォルトアプリケーションのアイコンパスを求める
set listResourceValue to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error|:(reference))
set ocidUTType to (item 2 of listResourceValue)
###選択したファイルのデフォルトのアプリケーションを取得
set ocidAppPathURL to appShardWorkspace's URLForApplicationToOpenContentType:(ocidUTType)
###デフォルトのアプリケーションが見つからない場合はファインダーにする
if ocidAppPathURL = (missing value) then
  set strFinderPath to "/System/Library/CoreServices/Finder.app"
  set ocidFinderPathStr to (refMe's NSString's stringWithString:(strFinderPath))
  set ocidFinderPath to ocidFinderPathStr's stringByStandardizingPath()
  set ocidAppPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFinderPath) isDirectory:false)
else
  log ocidAppPathURL's absoluteString() as text
end if
###アイコン名をPLISTから取得
set ocidPlistPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/Info.plist") isDirectory:false
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL)
set strIconFileName to (ocidPlistDict's valueForKey:("CFBundleIconFile")) as text
###ICONのURLにして
set strPath to ("Contents/Resources/" & strIconFileName) as text
set ocidIconFilePathURL to ocidAppPathURL's URLByAppendingPathComponent:(strPath) isDirectory:false
###拡張子の有無チェック
set strExtensionName to (ocidIconFilePathURL's pathExtension()) as text
if strExtensionName is "" then
  set ocidIconFilePathURL to ocidIconFilePathURL's URLByAppendingPathExtension:"icns"
end if
##-->これがアイコンパス
log ocidIconFilePathURL's absoluteString() as text
###ICONファイルが実際にあるか?チェック
set boolExists to appFileManager's fileExistsAtPath:(ocidIconFilePathURL's |path|)
###ICONがみつかない時用にデフォルトを用意する
if boolExists is false then
  set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns"
else
  set aliasIconPath to ocidIconFilePathURL's absoluteURL() as alias
end if

###ダイアログ
set recordResult to (display dialog "type identifier 戻り値です" with title "uniform type identifier" default answer strUTI buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" giving up after 20 with icon aliasIconPath without hidden answer)
###クリップボードコピー
if button returned of recordResult is "クリップボードにコピー" then
  set strText to text returned of recordResult as text
  ####ペーストボード宣言
  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
  set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
end if



|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File File Name Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Microsoft Fonts Microsoft Office Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom