2

这里有一些类似的问题,但没有一个涉及 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];
    }

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

任何帮助将不胜感激。

4

1 回答 1

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 字符串设置艺术品的数据,它应该可以工作。

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

于 2011-10-06T21:53:08.567 回答