Not able to close a window (QuickTime) with [NSWindow close] or [NSWindow performClose:(id)]
I am trying to o开发者_Go百科pen a video file with Quicktime and then close it. The video file is opening fine with [NSWorkspase openfile:path withapplication:@"Quick Time Player"]
. But when I am trying to close the Quicktime window nothing happens. Here is the code.
NSMutableArray *titles = [[NSApplication sharedApplication] orderedWindows];
int i;
id obj;
NSMutableString *mystring;
for(i=0; i< [titles count]; i++)
{
obj = (NSWindow*)[titles objectAtIndex:i];
mystring = [obj title];
if([mystring isEqualToString:@"PREVIEW"] == 1 )
{
[obj close];
}
//NSLog("Title : %@\n",(NSString*)[obj title]);
}
I would be obliged if any one could help me out.
You need to use NSWorkspace and NSRunningApplication to do this.
An example is given for the same question here:
NSRunningApplication - Terminate
Hey I found out the solution, it can easily be done through Apple Scripting, below is the script
tell application "QuickTime Player"
repeat with d in documents
if name of d is "abc.mp4" then
close d
end if
end repeat
end tell
its simple and easy. This script would give an error, however it would perform the required task. It will first grab control of the application QuickTIme Player and then iterate to find the window whose title is abc.mp4 and will eventually close it.
This won’t work because orderedWindows
returns only the windows from your application. There is no way to access a NSWindow
object from another application like QuickTime Player.
To do this you’ll have to use AppleEvents. There are many different ways to send them, but the easiest probably would be ScriptingBridge. Here is a pretty good tutorial.
精彩评论