AppleScript and Cocoa Question

Discussion in 'Mac Programming' started by aznelementmaste, Aug 6, 2008.

  1. aznelementmaste macrumors newbie

    Mar 24, 2008
    I have an applescript thats supposed to execute when I press a button. Then, the applescript is supposed to get all the songs from iTunes, and then load the songs into a table view inside a drawer (the button opens the drawer and executes the applescript).

    Here's the applescript:

    tell application "iTunes" to get names of tracks
    It does work. However, the table view doesn't load the tracks that the applescript gets. I've made a new file just for the drawer, and connected the table view's data source inside the drawer to an object that's the name of the file containing the code.

    Here's the code:

    - (IBAction)makeDrawerVisible:(id)sender
    	[drawer setLeadingOffset:0];
    	[drawer setTrailingOffset:0];
    	[drawer openOnEdge:1];
    	[itunessongs initWithSource:@"/Users/ericlee/Desktop/Development/Xcode/Mac/StopWatch%20%5BWORK%20IN%20PROGRESS%5D/itunesmusic.scpt"];
    	NSLog(@"Getting iTunes songs");
    	[drawerTableView reloadData];
    The drawer shows up, and's just the songs that doesn't show up.

    Thanks for any help!
  2. kainjow Moderator emeritus


    Jun 15, 2000
  3. aznelementmaste thread starter macrumors newbie

    Mar 24, 2008
    Okay, thanks for the tip!

    I've changed it so that it uses SB, and I love it! The only hard part is actually reading the iTunes.h file that was generated and trying to find the variable that contains all the tracks.

    Does anyone know the variable that contains all the tracks?

  4. kainjow Moderator emeritus


    Jun 15, 2000
    Well, just from a cursory glance at the file, it looks like you'd have to loop through sources to find a source (iTunesSource) with kind==iTunesESrcLibrary. Then call libraryPlaylists on the source and look for a playlist with its class==iTunesLibraryPlaylist. Then you can call tracks on the playlist to get all tracks. HTH :)
  5. aznelementmaste thread starter macrumors newbie

    Mar 24, 2008

    I'll try to figure out what you just wrote...:p
  6. hhas macrumors regular

    Oct 15, 2007
    You might like to take a look at objc-appscript. Aside from being better designed and more compatible than SB (unlike SB, appscript is designed to work as much like AppleScript as possible), it also comes with much better developer tools: ASDictionary and ASTranslate. For example, running the following AppleScript command in ASTranslate:

    tell application "iTunes" to get name of tracks
    produces the equivalent ObjC code which you can adapt to suit:

    // To create glue:  osaglue  -o ITGlue  -p IT  iTunes
    ITApplication *itunes = [[ITApplication alloc] initWithName: @"iTunes"];
    ITReference *ref = [[itunes tracks] name];
    id result = [ref getItem];
    Incidentally, I've just added a couple of new iTunes-related examples that you may find of help, including an [unfinished] iTunesController project that shows how to retrieve playlist info and display it in NSTableViews via Cocoa Bindings.

Share This Page