till today i used to check extensions combined with lists of extension types to get the right media type. Aren’t there simpler methods to get :
audio, video, text, picture, script files, packed files ??
i don’t like ‘kind of’ because it isn’t provided with international strings.
set SomeFile to POSIX file "/Applications/Preview.app/"
set ItemInfo to (info for SomeFile)
if ItemInfo's kind ≠"Folder" then return ItemInfo's type identifier
This won’t work for folders and some files don’t have a “type identifier” but those are very rare cases.
Wow! that’s too bad, I use info for all the time… If Apple deprecates it (isn’t Apple trying to deprecate AppleScript entirely anyhow? ” seems to get less useful with every OSX release) then one can always call on Satimage.
set SomeFile to POSIX file "/Applications/Preview.app/"
set ItemInfo to (URL info for SomeFile)
if ItemInfo's kind ≠"Folder" then return ItemInfo's type identifier
I understand the confusion and disappointment but Apple is trying to get rid of file handling in it’s Standard Additions and I think in the future it will be all like system events and Finder for file handling instead. The whole reason is that from a Scripting Addition it’s much harder to access the Cocoa API containing the file managers etc. When using a scripting addition you’re forced to use standard C libraries (read: BSD) which can return different results than Cocoa. Because AppleScript is running on user level, it’s better to have the same outcome of commands as in other applications so using an scriptable application to use the same file manager as any other application does is more an obvious choice. I think for that reason alone the file manager commands are moved to system events.
On of the examples why system events is much better (no localize issues) because the word ‘folder’ is not used in every language.
tell application "System Events"
set itemInfo to properties of disk item ((path to desktop folder) as string)
if class of itemInfo is folder then
return "it's a folder"
end if
end tell
My apologies for updating such an old thread (2012), but it is the first hit on a google search for “AppleScript content type”.
Thanks to @ShaneStanley and @Chris Stone (@ccstone), we now have a much better way via ASObjC to get the content types of any file, and many other file metadata attributes as well. It is much faster than using a shell script with mdlsResults.
So, I thought I’d share a short script and handler I put together based on the work of Shane and Chris. I hope you will find it useful.
Any errors in the script are mine.
Please reply if you find any issues or error, and/or have suggestions for improvements.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set filePath to POSIX path of (choose file with prompt "Choose a file")
--- To get the Primary Content Type, Use this ---
# set kMDKeyStr to "kMDItemContentType"
--- To Get ALL Content Types for the File, Use this ---
-- (usually the Primary is the FIRST on this list)
set kMDKeyStr to "kMDItemContentTypeTree"
set fContentTypeList to my getMetaData(filePath, kMDKeyStr)
set AppleScript's text item delimiters to ", "
set fContentTypeStr to fContentTypeList as text
return "File: " & filePath & linefeed & "Content Types: " & fContentTypeStr
(* RESULTS:
File: /Users/Shared/Dropbox/SW/DEV/TEST/TEST New File using JXA.scpt
Content Types: com.apple.applescript.script, public.data, public.item, public.script, public.source-code, public.plain-text, public.text, public.content
*)
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on getMetaData(pPOSIXPath, pkMDKey)
(* VER: 1.0 2017-05-01
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
PURPOSE: Get Value(s) of Spotlight Metadata for Given Key
PARAMETERS:
• pPOSIXPath : POSIX path of file
• pkMDKey : Spotlight kMD Key Name
RETURNS: List of key values (even if only one value)
AUTHOR: JMichaelTX (heavy lifting by @ccstone)
REQUIRES: use framework "Foundation"
REF: 1. Script by @ccstone with @ShaneStanley
Jun 10, 2016
Getting Individual Metadata Items with ASObjC
https://lists.apple.com/archives/applescript-users/2016/Jun/msg00068.html
2. Apple Uniform Type Idenfifiers (UTI) (Content Types)
https://tinyurl.com/Apple-UTI
3. Apple Spotlight Metadata Attribute Keys
http://tinyurl.com/apple-spotlight-metadata-keys
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
*)
local curApp, nsPath, myNSURL, nsMetaItem, nsMDValue
set curApp to current application
set nsPath to curApp's NSString's stringWithString:pPOSIXPath
--- EXPAND TILDE & SYMLINK (if any exist) ---
set nsPath to nsPath's stringByResolvingSymlinksInPath()
--- GET THE NSURL, WHETHER OR NOT THE FILE/FOLDER EXISTS ---
set myNSURL to curApp's |NSURL|'s fileURLWithPath:nsPath
--- GET THE SPOTLIGHT METADATA VALUE ---
set nsMetaItem to current application's NSMetadataItem's alloc()'s initWithURL:myNSURL
set nsMDValue to nsMetaItem's valueForAttribute:pkMDKey
--- To Get Multiple Keys ---
--set nsMDValue to nsMetaItem's valuesForAttributes:{pkMDKey1, pkMDKey2}
return (nsMDValue as list)
end getMetaData
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you’re just after the type identifier, it’s easy enough to get directly:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set posixPath to POSIX path of (choose file)
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath
set {theResult, theUTI, theError} to (aURL's getResourceValue:(reference) forKey:(current application's NSURLTypeIdentifierKey) |error|:(reference))
if not theResult then error theError's localizedDescription() as text
return theUTI as text
As well as being a little more robust – you don’t have to worry about the state of the Spotlight database – it’s also actually quite a bit faster.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit" -- for NSWorkspace
use scripting additions
set posixPath to POSIX path of (choose file)
set theUTI to current application's NSWorkspace's sharedWorkspace()'s typeOfFile:posixPath |error|:(missing value)
if theUTI = missing value then error theError's localizedDescription() as text
return theUTI as text