Reputation: 1607
I am working on the app which resizes the selected window. It works successfully when any window is selected. But crashes when no window is selected.
Currently fetching front most window from the following code.
AXUIElementCopyAttributeValue(frontMostApp, kAXFocusedWindowAttribute, (CFTypeRef *)&frontMostWindow);
But how to detect that control is on desktop or all the windows are inactive.
Upvotes: 3
Views: 310
Reputation: 4465
You can use AppleScript to do this. When creating a new project in Xcode, you can choose "Cocoa-AppleScript" as your app type by going under OS X > Other to make an app that has both Obj-C and AppleScript. You can use this code to make the app that has focus do something:
tell current app to ...
You can use this code to change the size of the window
set the bounds of the front window to {x, y, x + width, y + height}
Here, x
and y
are the distance from the top-left corner.
You can add this to your project and modify the size of the window that is currently at the front. This means that the frontmost window of the app with focus will be resized. This is a working and fully interactive example:
set theWindows to the windows of the current application
if the length of theWindows is 0 then
display alert "There are no windows to resize"
else
display dialog "Enter x" default answer ""
set x to text returned of result as number
display dialog "Enter y" default answer ""
set y to text returned of result as number
display dialog "Enter width" default answer ""
set width to text returned of result as number
set width to (x + width)
display dialog "Enter height" default answer ""
set height to text returned of result as number
set height to (y + height)
tell current application to set the bounds of the front window to {x, y, width, height}
end if
In Yosemite you can create apps that have nothing at their core but AppleScript with the "Script Editor" application. In Mavericks and older systems the app is called "AppleScript Editor". If you need Objective-C for other parts of your app you can create a Cocoa-AppleScript program using the method I described earlier.
Upvotes: 1
Reputation: 56
AXUIElementCopyAttributeValue() returns AXError, so you can catch it and then check what happened.
AXError error = AXUIElementCopyAttributeValue(frontMostApp, kAXFocusedWindowAttribute, (CFTypeRef *)&frontMostWindow);
if (error != kAXErrorSuccess) {
//solve problems here
}
In your specific case there is a value of an error returned: kAXErrorNoValue = -25212
Upvotes: 4