如何使用Xcode中的NSApplescript将图稿设置为iTunes歌曲?

时间:2011-10-06 16:25:35

标签: objective-c xcode macos applescript artwork

这里有类似的问题,但没有涉及NSApplescript。

我正在尝试将iTunes图片添加到选定的曲目,我的代码是:

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的路径。 我正在使用Script Debugger 4.5测试它并且它工作正常,但是当我将代码复制到我的Xcode项目并运行它时,不会将图形设置为其他元数据。 但如果我评论“艺术品的设定数据......”行,那么它确实设置了其他元数据(名称,艺术家等)

我的Xcode代码是:

NSString *scriptote = @"with timeout of 600 seconds\n"
    "tell application \"iTunes\"\n" 
...
    "set newFile to add aFileName as POSIX file to ipod_lib\n"
    "set current_track to newFile\n"
    "set jpegFilename to \":temp:artwork.jpg\" as string\n"
    // 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_track\n"
    "set name to \"Song Name\" as string\n"
    "set artist to \"Custom Artist\" as string\n"
    "set album to \"Custom Album\" as string\n"
    "set year to \"2011\" as string\n"
    "set track number to \"1\" as number\n"
    "set track count to \"38\" as number\n"
    "end tell\n"
...    
;

    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(@"Error\n Error line: ");
        }   
        [ascript release];
    }

我一直在google上搜索好几个小时而没有运气。 我不想使用ScriptBridge框架,因为我需要翻译很多苹果脚本,所以它现在不适合我。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

发现问题,以防其他人将来到达此处,问题出在HFS路径格式样式上。

我使用NSSearchPathForDirectoriesInDomains获取我的作品的路径,然后将所有@“/”替换为@“:”以格式化HFS样式的路径。

问题是HFS路径格式样式需要一个完整的路径,包括卷名和NSSearchPathForDirectoriesInDomain,以及任何其他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);

解决了这个问题,使用pathFormatted字符串来设置图稿的数据,它应该可以工作。

希望有一天这会有所帮助 - )