Skip to content

Instantly share code, notes, and snippets.

@hym3242
Last active May 16, 2024 01:14
Show Gist options
  • Save hym3242/0d3699eefd2d2267885cec50bdeccc2c to your computer and use it in GitHub Desktop.
Save hym3242/0d3699eefd2d2267885cec50bdeccc2c to your computer and use it in GitHub Desktop.
A objc program that clicks the "markup" button in "com.apple.screencaptureui" application 's "ScreenshotAnnotationWindow" (titled "Screenshot")
// compile with: cc clickScreenshotWindowMarkupButton.m -o clickScreenshotWindowMarkupButton -framework Cocoa
// This program clicks the "markup" button in "com.apple.screencaptureui" application 's "ScreenshotAnnotationWindow" (titled "Screenshot")
// Mostly written by the obviously-dumber-than-first-launched ChatGPT 4 Classic. No rights reserved.
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
// Function to find the PID using bundle identifier
pid_t pidForBundleIdentifier(NSString *bundleIdentifier) {
NSArray *apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleIdentifier];
if (apps.count > 0) {
return [[apps firstObject] processIdentifier];
}
return -1;
}
void clickMarkupButton(AXUIElementRef element) {
// Check if the current element is a button
CFStringRef role;
AXUIElementCopyAttributeValue(element, kAXRoleAttribute, (CFTypeRef *)&role);
if (role && CFStringCompare(role, kAXButtonRole, 0) == kCFCompareEqualTo) {
// Check for label "markup"
CFStringRef label;
AXUIElementCopyAttributeValue(element, kAXDescriptionAttribute, (CFTypeRef *)&label);
if (label && CFStringCompare(label, CFSTR("markup"), 0) == kCFCompareEqualTo) {
// Perform a press action on the 'markup' button
AXUIElementPerformAction(element, kAXPressAction);
CFRelease(label);
CFRelease(role);
return;
}
if (label) CFRelease(label);
}
if (role) CFRelease(role);
// Recursively check children
CFArrayRef children;
AXUIElementCopyAttributeValue(element, kAXChildrenAttribute, (CFTypeRef *)&children);
if (children) {
CFIndex count = CFArrayGetCount(children);
for (CFIndex i = 0; i < count; i++) {
AXUIElementRef child = (AXUIElementRef)CFArrayGetValueAtIndex(children, i);
clickMarkupButton(child);
}
CFRelease(children);
}
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Get the PID using the bundle identifier
pid_t pid = pidForBundleIdentifier(@"com.apple.screencaptureui");
if (pid == -1) {
NSLog(@"Application with bundle ID 'com.apple.screencaptureui' not found.");
return 1;
}
// Create an accessibility element for the application using its PID
AXUIElementRef screencaptureui = AXUIElementCreateApplication(pid);
// Get the window titled 'Screenshot'
AXUIElementRef window = NULL;
CFArrayRef windows = NULL;
AXUIElementCopyAttributeValue(screencaptureui, kAXWindowsAttribute, (CFTypeRef *)&windows);
if (windows) {
for (CFIndex i = 0; i < CFArrayGetCount(windows); i++) {
AXUIElementRef currentWindow = (AXUIElementRef)CFArrayGetValueAtIndex(windows, i);
CFStringRef title;
AXUIElementCopyAttributeValue(currentWindow, kAXTitleAttribute, (CFTypeRef *)&title);
if (CFStringCompare(title, CFSTR("Screenshot"), 0) == kCFCompareEqualTo) {
window = CFRetain(currentWindow);
CFRelease(title);
break;
}
if (title) CFRelease(title);
}
CFRelease(windows);
}
if (!window) {
NSLog(@"No window with title 'Screenshot' found.");
CFRelease(screencaptureui);
return 1;
}
// Recursively search and click the 'Markup' button
clickMarkupButton(window);
CFRelease(window);
CFRelease(screencaptureui);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment