Monday, May 20, 2019
  • Index
  •  » Code Exchange
  •  » Straightforward approach to getting a file's metadata attributes

#1 2018-10-24 03:04:57 am

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 290

Straightforward approach to getting a file's metadata attributes

One way of accessing a file's metadata attributes is via bash's mdls command. The challenge with that approach lies in the parsing of the output text for the file's attribute names and their values.

The current handler simplifies that process by taking advantage of the mdls command's plist option, which saves the output as a plist file. The plist file is then readily converted to a Cocoa NSDictionary via NSDictionary's dictionaryWithContentsOfFile: method. Finally, the NSDictionary is coerced to an AppleScript record.

The handler takes as its input argument a reference to the file whose metadata is to be accessed. The reference may be in the form of an HFS path, POSIX path, or AppleScript alias. The handler's return value is the file's metadata attributes in the form of an AppleScript record.

An alternative solution is the AST metadata for file command of DJ Bazzie Wazzie's AppleScript Toolbox plugin. This command combines both execution speed and coding efficiency. It's only drawback is the need to download and store the OSAX before it can be used.

Applescript:


use framework "Foundation"
use scripting additions

on fileMetadata(fileRef)
   try
       set posixPath to (fileRef as alias)'s POSIX path
   on error
       try
           set posixPath to (fileRef as POSIX file as alias)'s POSIX path
       on error
           error "File not found."
       end try
   end try
   set plistPath to do shell script "f=\"/tmp/$(uuidgen).plist\"; mdls " & posixPath's quoted form & " -plist $f; echo $f"
   set theMetadata to (current application's NSDictionary's dictionaryWithContentsOfFile:plistPath) as record
   current application's NSFileManager's defaultManager()'s removeItemAtPath:plistPath |error|:(missing value)
   return theMetadata
end fileMetadata

For example:

Applescript:


fileMetadata("/path/to/a/jpeg/file.jpg") --> {kMDItemKind:"JPEG image", kMDItemColorSpace:"RGB", kMDItemPixelWidth:4032, kMDItemPixelHeight:3024, ...}

Last edited by bmose (2018-10-24 03:15:43 am)


Filed under: file, mdls, metadata

Offline

 

#2 2018-10-24 04:23:22 am

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

Re: Straightforward approach to getting a file's metadata attributes

Another alternative is my Metadata Lib:

https://www.macosxautomation.com/apples … _Libs.html

like so:

Applescript:

use scripting additions
use script "Metadata Lib" version "2.0.0"

set theFile to choose file
set theMetadata to fetch metadata for item theFile

But if you're running 10.11 or later, you can also do it it directly in ASObjC:

Applescript:

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set theFile to choose file
set mdItem to current application's NSMetadataItem's alloc()'s initWithURL:theFile
set theMetadata to (mdItem's valuesForAttributes:(mdItem's attributes())) as record


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

Offline

 

#3 2018-10-24 06:21:20 am

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 290

Re: Straightforward approach to getting a file's metadata attributes

Shane Stanley wrote:

But if you're running 10.11 or later, you can also do it it directly in ASObjC

Your NSMetadataItem solution returns the same results as the mdls one I presented and is lightning fast. That does appear to be the preferred approach. Very nice!

I incorporated it into my original handler so that it will accept input in the form of an HFS path, POSIX path, or AppleScript alias:

Applescript:


on fileMetadata(fileRef)
   try
       set fileAlias to fileRef as alias
   on error
       try
           set fileAlias to fileRef as POSIX file as alias
       on error
           error "File not found."
       end try
   end try
   set mdItem to current application's NSMetadataItem's alloc()'s initWithURL:fileAlias
   set theMetadata to (mdItem's valuesForAttributes:(mdItem's attributes())) as record
   return theMetadata
end fileMetadata

Can you kindly explain why the initWithURL: method accepts an AppleScript alias argument? I have always created an NSURL object in those situations, but using an AppleScript alias would oftentimes be easier.

Last edited by bmose (2018-10-24 06:25:11 am)

Offline

 

#4 2018-10-24 04:07:01 pm

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

Re: Straightforward approach to getting a file's metadata attributes

bmose wrote:

Your NSMetadataItem solution returns the same results as the mdls one I presented and is lightning fast.



Avoiding the overhead of do shell script is nearly always going to be a big win in performance.

bmose wrote:

I incorporated it into my original handler so that it will accept input in the form of an HFS path, POSIX path, or AppleScript alias



Which is what the library version does (including abbreviated POSIX paths). You're reinventing the wheel ;-)

bmose wrote:

Can you kindly explain why the initWithURL: method accepts an AppleScript alias argument?



As of macOS 10.11, aliases and file references («class furl») are automatically bridged to NSURLs in this sort of situation. That's one of the reasons my code includes the AppleScript 2.5 use statement (the other being that 10.11 is also needed for automatic conversion between Cocoa dates and AppleScript dates. The library version handles both conversions pre-10.11, at some potential cost to performance.)

By the way, sadly DJ's AppleScript Toolbox is no longer:

https://astoolbox.wordpress.com/2018/10 … t-toolbox/


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

Offline

 

#5 2018-10-24 10:04:12 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 290

Re: Straightforward approach to getting a file's metadata attributes

Shane Stanley wrote:

As of macOS 10.11, aliases and file references («class furl») are automatically bridged to NSURLs in this sort of situation.

Thanks for the explanation. Very helpful info.

By the way, sadly DJ's AppleScript Toolbox is no longer

That's very unfortunate. The AppleScript Toolbox was a great resource.

Offline

 

#6 2018-10-24 10:19:31 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 290

Re: Straightforward approach to getting a file's metadata attributes

A minor modification was made to the handler so that it now returns an empty "record" (really an empty list) instead of throwing an error when it encounters the occasional file system item that returns no metadata attributes:

Applescript:


use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

on fileMetadata(fileRef)
   try
       set fileAlias to fileRef as alias
   on error
       try
           set fileAlias to fileRef as POSIX file as alias
       on error
           error "File not found."
       end try
   end try
   set mdItem to current application's NSMetadataItem's alloc()'s initWithURL:fileAlias
   set theMetadata to {}
   try
       set theMetadata to (mdItem's valuesForAttributes:(mdItem's attributes())) as record
   end try
   return theMetadata
end fileMetadata

Offline

 
  • Index
  •  » Code Exchange
  •  » Straightforward approach to getting a file's metadata attributes

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)