指定MIME TypeのUTI文字列などを取得する
指定MIME TypeからUTIの文字列などのさまざまな情報を取得するAppleScriptです。
漠然と身の回りにファイル拡張子、UTI、MIME-typeなどの情報が転がっていますが、それぞれの意味を考え直すと、向き不向きというか想定されている利用シーンなどが大幅に異なります。
| 種類 | 内容説明 |
|---|---|
| ファイル拡張子 | ファイルの形式を一意に特定するもの。ただし、「.jpg」「.jpeg」など若干の表記ゆらぎを許容。詐称できてしまう |
| UTI | ファイルのmacOS上での形式を一意に特定するもの。OS側が判断するものだが、拡張子次第。ただし、ゆらぎがない。UTI同士は階層構造を形成しており、上位のUTI(public.image)を指定することで下位UTIもまとめて指定できたりする |
| MIME-type | 当該のファイルがインターネット上のファイル交換時にどのように受け取られるかを示す識別子。AppleScriptバンドルは拡張子「.scptd」で、UTIは「com.apple.applescript.script-bundle」だが、MIME-typeを取得すると「inode/directory」となる |
と、ずいぶん違います。指定のファイルからMIME-typeを取得するだけであれば、こんな感じでしょうか。ただ、拡張子からMIME-typeを取得したり、MIME-typeからUTIを求めたりと、相互に変換する場合には(変換に意味がある場合には)いろいろと道具が必要です。
– Created by: Takaaki Naganoya
– Created on: 2019年04月14日
—
– Copyright © 2019 Piyomaru Software, All Rights Reserved
—
set aFile to POSIX path of (choose file)
set mRes to do shell script "/usr/bin/file -b –mime-type " & quoted form of aFile
–> "text/html"
UTI.frameworkを用いる場合にはこちら。
UTI.frameworkをビルドして使用しています。macOS 10.11/10.12/10.13では~/Library/Frameworksにインストールしてスクリプトエディタ上で本Scriptを実行すると記述どおりの結果が得られます。
macOS 10.14ではScript Debuggerを使用するか、あるいはSIPを解除してスクリプトエディタ上で実行することが可能です。自分でビルドして自分でCode Signしたコードぐらい、ホームディレクトリ下で勝手に実行させてほしいものです。
–> UTI.framework (To ~/Library/Frameworks)
– Created by: Takaaki Naganoya
– Created on: 2019年04月12日
—
– Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "UTI" –https://github.com/macmade/objc-uti
use scripting additions
set aUTIObj to (current application’s UTI’s UTIWithMIMEType:"image/gif")
set aRes to aUTIObj’s identifier()
–> com.compuserve.gif
set aRes to aUTIObj’s |description|() as string
–> "Graphics Interchange Format(GIF)"
set aRes to aUTIObj’s preferredOSType() as string
–> "GIFf"
set aRes to aUTIObj’s isDynamic() as boolean
–> false
set aRes to aUTIObj’s preferredFilenameExtension() as string
–> "gif"
set aRes to aUTIObj’s preferredMIMEType() as string
–> "image/gif"
set aRes to aUTIObj’s preferredNSPboardType() as string
–> "missing value"
set aRes to aUTIObj’s declaration() as record
–> {UTTypeIdentifier:"com.compuserve.gif", UTTypeDescription:"Graphics Interchange Format (GIF)", UTTypeTagSpecification:{|com.apple.ostype|:{"GIFf"}, |public.mime-type|:{"image/gif"}, |public.filename-extension|:{"gif"}}, UTTypeConformsTo:{"public.image"}}
set aRes to aUTIObj’s declaringBundleURL()’s |path|() as string
–> "/System/Library/CoreServices/CoreTypes.bundle"
set aRes to aUTIObj’s tagSpecifications() as record
–> {|com.apple.ostype|:{"GIFf"}, |public.mime-type|:{"image/gif"}, |public.filename-extension|:{"gif"}}
set aRes to aUTIObj’s conformsTo() as list
–> {"public.image"}
set aRes to aUTIObj’s referenceURL()
–> missing value
set aRes to aUTIObj’s |version|()
–> missing value