2012-04-26 6 views
0

は、iTunesに通知を送信する方法があるので、現在の曲を教えてくれます。私はそれが歌が変わったときに通知を送るだろうが、私のアプリケーションが開くときに私は現在の曲を手に入れたいと思う。私はスクリプトブリッジの使用を避けようとしています。NSDistributedNotificationCenterとiTunes

答えて

2

通知でこれを行うことはできません。唯一のオプションは、Scripting Bridgeまたはその他のApple Event関連のメソッドです。

+0

他のapple-eventメソッドを探すのに適した場所はどこですか? –

+0

私は、スクリプトブリッジとは別に、Appleイベントを投稿するための他の方法の1つを使用して話していました。あなたは[絶対に恐ろしいCarbon C API](http://developer.apple.com/legacy/mac/library/documentation/AppleScript/Conceptual/AppleEvents/AppleEvents.pdf)を使って生のアップルイベントを投稿したり、 "NSAppleScript" Scripting Bridgeはこれを行うための最も簡単で推奨される方法です。 –

1

NSAppleScriptを使用してAppleScriptを実行できます。

例:タイトルと現在のトラックのアーティスト

NSAppleScript* theScript = [[NSAppleScript alloc] initWithSource: 
         [NSString stringWithFormat: 
          @"tell application \"iTunes\" to if running then\n" 
          @" try\n" 
          @"  tell current track to return name & return & artist\n" 
          @" end try\n" 
          @" return \"no track\"\n" 
          @"end if\n" 
          @"return \"iTunes is not open\"\n"]]; 
NSDictionary *errorDict; 
NSAppleEventDescriptor *resultDescriptor = [theScript executeAndReturnError:&errorDict]; 
NSString *title_artist = [resultDescriptor stringValue]; 
// do something with title_artist 

例を取得する:あなたが使用することができます:「リソースフォルダ」

NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"theScriptName" ofType:@"scpt"]; 
NSAppleScript *theScript = [[NSAppleScript alloc] initWithContentsOfURL: [NSURL fileURLWithPath:scriptPath] error: nil]; 

で別の解決策をAppleScriptのファイルを使用しますNSTask oscript経由でAppleScriptを実行する