Sachin
Sachin

Reputation: 18777

How to access file info programmatically on Mac?

Setup:

I have a bunch of audio files on my new Mac (Yosemite). When I right click on any file and do "Get Info", it gives me nice detailed info about that song under "More Info" tag, e.g.

enter image description here

Objective:

I need to write a program where I can list, something like this for all songs in a directory:

Album, Title, Year Recorded
.
.
.

Problem:

I have no idea how to do this. I am totally new to Mac and Objective-C/Swift (Objective-C or Swift is what I will have to use to write this program I guess??)

Is there an API where I can access this file info programmatically? And is there any other language which I can use to write this sort of program? Something which I already know, like Java, Python, etc.?

I don't have any code as of now to show 'What I have tried so far', since I am still searching for the starting point. Any pointers would be much appreciated.

Upvotes: 3

Views: 1352

Answers (2)

Ken Thomases
Ken Thomases

Reputation: 90671

As you've noticed, the mdls command can provide this metadata. You should not try to parse that. Instead, you can use the same APIs that it is built on.

You need to obtain an NSURL for a file of interest. Then, you can obtain a dictionary of its metadata attributes like so:

MDItemRef item = MDItemCreateWithURL(NULL, (__bridge CFURLRef)url);
NSArray* names = @[ (__bridge NSString*)kMDItemAlbum, /* ... */ ];
NSDictionary* dictionary = CFBridgingRelease(MDItemCopyAttributes(item, (__bridge CFArrayRef)names));
CFRelease(item);

Now, dictionary contains the desired attributes.

Upvotes: 2

Sachin
Sachin

Reputation: 18777

So, I have found one way to do this.

Mac does provide a utility called mdls, which lists file metadata. So if I do the mdls on my about example file, I get this:

myuser00m:Hindi myuser$ mdls Pani\ Da\ Rang.mp3 
kMDItemAlbum                    = "Vicky Donor"
kMDItemAlternateNames           = (
    "/Users/myuser/Documents/My Stuff/Music/Hindi/Pani Da Rang.mp3"
)
kMDItemAudioBitRate             = 165000
kMDItemAudioChannelCount        = 2
kMDItemAudioEncodingApplication = "Eac * Lame"
kMDItemAudioSampleRate          = 44100
kMDItemAuthors                  = (
    "Ayushmann Khurrana"
)
kMDItemComposer                 = "Music: Abhishek - Akshay"
kMDItemContentCreationDate      = 2015-06-16 04:42:30 +0000
kMDItemContentModificationDate  = 2015-06-16 04:42:30 +0000
kMDItemContentType              = "public.mp3"
kMDItemContentTypeTree          = (
    "public.mp3",
    "public.audio",
    "public.audiovisual-content",
    "public.data",
    "public.item",
    "public.content"
)
kMDItemCopyright                = "www.Songs.PK"
kMDItemDateAdded                = 2015-07-26 19:52:49 +0000
kMDItemDisplayName              = "Pani Da Rang"
kMDItemDurationSeconds          = 240.8489795918368
kMDItemFSContentChangeDate      = 2015-06-16 04:42:30 +0000
kMDItemFSCreationDate           = 2015-06-16 04:42:30 +0000
kMDItemFSCreatorCode            = ""
kMDItemFSFinderFlags            = 0
kMDItemFSHasCustomIcon          = (null)
kMDItemFSInvisible              = 0
kMDItemFSIsExtensionHidden      = 0
kMDItemFSIsStationery           = (null)
kMDItemFSLabel                  = 0
kMDItemFSName                   = "Pani Da Rang.mp3"
kMDItemFSNodeCount              = (null)
kMDItemFSOwnerGroupID           = 784317889
kMDItemFSOwnerUserID            = 376797083
kMDItemFSSize                   = 5826388
kMDItemFSTypeCode               = ""
kMDItemKind                     = "MP3 audio"
kMDItemLogicalSize              = 5826388
kMDItemLyricist                 = "www.Songs.PK"
kMDItemMediaTypes               = (
    Sound
)
kMDItemMusicalGenre             = "Bollywood"
kMDItemPhysicalSize             = 5828608
kMDItemRecordingYear            = 2012
kMDItemTitle                    = "Pani Da Rang"
kMDItemTotalBitRate             = 165000
myuser00m:Hindi myuser$ 

Now I need to write some code to parse this and extract fields of interest.

Obviously, it is not that cool solution, but until I find something better, this will do. I will update my answer once I find something better.

Upvotes: 0

Related Questions