如何在Mac上以编程方式访问文件信息? [英] How to access file info programmatically on Mac?
问题描述
我的新Mac(优胜美地)上有一堆音频文件.当我右键单击任何文件并执行获取信息"时,它会在更多信息"标签下为我提供有关该歌曲的详细信息,例如
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.
我需要编写一个可以列出的程序,对于目录中的所有歌曲,类似这样:
I need to write a program where I can list, something like this for all songs in a directory:
Album, Title, Year Recorded
.
.
.
问题:
我不知道该怎么做.我对Mac和Objective-C
/Swift
完全陌生(Objective-C
或Swift
是我想用来编写此程序的方式,我想是吗?)
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??)
是否有一个API,可以通过编程方式访问此文件信息?还有其他我可以用来编写此类程序的语言吗?我已经知道的东西,例如Java
,Python
等?
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.
推荐答案
您已经注意到,mdls
命令可以提供此元数据.您不应该尝试解析它.相反,您可以使用与其建立相同的API.
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.
您需要获取感兴趣的文件的NSURL
.然后,您可以获取其元数据属性的字典,如下所示:
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);
现在,dictionary
包含所需的属性.
Now, dictionary
contains the desired attributes.
这篇关于如何在Mac上以编程方式访问文件信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!