如何在Xcode中使用NSApplescript设置iTunes歌曲的artwork



这里有一些类似的问题,但都不涉及NSApplescript。

我正在尝试添加iTunes artwork到选定的轨道,我的代码是:

set newFile to add aFileName as POSIX file to ipod_lib
set current_track to newFile
set jpegFilename to ":temp:artwork.jpg" as string
set data of artwork 1 of current_track to (read (file jpegFilename) as picture)
tell current_track
  set name to "Song Name" as string
  set artist to "Custom Artist" as string
  set album to "Custom Album" as string
  set year to "2011" as string
  set track number to "1" as number
  set track count to "38" as number
end tell

其中aFileName为mp3文件的路径。我正在使用脚本调试器4.5进行测试,它工作得很好,但是当我将代码复制到我的Xcode项目并运行时,它不会设置艺术作品,也不会设置其他元数据。但是如果我注释了"set data of artwork.."行,那么它就会设置其他元数据(name, artist等)

我的Xcode代码是:
NSString *scriptote = @"with timeout of 600 secondsn"
    "tell application "iTunes"n" 
...
    "set newFile to add aFileName as POSIX file to ipod_libn"
    "set current_track to newFilen"
    "set jpegFilename to ":temp:artwork.jpg" as stringn"
    // If a comment the following line, everything else works, if I left this line, no meta data is set.
    "set data of artwork 1 of current_track to (read (file jpegFilename) as picture)n" 
    "tell current_trackn"
    "set name to "Song Name" as stringn"
    "set artist to "Custom Artist" as stringn"
    "set album to "Custom Album" as stringn"
    "set year to "2011" as stringn"
    "set track number to "1" as numbern"
    "set track count to "38" as numbern"
    "end telln"
...    
;
    if ((ascript = [[NSAppleScript alloc] initWithSource:scriptote])) {
        status = [[ascript executeAndReturnError:&errorInfo] stringValue];
        if (!errorInfo) {
            // do something 
            NSLog(@"NO Error");
        } else {
            // process errors
           // NSRange errorRange = [[errorInfo objectForKey:@"NSAppleScriptErrorRange"] rangeValue];
            NSLog(@"Errorn Error line: ");
        }   
        [ascript release];
    }

我在谷歌上搜索了好几个小时都没有运气。我不想使用ScriptBridge框架,因为我需要翻译大量的applesscripts,所以它不是我现在的选择。

找到了问题,以防将来有人到达这里,问题是与HFS路径格式风格。

我正在使用nssearchpathfordirectortoriesindomains获取我的艺术作品的路径,然后替换所有的@":"来格式化HFS风格的路径。

问题是HFS路径格式风格需要一个完整的路径,包括卷名和nssearchpathfordirectortoriesindomain和任何其他NSFileManager方法返回路径从/Users/....开始我需要一个这样的路径:Macintosh HD/Users/…

为了获得HFS完整路径,我使用了以下代码:
// Get an HFS-style reference to a specified file
// (imagePath is an NSString * containing a POSIX-style path to the artwork image file)
NSURL *fileURL = [NSURL fileURLWithPath:imagePath];
NSString *pathFormatted = (NSString *)CFURLCopyFileSystemPath((CFURLRef)fileURL, kCFURLHFSPathStyle);

解决了这个问题,使用path格式化字符串来设置artwork的数据,应该可以工作了。

希望这能对某个人有所帮助-)

最新更新