使用ScriptingBridge获取当前歌曲的iTunes图稿 [英] Get iTunes Artwork for Current Song with ScriptingBridge

查看:104
本文介绍了使用ScriptingBridge获取当前歌曲的iTunes图稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找出如何通过脚本桥获取当前正在播放的歌曲的iTunes图稿.我已经了解到它可以用于某些歌曲,但是对于其他歌曲,我可以使用SIGABRT.我不确定问题可能是什么,因此将不胜感激任何帮助.这是我到目前为止的内容:

I have been trying to figure out how to get the iTunes artwork for the currently playing song with scripting bridge. I have gotten to a point where it works for some songs, but for others, I get a SIGABRT. I'm not sure what the issue could be, so any help would be greatly appreciated. Here is what I have so far:

iTunesApplication * iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
NSImage *songArtwork;
iTunesTrack *current = [iTunes currentTrack];
iTunesArtwork *artwork = (iTunesArtwork *)[[[current artworks] get] lastObject];
if(artwork != nil)
  songArtwork = [artwork data];
else
  songArtwork = [NSImage imageNamed:@"Image.tiff"];

NSMenuItem *artworkMenuItem = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
[songArtwork setSize:NSMakeSize(128, 128)];
[artworkMenuItem setImage:songArtwork];
[Menu insertItem:artworkMenuItem atIndex:0];

我为某些歌曲工作,并在菜单项中很好地显示了图稿,但对于其他歌曲,我在行上得到了一个SIGABRT:

I for some songs it works, and displays the artwork nicely in the menu item, but for others I get a SIGABRT on the line:

[songArtwork setSize:NSMakeSize(128, 128)];

控制台的输出如下:

2011-08-12 23:13:20.094 SongViewer[2146:707] -[NSAppleEventDescriptor setSize:]:     unrecognized selector sent to instance 0x102827f70
2011-08-12 23:13:20.095 SongViewer[2146:707] An uncaught exception was raised
2011-08-12 23:13:20.096 SongViewer[2146:707] -[NSAppleEventDescriptor setSize:]: unrecognized selector sent to instance 0x102827f70
2011-08-12 23:13:20.097 SongViewer[2146:707] (
0   CoreFoundation                      0x00007fff86f11986 __exceptionPreprocess + 198
1   libobjc.A.dylib                     0x00007fff8b04cd5e objc_exception_throw + 43
2   CoreFoundation                      0x00007fff86f9d5ae -[NSObject doesNotRecognizeSelector:] + 190
3   CoreFoundation                      0x00007fff86efe803 ___forwarding___ + 371
4   CoreFoundation                      0x00007fff86efe618 _CF_forwarding_prep_0 + 232
5   SongViewer                          0x0000000100002a83 -[IPMenulet awakeFromNib] + 4483
6   CoreFoundation                      0x00007fff86f089e1 -[NSObject performSelector:] + 49
7   CoreFoundation                      0x00007fff86f08962 -[NSSet makeObjectsPerformSelector:] + 274
8   AppKit                              0x00007fff8d9d9c27 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1245
9   AppKit                              0x00007fff8d9d01b9 loadNib + 322
10  AppKit                              0x00007fff8d9cf6b6 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217
11  AppKit                              0x00007fff8d9cf5d1 +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141
12  AppKit                              0x00007fff8d9cf514 +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364
13  AppKit                              0x00007fff8dc42355 NSApplicationMain + 398
14  SongViewer                          0x0000000100001882 main + 34
15  SongViewer                          0x0000000100001854 start + 52
)
2011-08-12 23:13:20.098 SongViewer[2146:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSAppleEventDescriptor setSize:]: unrecognized selector sent to instance 0x102827f70'
*** First throw call stack:
(
0   CoreFoundation                      0x00007fff86f11986 __exceptionPreprocess + 198
1   libobjc.A.dylib                     0x00007fff8b04cd5e objc_exception_throw + 43
2   CoreFoundation                      0x00007fff86f9d5ae -[NSObject doesNotRecognizeSelector:] + 190
3   CoreFoundation                      0x00007fff86efe803 ___forwarding___ + 371
4   CoreFoundation                      0x00007fff86efe618 _CF_forwarding_prep_0 + 232
5   SongViewer                          0x0000000100002a83 -[IPMenulet awakeFromNib] + 4483
6   CoreFoundation                      0x00007fff86f089e1 -[NSObject performSelector:] + 49
7   CoreFoundation                      0x00007fff86f08962 -[NSSet makeObjectsPerformSelector:] + 274
8   AppKit                              0x00007fff8d9d9c27 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1245
9   AppKit                              0x00007fff8d9d01b9 loadNib + 322
10  AppKit                              0x00007fff8d9cf6b6 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217
11  AppKit                              0x00007fff8d9cf5d1 +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141
12  AppKit                              0x00007fff8d9cf514 +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364
13  AppKit                              0x00007fff8dc42355 NSApplicationMain + 398
14  SongViewer                          0x0000000100001882 main + 34
15  SongViewer                          0x0000000100001854 start + 52
)
terminate called throwing an exception(gdb) 

如果有人知道什么地方可能出问题了,请告诉我!!

If anyone has any idea what could be wrong, please let me know!!

推荐答案

好,所以我知道了.解决方案是使用API​​提供的NSData原始数据,而不是NSImage.所以我用:

Ok so I figured it out. The solution is to use the NSData raw data provided by the API rather than the NSImage. So I used:

NSImage *songArtwork = [[NSImage alloc] initWithData:[artwork rawData]];

而不是

songArtwork = [artwork data];

这篇关于使用ScriptingBridge获取当前歌曲的iTunes图稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆