如何在 Xcode 中使用 NSApplescript 将插图设置为 iTunes 歌曲?
这里有一些类似的问题,但没有涉及 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 框架,因为我需要翻译很多 applescript,所以它现在不适合我。
任何帮助将不胜感激。
there are some similars questions here but none involves NSApplescript.
I'm trying to add iTunes artwork to a selected track, my code is:
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
Where aFileName is the path of an mp3.
I'm testing this using Script Debugger 4.5 and it works fine, but when I copy the code to my Xcode project and run it wont set the artwork neither the other meta data.
But if I comment the "set data of artwork.." line, then it does set the other meta data (name, artist, etc)
My Xcode code is:
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];
}
I have been searching on google for many hours without luck.
I don't want to use ScriptBridge framework because I will need to translate lot of applescripts so its not an option for me right now.
Any help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现问题了,以防将来其他人到达这里,问题出在 HFS 路径格式样式上。
我使用 NSSearchPathForDirectoriesInDomains 获取我的作品的路径,然后将所有 @"/" 替换为 @":" 以将路径格式化为 HFS 样式。
问题是 HFS 路径格式样式需要一个完整路径,包括卷名和 NSSearchPathForDirectoriesInDomain 以及任何其他 NSFileManager 方法返回从 /Users/.... 开始的路径,我需要这样的路径: Macintosh HD/Users/...
到获取 HFS 完整路径我使用了以下代码:
这解决了问题,使用 pathFormatted 字符串设置艺术品的数据,它应该可以工作。
希望有一天这会对某人有所帮助 -)
Found the problem, in case someone else reach here in the future the problem was with the HFS path format style.
I was getting the path to my artwork using NSSearchPathForDirectoriesInDomains and then replacing all the @"/" for @":" to format the path in HFS style.
The problem is that HFS path format style need a full path including Volume Name and NSSearchPathForDirectoriesInDomain and any other NSFileManager methods return paths starting in /Users/.... and I needed a path like this: Macintosh HD/Users/...
To get the HFS full path I used the following code:
That solves the problem, use the pathFormatted string to set the data of artwork and it should work.
Hope this would help someone someday -)