简体   繁体   中英

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

[getTrack executeAndReturnError:nil] return a NSAppleEventDescriptor

To get a NSString from a NSAppleEventDescriptor :

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM