-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <ApplicationServices/ApplicationServices.h> | ||
#include <Carbon/Carbon.h> | ||
|
||
CFStringRef GetActiveWindowTitle() { | ||
// Get the accessibility element corresponding to the frontmost application | ||
AXUIElementRef appElem = AXUIElementCreateApplication(CGMainPID()); | ||
if (!appElem) { | ||
return NULL; | ||
} | ||
|
||
// Get the accessibility element corresponding to the frontmost window | ||
AXUIElementRef window = NULL; | ||
AXError error = AXUIElementCopyAttributeValue(appElem, kAXFocusedWindowAttribute, (CFTypeRef*)&window); | ||
CFRelease(appElem); // Release the app element as we don't need it anymore | ||
|
||
if (error != kAXErrorSuccess) { | ||
return NULL; | ||
} | ||
|
||
// Get the title of the frontmost window | ||
CFStringRef title = NULL; | ||
error = AXUIElementCopyAttributeValue(window, kAXTitleAttribute, (CFTypeRef*)&title); | ||
CFRelease(window); // Release the window element | ||
|
||
if (error != kAXErrorSuccess) { | ||
return NULL; | ||
} | ||
|
||
return title; | ||
} | ||
|
||
// Function to get the application name (optional) | ||
CFStringRef GetActiveWindowAppName() { | ||
AXUIElementRef appElem = AXUIElementCreateApplication(CGMainPID()); | ||
if (!appElem) { | ||
return NULL; | ||
} | ||
|
||
// Get the application name | ||
CFStringRef appName = NULL; | ||
AXError error = AXUIElementCopyAttributeValue(appElem, kAXApplicationNameAttribute, (CFTypeRef*)&appName); | ||
CFRelease(appElem); | ||
|
||
if (error != kAXErrorSuccess) { | ||
return NULL; | ||
} | ||
|
||
return appName; | ||
} | ||
|
||
int main() { | ||
// Get the title of the active window | ||
CFStringRef title = GetActiveWindowTitle(); | ||
if (title) { | ||
// Process the title (e.g., print it) | ||
printf("Active Window Title: %s\n", CFStringGetCString(title, kCFStringEncodingUTF8)); | ||
CFRelease(title); | ||
} else { | ||
printf("Failed to get active window title\n"); | ||
} | ||
|
||
// Optionally get the application name | ||
CFStringRef appName = GetActiveWindowAppName(); | ||
if (appName) { | ||
printf("Active Window Application: %s\n", CFStringGetCString(appName, kCFStringEncodingUTF8)); | ||
CFRelease(appName); | ||
} else { | ||
printf("Failed to get active window application name\n"); | ||
} | ||
|
||
return 0; | ||
} |