/** Prints the size of the window of the given pid - Parameter pid: the pid of the desired window */ void print_window_size(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(" SIZE OF WINDOW IS: %f, %f \n", windowSize.width, windowSize.height); }
AXUIElementRef getFrontMostWindow() { AXUIElementRef frontMostWindow; AXUIElementCopyAttributeValue(getFrontMostApp(), kAXFocusedWindowAttribute, (CFTypeRef *)&frontMostWindow); return frontMostWindow; }
/** 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); }
/** Get position of the user focused window - Returns: `CGPoint` that corresponds to the focused window's position */ CGPoint get_focused_window_position(pid_t pid) { CGPoint ret; AXValueRef temp; AXUIElementRef frontMostApp; AXUIElementRef frontMostWindow; // Get the front most app frontMostApp = getFrontMostApp(pid); AXUIElementCopyAttributeValue(frontMostApp, kAXFocusedWindowAttribute, (CFTypeRef *)&frontMostWindow); // Copy the window position attribute from frontMostWindow AXUIElementCopyAttributeValue(frontMostWindow, kAXPositionAttribute, (CFTypeRef *)&temp); AXValueGetValue(temp, kAXValueCGPointType, &ret); CFRelease(temp); CFRelease(frontMostWindow); CFRelease(frontMostApp); return ret; }
/** 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; }