使用ScriptingBridge访问iTunes Library

之前坑过两次访问iTunes Library的方法

今天再用iTunesLibrary.framework的时候,突然发现资料库不能正确加载了,原因不明。不过还好,我们同样可以在Objective-C下使用ScriptingBridge,不用依赖于python。

为了访问/控制iTunes,我们需要先得到iTunes的ScriptingBridge的头文件,

$ sdef /Applications/iTunes.app | sdp -fh --basename iTunes

然后把得到的iTunes.h引入工程中,就可以开始了~

下面展示了一个Dump出所有音乐的封面的程序

#import <Foundation/Foundation.h>
#import <ScriptingBridge/ScriptingBridge.h>
#import "iTunes.h"
 

typedef enum : NSUInteger {
    iTunesArtworkTypeJPEG = 1246774599,
    iTunesArtworkTypePNG  = 1347307296,
} iTunesArtworkType;

int createDirectoryAtPath(NSString * path)
{
    NSFileManager * manager = [NSFileManagerdefaultManager];
    if (![manager fileExistsAtPath:path]) {
        NSError * error = nil;
        [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
        if (error) {
            printf("[ERROR] %s\n",error.localizedDescription.UTF8String);
            printf("Continue? [N] ");
            char c = getchar();
            if (c != 'Y' && c != 'y') {
                return -1;
            }
        }
    }
    return 0;
}

int main(int argc, const char * argv[])
{
    if (argc != 2) return 0;
    NSString * saveAt = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding];
    iTunesApplication * iTunesApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
    SBElementArray *iTunesSources = [iTunesApp sources];
    iTunesSource * library;
    for (iTunesSource * source in iTunesSources) {
        if (source.kind == iTunesESrcLibrary) {
            library = source;
            break;
        }
    }
    SBElementArray * libraryPlaylists = [library libraryPlaylists];
    iTunesLibraryPlaylist * libraryPlaylist = [libraryPlaylists objectAtIndex:0];
    SBElementArray * musicTracks = [libraryPlaylist fileTracks];
    for (iTunesFileTrack * track in musicTracks) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            createDirectoryAtPath(saveAt);
        });
        SBElementArray * artworks = track.artworks;
        NSString * trackSaveAt = [saveAt stringByAppendingPathComponent:track.name];
        if (createDirectoryAtPath(trackSaveAt) != -1) {
            NSUInteger index = 1;
            for (iTunesArtwork * artwork in artworks) {
                NSAppleEventDescriptor * descriptor = (NSAppleEventDescriptor *)artwork.format;
                if (descriptor.typeCodeValue == iTunesArtworkTypePNG) {
                    [artwork.rawData writeToFile:[trackSaveAt stringByAppendingFormat:@"/%ld.PNG",index] atomically:YES];
                } else if (descriptor.typeCodeValue == iTunesArtworkTypeJPEG) {
                    [artwork.rawData writeToFile:[trackSaveAt stringByAppendingFormat:@"/%ld.JPEG",index] atomically:YES];
                } else {
                
                }
            }
        } else {
          return 0;
        }
    }
    return 0;
}
声明: 本文为0xBBC原创, 转载注明出处喵~

发表评论

电子邮件地址不会被公开。 必填项已用*标注