1

I am trying to filter out an array of tracks from iTunes using Scripting Bridge in a Sandboxed app. The following works when the app is not Sandboxed, but not when it is:

if(!self.iTunes){
    self.iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
    self.library = [[[[self.iTunes sources] get] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"kind == %i", iTunesESrcLibrary]] objectAtIndex:0];
    iTunesLibraryPlaylist *libraryPlaylist = [[[[self.library playlists] get] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"specialKind == %i", iTunesESpKMusic]] objectAtIndex:0];
    self.musicTracks = [libraryPlaylist tracks];
}

// Create the playlist in iTunes
NSString *playlistName = selectedEquation.name;
iTunesPlaylist *playlist = [[[self.iTunes classForScriptingClass:@"playlist"] alloc] init];
[[self.library userPlaylists] insertObject:playlist atIndex:0];
[playlist setName:playlistName];


// Use persistent id to match between iTunesLibrary and ScriptingBridge
NSArray* iDArray = [self.tableTracks valueForKey:@"persistentID"];
for (NSNumber *decID in iDArray){
    NSString* hexID = [NSString stringWithFormat:@"%llx", (long long)[decID integerValue]];
    hexID = [hexID uppercaseString];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"persistentID", hexID];
    [[[self.musicTracks filteredArrayUsingPredicate:predicate] objectAtIndex:0] duplicateTo:playlist];
}

Is this a bug, or am I missing something? It will work if I convert self.musicTracks into an NSArray before I run the filter. However this slows things down tremendously, to the point where it's practically not useable.

Edit: Here is my entitlement file. I can definitely add playlists and tracks to them--it's just the filtering that doesn't work anymore.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.security.app-sandbox</key>
    <true/>
    <key>com.apple.security.assets.music.read-write</key>
    <true/>
    <key>com.apple.security.scripting-targets</key>
    <dict>
        <key>com.apple.iTunes</key>
        <array>
            <string>com.apple.iTunes.library.read-write</string>
        </array>
    </dict>
</dict>
</plist>
abroekhof
  • 796
  • 1
  • 7
  • 20
  • @TBlue right, good point. It's added to the question – abroekhof Jun 02 '13 at 12:59
  • This happened to me too. See this question: http://stackoverflow.com/questions/16420120/itunes-scripting-with-scripting-bridge-sandboxing – Alex Jun 02 '13 at 14:59

1 Answers1

1

Look, a workaround!

NSArray* sbIDs = [self.musicTracks arrayByApplyingSelector:@selector(persistentID)];
// Use persistent id to match between iTunesLibrary and ScriptingBridge
NSArray* iDArray = [self.tableTracks valueForKey:@"persistentID"];
for (NSNumber *decID in iDArray){
    NSString* hexID = [NSString stringWithFormat:@"%llx", (long long)[decID integerValue]];
    hexID = [hexID uppercaseString];
    // Persistent ID's have to have 16 characters in them.  Prepend zeros to make it so.
    while ([hexID length]<16) {
        hexID = [NSString stringWithFormat:@"%@%@", @"0", hexID];
    }
    NSInteger sbIndex = [sbIDs indexOfObject:hexID];
    [[self.musicTracks objectAtIndex:sbIndex] duplicateTo:playlist];
}

This should not be the answer, but it works and is quick.

abroekhof
  • 796
  • 1
  • 7
  • 20