Reputation: 3919
How can I trick an NSWindow as displaying as active, without activating the application itself?
(The reason is that I wish to somehow trick Cocoa into doing that temporarily while I take a "screenshot" of the window.
It's very difficult to do this through all the methods I've experimented with, because things always happen not instantly but "soon". For example, I can't just activate the app, take the screenshot, and re-activate the previously active app, because "activate app" is an operation that just happens "sometime in the future", i.e. the app doesn't wait for the operation to finish before continuing. So it's all very tricky.)
Upvotes: 0
Views: 394
Reputation: 226
Worth a try:
NSDisableScreenUpdates();
...activate app/window, take screenshot, deactivate
NSEnableScreenUpdates();
Upvotes: 3
Reputation: 27073
In case of scriptable applications you can simply use 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
Use NSAppleScript
within Cocoa:
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];
Upvotes: 1