/** Moves the focused window to specified `x`, `y` coordinates - Parameter x: new window screen x coordinate - Parameter y: new window screen y coordinate - Parameter pid: front most pid (use swift to get this) */ void move_focused_window(float x, float y, pid_t pid) { AXValueRef temp; CGSize windowSize; CGPoint windowPosition; CFStringRef windowTitle; AXUIElementRef frontMostApp; AXUIElementRef frontMostWindow; // Get the front most app frontMostApp = getFrontMostApp(pid); // Copy window attributes from frontMostApp AXUIElementCopyAttributeValue(frontMostApp, kAXFocusedWindowAttribute, (CFTypeRef *)&frontMostWindow); AXUIElementCopyAttributeValue(frontMostWindow, kAXSizeAttribute, (CFTypeRef *)&windowTitle); AXUIElementCopyAttributeValue(frontMostWindow, kAXSizeAttribute, (CFTypeRef *)&temp); AXValueGetValue(temp, kAXValueCGSizeType, &windowSize); CFRelease(temp); AXUIElementCopyAttributeValue(frontMostWindow, kAXPositionAttribute, (CFTypeRef *)&temp); AXValueGetValue(temp, kAXValueCGPointType, &windowPosition); CFRelease(temp); // printf("\n"); // CFShow(windowTitle); // printf("Moved window to (%f, %f)\n", windowPosition.x, windowPosition.y); // Change the window position to prevent error upon trying to move to same position windowPosition.x += 1; windowPosition.y += 1; temp = AXValueCreate(kAXValueCGPointType, &windowPosition); AXUIElementSetAttributeValue(frontMostWindow, kAXPositionAttribute, temp); CFRelease(temp); AXUIElementCopyAttributeValue(frontMostWindow, kAXPositionAttribute, (CFTypeRef *)&temp); AXValueGetValue(temp, kAXValueCGPointType, &windowPosition); CFRelease(temp); // Change the window position to the desired position that is provided in params windowPosition.x = x; windowPosition.y = y; temp = AXValueCreate(kAXValueCGPointType, &windowPosition); AXUIElementSetAttributeValue(frontMostWindow, kAXPositionAttribute, temp); CFRelease(temp); CFRelease(frontMostWindow); CFRelease(frontMostApp); }
static void EQResize (int pid) { ProcessSerialNumber psn; GetProcessForPID(pid, &psn); SetFrontProcessWithOptions(&psn, kSetFrontProcessFrontWindowOnly); AXValueRef temp; CGPoint windowPosition; AXUIElementRef frontMostApp; AXUIElementRef frontMostWindow; frontMostApp = AXUIElementCreateApplication(pid); AXUIElementSetAttributeValue(frontMostApp, kAXFrontmostAttribute, kCFBooleanTrue); // make application frontmost AXUIElementCopyAttributeValue(frontMostApp, kAXFocusedWindowAttribute, (CFTypeRef *)&frontMostWindow); AXUIElementSetAttributeValue(frontMostWindow, kAXMainAttribute, kCFBooleanTrue); // make window frontmost AXUIElementCopyAttributeValue(frontMostWindow, kAXPositionAttribute, (CFTypeRef *)&temp); AXValueGetValue(temp, kAXValueCGPointType, &windowPosition); CFRelease(temp); windowPosition.x = 0; windowPosition.y = -33; temp = AXValueCreate(kAXValueCGPointType, &windowPosition); AXUIElementSetAttributeValue(frontMostWindow, kAXPositionAttribute, temp); CFRelease(temp); CFRelease(frontMostWindow); CFRelease(frontMostApp); }
AXError AXUIElementSetOrigin(AXUIElementRef element, CGPoint originValue) { AXValueRef origin = AXValueCreate(kAXValueCGPointType, &originValue); if (origin == NULL) return kAXErrorFailure; AXError error = AXUIElementSetAttributeValue(element, kAXPositionAttribute, origin); CFRelease(origin); return error; }
AXError AXUIElementSetSize(AXUIElementRef element, CGSize sizeValue) { AXValueRef size = AXValueCreate(kAXValueCGSizeType, &sizeValue); if (size == NULL) return kAXErrorFailure; AXError error = AXUIElementSetAttributeValue(element, kAXSizeAttribute, size); CFRelease(size); return error; }
bool AXLibSetWindowSize(AXUIElementRef WindowRef, int Width, int Height) { bool Result = false; CGSize WindowSize = CGSizeMake(Width, Height); CFTypeRef WindowSizeRef = (CFTypeRef)AXValueCreate(kAXValueCGSizeType, (void*)&WindowSize); if(WindowSizeRef) { Result = AXLibSetWindowProperty(WindowRef, kAXSizeAttribute, WindowSizeRef); CFRelease(WindowSizeRef); } return Result; }
bool AXLibSetWindowPosition(AXUIElementRef WindowRef, int X, int Y) { bool Result = false; CGPoint WindowPos = CGPointMake(X, Y); CFTypeRef WindowPosRef = (CFTypeRef)AXValueCreate(kAXValueCGPointType, (const void*)&WindowPos); if(WindowPosRef) { Result = AXLibSetWindowProperty(WindowRef, kAXPositionAttribute, WindowPosRef); CFRelease(WindowPosRef); } return Result; }
/** Resizes the focused window to specified sizes `x`, `y` - Parameter x: new window width - Parameter y: new window height - Parameter pid: front most pid (use swift to get this) */ void resize_focused_window(float x, float y, pid_t pid) { AXValueRef temp; CGSize windowSize; CGPoint windowPosition; CFStringRef windowTitle; AXUIElementRef frontMostApp; AXUIElementRef frontMostWindow; // Get the frontMostApp frontMostApp = getFrontMostApp(pid); // Copy window attributes AXUIElementCopyAttributeValue(frontMostApp, kAXFocusedWindowAttribute, (CFTypeRef *)&frontMostWindow); AXUIElementCopyAttributeValue(frontMostWindow, kAXSizeAttribute, (CFTypeRef *)&windowTitle); AXUIElementCopyAttributeValue(frontMostWindow, kAXSizeAttribute, (CFTypeRef *)&temp); AXValueGetValue(temp, kAXValueCGSizeType, &windowSize); CFRelease(temp); AXUIElementCopyAttributeValue(frontMostWindow, kAXPositionAttribute, (CFTypeRef *)&temp); AXValueGetValue(temp, kAXValueCGPointType, &windowPosition); CFRelease(temp); //printf("\n"); // CFShow(windowTitle); //printf("RESIZE: Window is at (%f, %f) and has dimensions of (%f, %f) resized to (%f, %f) according to (%f, %f, %f, %f)\n", windowPosition.x, windowPosition.y, windowSize.width, windowSize.height, x1-x, y1 - y, x, y, x1, y1); // Change the window size windowSize.width = x; windowSize.height = y; // printf(" \n-- Resized window to dimensions: (%f, %f))\n\n", windowSize.width, windowSize.height); temp = AXValueCreate(kAXValueCGSizeType, &windowSize); AXUIElementSetAttributeValue(frontMostWindow, kAXSizeAttribute, temp); CFRelease(temp); CFRelease(frontMostWindow); CFRelease(frontMostApp); }
int main ( int argc, char ** argv ) { int i; AXValueRef temp; AXValueRef pos1; AXValueRef pos2; AXValueRef pos3; AXValueRef size1; AXValueRef size2; AXValueRef size3; CGSize windowSize1; CGSize windowSize2; CGSize windowSize3; CGPoint windowPosition1; CGPoint windowPosition2; CGPoint windowPosition3; CFStringRef windowTitle1; CFStringRef windowTitle2; CFStringRef windowTitle3; AXUIElementRef app1; AXUIElementRef app2; AXUIElementRef app3; AXUIElementRef frontWindow1; AXUIElementRef frontWindow2; AXUIElementRef frontWindow3; if (!hasAccessibilityAPIAuthorization()) { printf("Can't use accessibility API!\n"); return 1; } waitForFiveSeconds(); app1 = getFrontMostApp(); waitForFiveSeconds(); app2 = getFrontMostApp(); waitForFiveSeconds(); app3 = getFrontMostApp(); /** * Get app1's front most window. */ AXUIElementCopyAttributeValue( app1, kAXFocusedWindowAttribute, (CFTypeRef *)&frontWindow1 ); /* Get the title of the window */ AXUIElementCopyAttributeValue( frontWindow1, kAXTitleAttribute, (CFTypeRef *)&windowTitle1 ); /* Get the window size and position */ AXUIElementCopyAttributeValue( frontWindow1, kAXSizeAttribute, (CFTypeRef *)&temp ); AXValueGetValue(temp, kAXValueCGSizeType, &windowSize1); CFRelease(temp); AXUIElementCopyAttributeValue( frontWindow1, kAXPositionAttribute, (CFTypeRef *)&temp ); AXValueGetValue(temp, kAXValueCGPointType, &windowPosition1); CFRelease(temp); /** * Get app2's front most window. */ AXUIElementCopyAttributeValue( app2, kAXFocusedWindowAttribute, (CFTypeRef *)&frontWindow2 ); /* Get the title of the window */ AXUIElementCopyAttributeValue( frontWindow2, kAXTitleAttribute, (CFTypeRef *)&windowTitle2 ); /* Get the window size and position */ AXUIElementCopyAttributeValue( frontWindow2, kAXSizeAttribute, (CFTypeRef *)&temp ); AXValueGetValue(temp, kAXValueCGSizeType, &windowSize2); CFRelease(temp); AXUIElementCopyAttributeValue( frontWindow2, kAXPositionAttribute, (CFTypeRef *)&temp ); AXValueGetValue(temp, kAXValueCGPointType, &windowPosition2); CFRelease(temp); /** * Get app3's front most window. */ AXUIElementCopyAttributeValue( app3, kAXFocusedWindowAttribute, (CFTypeRef *)&frontWindow3 ); /* Get the title of the window */ AXUIElementCopyAttributeValue( frontWindow3, kAXTitleAttribute, (CFTypeRef *)&windowTitle3 ); /* Get the window size and position */ AXUIElementCopyAttributeValue( frontWindow3, kAXSizeAttribute, (CFTypeRef *)&temp ); AXValueGetValue(temp, kAXValueCGSizeType, &windowSize3); CFRelease(temp); AXUIElementCopyAttributeValue( frontWindow3, kAXPositionAttribute, (CFTypeRef *)&temp ); AXValueGetValue(temp, kAXValueCGPointType, &windowPosition3); CFRelease(temp); // printf("\n"); printf("OK, those three windows will now be locked to size and position\n"); printf("Press ctrl-c to exit\n"); // maintain the window sizes and positions until this application is closed while (true) { pos1 = AXValueCreate(kAXValueCGPointType, &windowPosition1); AXUIElementSetAttributeValue(frontWindow1, kAXPositionAttribute, pos1); size1 = AXValueCreate(kAXValueCGSizeType, &windowSize1); AXUIElementSetAttributeValue(frontWindow1, kAXSizeAttribute, size1); pos2 = AXValueCreate(kAXValueCGPointType, &windowPosition2); AXUIElementSetAttributeValue(frontWindow2, kAXPositionAttribute, pos2); size2 = AXValueCreate(kAXValueCGSizeType, &windowSize2); AXUIElementSetAttributeValue(frontWindow2, kAXSizeAttribute, size2); pos3 = AXValueCreate(kAXValueCGPointType, &windowPosition3); AXUIElementSetAttributeValue(frontWindow3, kAXPositionAttribute, pos3); size3 = AXValueCreate(kAXValueCGSizeType, &windowSize3); AXUIElementSetAttributeValue(frontWindow3, kAXSizeAttribute, size3); } // CFRelease(temp); CFRelease(pos1); CFRelease(pos2); CFRelease(pos3); CFRelease(size1); CFRelease(size2); CFRelease(size3); /* Clean up */ CFRelease(frontWindow1); CFRelease(frontWindow2); CFRelease(frontWindow3); CFRelease(app1); CFRelease(app2); CFRelease(app3); // end return 0; }