PyObject * CGValueToPyTuple(AXValueRef value) //IN: AXValueRef to convert { int val1 = 0; int val2 = 0; PyObject *tuple = PyTuple_New(2); if (kAXValueCGSizeType == AXValueGetType(value)) { CGSize size; if (AXValueGetValue(value,kAXValueCGSizeType,&size) == 0){ return NULL; } val1 = size.width; val2 = size.height; } if (kAXValueCGPointType == AXValueGetType(value)){ CGPoint point; if (AXValueGetValue(value,kAXValueCGPointType,&point) == 0){ return NULL; } val1 = point.x; val2 = point.y; } PyTuple_SetItem(tuple,0,Py_BuildValue("i",val1)); PyTuple_SetItem(tuple,1,Py_BuildValue("i",val2)); return tuple; }
/** 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); }
/** 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); }
/** 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); }
CGSize AXLibGetWindowSize(AXUIElementRef WindowRef) { CGSize WindowSize = {}; AXValueRef WindowSizeRef = (AXValueRef) AXLibGetWindowProperty(WindowRef, kAXSizeAttribute); if(WindowSizeRef) { AXValueGetValue(WindowSizeRef, kAXValueCGSizeType, &WindowSize); CFRelease(WindowSizeRef); } return WindowSize; }
CGPoint AXLibGetWindowPosition(AXUIElementRef WindowRef) { CGPoint WindowPos = {}; AXValueRef WindowPosRef = (AXValueRef) AXLibGetWindowProperty(WindowRef, kAXPositionAttribute); if(WindowPosRef) { AXValueGetValue(WindowPosRef, kAXValueCGPointType, &WindowPos); CFRelease(WindowPosRef); } return WindowPos; }
AXError AXUIElementGetOrigin(AXUIElementRef element, CGPoint *originValue) { AXValueRef origin; AXError error = AXUIElementCopyAttributeValue(element, kAXPositionAttribute, (CFTypeRef *) &origin); if (error != kAXErrorSuccess) return error; error = AXValueGetValue(origin, kAXValueCGPointType, originValue) ? kAXErrorSuccess : kAXErrorFailure; CFRelease(origin); return error; }
AXError AXUIElementGetSize(AXUIElementRef element, CGSize *sizeValue) { AXValueRef size; AXError error = AXUIElementCopyAttributeValue(element, kAXSizeAttribute, (CFTypeRef *) &size); if (error != kAXErrorSuccess) return error; error = AXValueGetValue(size, kAXValueCGSizeType, sizeValue) ? kAXErrorSuccess : kAXErrorFailure; CFRelease(size); return error; }
/** 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; }
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; }