I'm trying to find out if QuickTime player is paused or playing from Cocoa. I use the following small AppleScript in Script Debugger and AppleScript Editor and it returns true
or false
as expected:
tell application "QuickTime Player" to tell document 1 to return playing
However, the following code snippet in a Cocoa app isn't working:
NSString *source = @"tell application \"QuickTime Player\" to tell document 1 to return playing";
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:source];
NSDictionary *dict = nil;
NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&dict];
After stepping through the above code my debug console looks like this:
In case it's relevant, it takes about four seconds for the last debugger step, the one assigning a value to descriptor
, to execute, which seems very long to me.
I built a simple command line tool app with just the above within the @autorelease
block, and it works:
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *source = @"tell application \"QuickTime Player\" to tell document 1 to get playing";
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:source];
NSDictionary *dict = nil;
NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&dict];
NSLog(@"%@", descriptor);
NSLog(@"%@", dict);
}
return 0;
}
It's output (when QuickTime Player is running) is:
2014-05-17 11:48:07.255 Sandbox[52872:303] <NSAppleEventDescriptor: 'true'("true")>
2014-05-17 11:48:07.256 Sandbox[52872:303] (null)
Program ended with exit code: 0
Stepping through the sandbox code in the debugger executes that descriptor
assignment in less than a second. So what could be different in the app project that is preventing this from working?
It was one of those stupid mistakes that I figured out while my mind worked on it in the background while I worked at other things.
Sandbox. The app is sandboxed and the entitlements give it access to iTunes Apple Events, but I hadn't added QuickTime Player entitlement. The testing app I built wasn't sandboxed, which was why it worked fine.