In working in Xcode 4.6.1, I want to get a list of all windows, choose a specific window (maybe by windowID) and specify its Size (width & height), and Origin (X & Y).
I can get the Screen size, NSStringFromRect allows me to view the results in NSLog. NSRect is {NSPoint origin; NSSize size;} and in context of my display {{0, 0}, {2560, 1440}}.
I use the following to get my displays Size. (Thanks to Guillaume comment regarding C structs & casting):
-(void)awakeFromNib
{
NSRect screenRect = [[NSScreen mainScreen] frame];
CGSize myScreenSize = screenRect.size;
NSLog(@"myScreenSize = %d x %d", (int)myScreenSize.width, (int)myScreenSize.height);
// returns: myScreenSize = 2560 x 1440
}
@end
I would like to try setting the Size & Origin of another launched Application window(that doesn't belong to my process).
So first I get a list of all running Applications, and a windowID for each window, then choose a Window to move/resize.
Based on other searches this could be done using CGSPrivate.h, or the Accessibility api. I don't know how to use either of these yet to SET the size etc of a specific windowID/Number or PID
#import "rsAppDelegate.h"
@implementation rsAppDelegate
-(void)awakeFromNib
{
// get the size of the main screen
NSRect screenRect = [[NSScreen mainScreen] frame];
// devrived fromGuillaume's suggestion
CGSize myScreenSize = screenRect.size;
CGPoint myScreenOrigin = screenRect.origin;
NSLog(@"myScreenSize = %d x %d", (int)myScreenSize.width, (int)myScreenSize.height);
NSLog(@"Origin = %d , %d", (int)myScreenOrigin.x, (int)myScreenOrigin.y);
// To get a list of Application windows, the PID of the window, the window number, and the window bounds (origin, height, width)
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
for (NSMutableDictionary* entry in (__bridge NSArray*)windowList)
{
NSArray *ownerName = [entry objectForKey:(id)kCGWindowOwnerName];
NSInteger ownerPID = [[entry objectForKey:(id)kCGWindowOwnerPID] integerValue];
NSInteger windowNumber = [[entry objectForKey:(id)kCGWindowNumber] integerValue];
NSArray *ownerBounds = [entry objectForKey:(id)kCGWindowBounds];
NSLog(@"\nApp = %@ \nAppPID = %ld \nwindowNumber = %ld \nProgramBounds = %@", ownerName, (long)ownerPID, (long)windowNumber, ownerBounds);
}
CFRelease(windowList);
/*
OUTPUT:
myScreenSize = 2560 x 1440 | Origin = 0 , 0
App = Xcode
AppPID = 3260
windowNumber = 4493
ProgramBounds = {
Height = 1285;
Width = 1852;
X = 339;
Y = 32;
}
*/
// Knowing the name of the application, it's PID, window number, and bounds to a percentage of the screen:
// Set it's origin and size
}
@end
So now I know the screen size, and have all the available info about all the windows from each application. How do I specify a NEW Origin, Width, Height of an application with PID 3260, Window Number 4493?
newPos = AXValueCreate(kAXValueCGPointType, windowOrigin);
AXUIElementSetAttributeValue(chosenWindow, kAXPositionAttribute, newPos)
newSize = AXValueCreate(kAXValueCGSizeType, windowSize);
AXUIElementSetAttributeValue(chosenWindow, kAXSizeAttribute, newSize);
How do I specify the chosenWindow ? What format do I specify windowOrigin & windowSize ?
Here is the info I've found, but I don't know how to use:
kAXValueCGPointType is a wrapper for CGPoint; see CoreGraphics.h Declared in AXValue.h
kAXValueCGSizeType is a wrapper for CGSize;
These are AXValueType wrappers for other structures. You must use the AXValueCreate and AXValueGetValue functions to convert between the wrapped structure and the native structure.