2

I am new to Objective-C. I am now trying to get song information of selected track. But I can't.

I've found that following code

iTunesFileTrack *cuTrack = [iTunesApp.currentTrack get];
NSLog(@"result = %@",cuTrack);

outputs reference(?) of current track. Like:

2014-01-21 00:07:09.908 CommunicateWithiTunes[43052:303] result = <ITunesFileTrack @0x60800005de20: ITunesFileTrack id 12825 of ITunesUserPlaylist id 12773 of ITunesSource id 74 of application "iTunes" (166)>

so it can be re-usable like:

NSLog(@"name = %@", cuTrack.name);
NSLog(@"location = %@", cuTrack.location);

But if I re-write code as selection like:

iTunesFileTrack *selectedTrack = [iTunesApp.selection get];
NSLog(@"result = %@",selectedTrack);

outputs text(?) of selected track like:

2014-01-21 00:15:40.753 CommunicateWithiTunes[43145:303] result = (
 "<ITunesFileTrack @0x61000044fe70: ITunesFileTrack id 12825 of ITunesUserPlaylist id 12773 of ITunesSource id 74 of application \"iTunes\" (166)>"

so the result can't be re-usable.

Why this kind of difference occur? Or Can anyone tell me the correct way to get information of selected track?

xanadu6291
  • 931
  • 10
  • 20

1 Answers1

3

The selection is always an array (just like in AppleScript). Now just cycle through the items of the array. In your example there is only one. That is what the parentheses in your console log output are telling you: your second result is an array consisting of one item which is a reference to a file track, which itself is identical to your first result.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I should also say: Welcome to Objective-C! If you'd like to pause and study Objective-C a little before you proceed much further, my book teaches it to you, starting with C and building up to Objective-C: http://www.apeth.com/iOSBook/ch01.html The book is geared towards iOS, not Mac OS X, but the language is exactly the same in both, so those chapters might be helpful to you. – matt Jan 20 '14 at 16:29
  • Another option, if you are coming from AppleScript, is not to use Scripting Bridge, but instead to write your script in AppleScript inside a Cocoa app. That is what AppleScript-Objective C Bridge lets you do. You can write in the AppleScript language, but you can also talk to Objective-C when you need to. http://www.macosxautomation.com/applescript/apps/ – matt Jan 20 '14 at 16:31
  • Thanks matt!! I confirmed that my second example is array. Also, thanks for teach me your book. Yes I'm came from AppleScript. But I'd like to try Scripting Bridge for my study. – xanadu6291 Jan 20 '14 at 18:01
  • @xanadu: Scripting Bridge is neither a good ObjC/Cocoa technology nor a good AppleScript one, so probably not a good place to start if you're trying to learn either. The advantage of using the ASOC bridge is it that allows AppleScript to do what it's good at (application automation) and ObjC to do what it's good at (most everything else). ASOC's not perfect, but it's good enough for writing simpler programs entirely in AS, or more complex apps using a mixture of AS and ObjC, without too much impedance mismatch between the two. Shane Stanley's book (from Matt's link) is a good place to start. – foo Jan 21 '14 at 13:23
  • Thanks foo for comment. In fact, I'm a Japanese. So English guide itself to be much impedance for me. But I have bookmarked matt's iOSBook and referring it. – xanadu6291 Jan 21 '14 at 15:39
  • 1
    Anyway, @foo, AppleScript itself is a terrible language (and I say that even though I wrote a book about it!), and in any case why not let xanadu try what he wants to try and learn what he wants to learn? – matt Jan 21 '14 at 16:53