Andrew Sheridan
Andrew Sheridan

Reputation: 19

Get usable description for Apple Event

So I have my code to get the current song that is playing by making a AppleScript object and using the returned value from AppleScript as the info that is sent to the user. Sadly it throws in a bunch of other junk that I need to get rid of.

Here is my code:

-(NSString *)getCurrentTrack {
    NSString *currentTrack = @"";
    NSAppleScript *getTrack = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\" to get the name of the current track"];


    currentTrack = [getTrack executeAndReturnError:nil];

    return currentTrack;

    //tell application "iTunes" to get the name of the current track
}

The returned value of currentTrack is:

<NSAppleEventDescriptor: 'utxt'("track name here")>

So I need to get rid of <NSAppleEventDescriptor: 'utxt'(" and the ")> at the end

Upvotes: 0

Views: 310

Answers (2)

jackjr300
jackjr300

Reputation: 7191

[getTrack executeAndReturnError:nil] return a NSAppleEventDescriptor

To get a NSString from a NSAppleEventDescriptor :

NSAppleEventDescriptor *resultDescriptor = [getTrack executeAndReturnError:nil];
return [resultDescriptor stringValue];

Upvotes: 1

user1364390
user1364390

Reputation:

I hope this works:

-(NSString *)getCurrentTrack {
    NSString *currentTrack = @"";
    NSAppleScript *getTrack = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\" to get the name of the current track"];


    currentTrack = [getTrack executeAndReturnError:nil];

    currentTrack = [currentTrack stringByReplacingOccurrencesOfString:@"\"" withString:@"'"];
    NSArray *left = [currentTrack componentsSeparatedByString:@"('"];
    NSString *text2 = [left objectAtIndex:1];
    NSArray *right = [text2 componentsSeparatedByString:@"')"];

    return [right objectAtIndex:0];


    //tell application "iTunes" to get the name of the current track
}

It should return track name here using the <NSAppleEventDescriptor: 'utxt'("track name here")> example you gave me, however I escaped the " manually, as I created a fake response like so: currentTrack = @"<NSAppleEventDescriptor: 'utxt'(\"track name here\")>";

Please tell me if it worked, -Joseph Rautenbach (you know me lol)

Upvotes: 0

Related Questions