How to access file information programmatically on Mac?

Setting:

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 details about that song in the More Info tag, for example.

enter image description here

Purpose:

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

Album, Title, Year Recorded
.
.
.

      

Problem:

I do not know how to do that. I'm totally new to Mac and Objective-C

/ Swift

( Objective-C

or Swift

what will I have to use to write this program, I guess?)

Is there an API where I can access this file information programmatically? And is there any other language I can use to write this kind of program? Something I already know like Java

, Python

etc.?

I don't have any code at the moment to show "What I have tried so far" as I am still looking for a starting point. Any pointers would be much appreciated.

+3


source to share


2 answers


As you noticed, the team mdls

can provide this metadata. You shouldn't try to take it apart. Instead, you can use the same API it is built on.

You need to get NSURL

for the file of interest. Then you can get a dictionary of your metadata attributes like:



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 required attributes.

+2


source


So I found one way to do this.

Mac provides a utility called mdls

that lists the file metadata. So if I do mdls

in my 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 the fields of interest.

Obviously, this is not a cool solution, but until I find something better, it will be done. I'll update my answer when I find something better.

0


source







All Articles