诱骗NSWindow显示为活跃

时间:2011-05-29 00:16:13

标签: objective-c cocoa

如何在不激活应用程序本身的情况下将NSWindow显示为活动状态?

(原因是我希望以某种方式欺骗Cocoa暂时这样做,同时我拍摄窗口的“截图”。

通过我尝试过的所有方法来实现这一点非常困难,因为事情总是不会立即发生,而是“很快”发生。例如,我不能只是激活应用程序,截取屏幕截图,并重新激活以前活动的应用程序,因为“激活应用程序”是一种“将来某个时候”发生的操作,即应用程序不会等待在继续之前完成操作。所以这一切都非常棘手。)

3 个答案:

答案 0 :(得分:3)

值得一试:

NSDisableScreenUpdates();
...activate app/window, take screenshot, deactivate
NSEnableScreenUpdates();

答案 1 :(得分:1)

如果是可编写脚本的应用程序,您只需使用AppleScript:

-- Get name of frontmost application at front
set active to name of (info for (path to frontmost application))

-- Activate specific application to take the screenshot from
tell application "Safari" to activate

-- Take screenshot and save it to the desktop
do shell script "screencapture -tjpg " & quoted form of ((POSIX path of (path to desktop)) & "Shot.png") as string

-- Make the previous frontmost application again frontmost
tell application active to activate

在Cocoa中使用NSAppleScript

NSString * source = @"set active to name of (info for (path to frontmost application))\n"
"tell application \"Safari\" to activate\n"
"do shell script \"screencapture -tjpg \" & quoted form of ((POSIX path of (path to desktop)) & \"Shot.png\") as string\n"
"tell application active to activate";

NSAppleScript *theScript = [[NSAppleScript alloc] initWithSource:source];
[theScript executeAndReturnError:nil];

答案 2 :(得分:0)

我怀疑有任何好方法可以做到这一点