/** 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); }
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); }
/** 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); }
AXUIElementRef getFrontMostWindow() { AXUIElementRef frontMostWindow; AXUIElementCopyAttributeValue(getFrontMostApp(), kAXFocusedWindowAttribute, (CFTypeRef *)&frontMostWindow); return frontMostWindow; }
CFTypeRef getFrontMostWindowTitle(pid_t pid) { AXUIElementRef app; CFTypeRef windowTitle; CFTypeRef temp; app = AXUIElementCreateApplication(pid); // check for nulls AXUIElementCopyAttributeValue(app, kAXFocusedWindowAttribute, (CFTypeRef *)&temp ); AXUIElementCopyAttributeValue( temp, kAXTitleAttribute, (CFTypeRef *)&windowTitle ); return windowTitle; }
/** 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; }
AXError AXUIElementCopyWindowAtPosition(CGPoint position, AXUIElementRef *window) { *window = NULL; AXUIElementRef systemWideElement = AXUIElementCreateSystemWide(); AXUIElementRef element = NULL; CFStringRef role = NULL; // First, retrieve the element at the given position. AXError error = AXUIElementCopyElementAtPosition(systemWideElement, position.x, position.y, &element); if (error != kAXErrorSuccess) goto end; // If this element is a window, return it. error = AXUIElementCopyAttributeValue(element, kAXRoleAttribute, (CFTypeRef *) &role); if (error != kAXErrorSuccess) goto end; if (CFStringCompare(role, kAXWindowRole, 0) == kCFCompareEqualTo) { *window = element; CFRetain(*window); goto end; } // Otherwise, return the window attribute. error = AXUIElementCopyAttributeValue(element, kAXWindowAttribute, (CFTypeRef *)window); end: CFRelease(systemWideElement); if (element != NULL) CFRelease(element); if (role != NULL) CFRelease(role); return error; }
/* Return title of a MoveWin::Window as a Ruby string */ VALUE MW_Window_title(VALUE self) { void *mwWindow; CFStringRef title; Data_Get_Struct(self, MW_Window, mwWindow); AXUIElementCopyAttributeValue( ((MW_Window *)mwWindow)->axWindow, kAXTitleAttribute, (CFTypeRef *)&title ); return convertStringRef(title); }
/** 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); }
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; }
AXError AXUIElementGetTitle(AXUIElementRef element, CFStringRef *title) { return AXUIElementCopyAttributeValue(element, kAXTitleAttribute, (CFTypeRef *) title); }
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; }
CFTypeRef AXLibGetWindowProperty(AXUIElementRef WindowRef, CFStringRef Property) { CFTypeRef TypeRef; AXError Error = AXUIElementCopyAttributeValue(WindowRef, Property, &TypeRef); return (Error == kAXErrorSuccess) ? TypeRef : NULL; }