-//Runs an AppleScript and returns the result as an NSString after stripping quotes, if needed. It takes in script and automatically adds the tell iTunes and end tell statements.
-- (NSString *)runScriptAndReturnResult:(NSString *)script
-{
- AEDesc scriptDesc, resultDesc;
- Size length;
- NSString *result;
- Ptr buffer;
-
- script = [NSString stringWithFormat:@"tell application \"iTunes\"\n%@\nend tell", script];
-
- AECreateDesc(typeChar, [script cString], [script cStringLength],
-&scriptDesc);
-
- OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
-
- length = AEGetDescDataSize(&resultDesc);
- buffer = malloc(length);
-
- AEGetDescData(&resultDesc, buffer, length);
- AEDisposeDesc(&scriptDesc);
- AEDisposeDesc(&resultDesc);
- result = [NSString stringWithCString:buffer length:length];
- if ( (! [result isEqualToString:@""]) &&
- ([result characterAtIndex:0] == '\"') &&
- ([result characterAtIndex:[result length] - 1] == '\"') ) {
- result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
- }
- free(buffer);
- buffer = nil;
- return result;
-}
-