Monday, December 11, 2017

#1 2012-04-11 05:17:47 pm

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 538
Website

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. neutral

Offline

 

#2 2012-04-11 11:55:51 pm

Aesthir
Member
Registered: 2010-11-25
Posts: 32

Re: get file types

You could use the “type identifier”  big_smile

eg:

Applescript:

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

Offline

 

#3 2012-04-12 01:40:09 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3202

Re: get file types

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

Offline

 

#4 2012-04-12 03:05:40 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 544

Re: get file types

Use System Events:

Applescript:

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:

Applescript:

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

Offline

 

#5 2012-04-12 05:31:35 am

Aesthir
Member
Registered: 2010-11-25
Posts: 32

Re: get file types

Yvan Koenig wrote:

Infos For is deprecated.


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…    big_smile
   

Applescript:

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

Offline

 

#6 2012-04-12 06:38:17 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 544

Re: get file types

Applescript:

if ItemInfo's kind ≠ "Folder" then return ItemInfo's type identifier

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

Offline

 

#7 2012-04-12 07:08:51 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2726
Website

Re: get file types

Aesthir wrote:

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…    big_smile


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.

Aesthir wrote:

Applescript:

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


On of the examples why system events is much better (no localize issues) because the word 'folder' is not used in every language.

Applescript:


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

Last edited by DJ Bazzie Wazzie (2014-07-30 08:08:01 am)

Offline

 

#8 2017-09-21 08:11:19 pm

JMichaelTX
Member
From:: Houston, TX (The Woodlands)
Registered: 2014-07-12
Posts: 139

Re: get file types

Joy wrote:

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. neutral


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.

Applescript:


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
[url]https://lists.apple.com/archives/applescript-users/2016/Jun/msg00068.html[/url]

2. Apple Uniform Type Idenfifiers (UTI) (Content Types)
[url]https://tinyurl.com/Apple-UTI[/url]

3. Apple Spotlight Metadata Attribute Keys
[url]http://tinyurl.com/apple-spotlight-metadata-keys[/url]

--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
*)


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
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


iMac-27 Late 2015 Retina 5K Screen (& others)
macOS 10.11.6 (El Capitan)

Offline

 

#9 2017-09-21 09:45:58 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5197

Re: get file types

If you're just after the type identifier, it's easy enough to get directly:

Applescript:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#10 2017-09-21 10:00:26 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5197

Re: get file types

This is evem simpler:

Applescript:

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)