how can I pass file ref in objc scripting bridge (trying to add track to iTunes)

  • I'm trying to add an mp3 file to iTunes using the scripting bridge.
    I'm successful at instantiating an iTunes object and talking to it (able to
    retrieve playlist and track info), but I'm at a loss at to how to add a
    track.

    Code portions:

    // find already running iTunes

    iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:
    @"com.apple.iTunes"];

    // a test file to add (not shown - I used FSNewAliasFromPath and FSPathMakeRef
    to verify the path name was valid and could be turned into a file reference)

    NSString *aFilePath = @"//Users/jmead1/bach.mp3"; // test file

    // to add a file to iTunes library, add it to the library playlist. Script
    Editor shows the to: part as optional, but if I leave it out I get syntax
    error.

    // locate the iTunes library playlist

    SBElementArray *sources = [iTunes sources];

    iTunesSource *libsource=nil;

    uint i;

    for (i=0; i<[sources count]; i++)

    {

    if ([[sources objectAtIndex:i] kind] == 'kLib')

    {

    libsource = [sources objectAtIndex:i];

    break;

    }

    }

    SBElementArray *libPlaylists = [[libsource libraryPlaylists] copy];

    id anObject = [[libPlaylists objectAtIndex: 0] get];

    iTunesLibraryPlaylist *theLibraryPlaylist = anObject;

    // tried:
    // 1. using just the filename string as the array object (gives same error
    -35)
    // 2. using aliasHandle from successful FSNewAliasFromPath call - this gives
    syntax error in the array constructor
    // 3. using FSRef from successful FSPathMakeRef call - this gives syntax
    error in the array constructor
    // 4. put the filename in quotes and prefixed with "alias " - gives same
    error -35
    // 5. this attempt -- put the file name in quotes and prefixed with "POSIX
    file " - error -35 shown below.

    NSMutableString *anotherFileName = [@"POSIX file \"" mutableCopy];

    [anotherFileName appendString: aFilePath];

    [anotherFileName appendString: @"\""];

    NSArray *anNSArrry = [NSArray arrayWithObject: anotherFileName];

    iTunesTrack *anITunesTrack = [iTunes add: anNSArrry to
    : theLibraryPlaylist];

    Error result from my latest attempt is:

    *Error: NSGenericException, Apple event returned an error.  Event =
    'hook'\'Add '{ '----':[ 'utxt'("POSIX file \"//Users/jmead1/bach.mp3\"") ],
    'insh':'obj '{ 'want':'cLiP', 'from':'obj '{ 'want':'cSrc', 'from':'null'(),
    'form':'ID  ', 'seld':42 }, 'form':'ID  ', 'seld':1472 } }*

    *Error info = {*

    *    ErrorNumber = -35;*

    *}*

    *
    *

    What kind of object do I need to use to create the file name array?

    or am I completely misunderstanding how to do this?

    Thanks

    Jim
  • Le 22 févr. 08 à 23:35, James Mead a écrit :

    > I'm trying to add an mp3 file to iTunes using the scripting bridge.
    > I'm successful at instantiating an iTunes object and talking to it
    > (able to
    > retrieve playlist and track info), but I'm at a loss at to how to
    > add a
    > track.
    >
    > Code portions:
    >
    > // find already running iTunes
    >
    > iTunesApplication *iTunes = [SBApplication
    > applicationWithBundleIdentifier:
    > @"com.apple.iTunes"];
    >
    >
    > // a test file to add (not shown - I used FSNewAliasFromPath and
    > FSPathMakeRef
    > to verify the path name was valid and could be turned into a file
    > reference)
    >
    >
    > NSString *aFilePath = @"//Users/jmead1/bach.mp3"; // test file
    >
    >
    > // to add a file to iTunes library, add it to the library playlist.
    > Script
    > Editor shows the to: part as optional, but if I leave it out I get
    > syntax
    > error.
    >
    >
    > // locate the iTunes library playlist
    >
    > SBElementArray *sources = [iTunes sources];
    >
    > iTunesSource *libsource=nil;
    >
    > uint i;
    >
    > for (i=0; i<[sources count]; i++)
    >
    > {
    >
    > if ([[sources objectAtIndex:i] kind] == 'kLib')
    >
    > {
    >
    > libsource = [sources objectAtIndex:i];
    >
    > break;
    >
    > }
    >
    > }
    >
    >
    > SBElementArray *libPlaylists = [[libsource libraryPlaylists] copy];
    >
    > id anObject = [[libPlaylists objectAtIndex: 0] get];
    >
    > iTunesLibraryPlaylist *theLibraryPlaylist = anObject;
    >
    >
    > // tried:
    > // 1. using just the filename string as the array object (gives same
    > error
    > -35)
    > // 2. using aliasHandle from successful FSNewAliasFromPath call -
    > this gives
    > syntax error in the array constructor
    > // 3. using FSRef from successful FSPathMakeRef call - this gives
    > syntax
    > error in the array constructor
    > // 4. put the filename in quotes and prefixed with "alias " - gives
    > same
    > error -35
    > // 5. this attempt -- put the file name in quotes and prefixed with
    > "POSIX
    > file " - error -35 shown below.
    >
    > NSMutableString *anotherFileName = [@"POSIX file \"" mutableCopy];
    >
    > [anotherFileName appendString: aFilePath];
    >
    > [anotherFileName appendString: @"\""];
    >
    > NSArray *anNSArrry = [NSArray arrayWithObject: anotherFileName];
    >
    > iTunesTrack *anITunesTrack = [iTunes add: anNSArrry to
    > : theLibraryPlaylist];

    Use NSURL. All file functions accept either NSURL or FSRef. As I don't
    know how to pass FSRef, so I'm using NSURL and it works.

    >
    >
  • Thank you Jean-Daniel. NSURL worked.Jim

    On 2/22/08, Jean-Daniel Dupas <devlists...> wrote:
    >
    >
    >
    > Use NSURL. All file functions accept either NSURL or FSRef. As I don't
    > know how to pass FSRef, so I'm using NSURL and it works.
    >
    >>
    >>
    >
previous month february 2008 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29    
Go to today