get file types

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. :expressionless:

You could use the “type identifier” :smiley:

eg:

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.

Aesthir

Hello

Infos For is deprecated.
It may be dropped entirely more or less soon.

Yvan KOENIG (VALLAURIS, France) jeudi 12 avril 2012 08:39:59

Use System Events:

set SomeFile to (POSIX file "/Applications/Preview.app/") as alias -- drivename agnostic
tell application "System Events"
	properties of SomeFile
end tell
--> {short version:"5.0.2", container:folder "Santanni:Applications:", path:"Santanni:Applications:Preview.app:", file type:"APPL", volume:"Santanni", physical size:missing value, URL:"file://localhost/Applications/Preview.app/", id:"Preview.app,-100,557024", displayed name:"Voorvertoning", busy status:false, kind:"Programma", creator type:"prvw", version:"5.0.1, Copyright 2002-2009 Apple Inc.", name extension:"app", POSIX path:"/Applications/Preview.app", name:"Preview.app", modification date:date "woensdag 15 juli 2009 07:51:30", size:missing value, class:alias, type identifier:"com.apple.application-bundle", package folder:true, stationery:false, creation date:date "woensdag 15 juli 2009 07:51:30", visible:true, product version:""}

And yes, the ‘kind’ property is localised. I’ve used this for a long time:

do shell script "mdls -name kMDItemContentType " & quoted form of (POSIX path of theFile) -- language-independent

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. :smiley:

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
if ItemInfo's kind ≠ "Folder" then return ItemInfo's type identifier

That too is localised, so does not work in anything but (US) English.

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

on getMetaData(pPOSIXPath, pkMDKey)

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.

This is evem simpler:

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