Skip to content

Instantly share code, notes, and snippets.

@zaru
Last active February 9, 2024 19:49
Show Gist options
  • Save zaru/2405fee754d25cb16a1622a4187758bc to your computer and use it in GitHub Desktop.
Save zaru/2405fee754d25cb16a1622a4187758bc to your computer and use it in GitHub Desktop.
$ swiftc -o get_window_title get_window_title.swift && get_window_title 9999
import Foundation
import AppKit
func main() -> Int {
let argv = ProcessInfo.processInfo.arguments
let pid = Int32(argv[1])!
print(pid)
let appRef = AXUIElementCreateApplication(pid)
var menubar: CFTypeRef?
AXUIElementCopyAttributeValue(appRef, kAXMenuBarAttribute as CFString, &menubar)
var children: CFTypeRef?
AXUIElementCopyAttributeValue(menubar as! AXUIElement, kAXChildrenAttribute as CFString, &children)
let items = children as? NSArray
for item in items! {
var menuLabel: CFTypeRef?
AXUIElementCopyAttributeValue(item as! AXUIElement, kAXTitleAttribute as CFString, &menuLabel)
print(menuLabel)
}
return 0
}
_ = main()
import Foundation
import AppKit
func main() -> Int {
let argv = ProcessInfo.processInfo.arguments
let pid = Int32(argv[1])!
print(pid)
let appRef = AXUIElementCreateApplication(pid)
var window: CFTypeRef?
var result: AXError = AXUIElementCopyAttributeValue(appRef, kAXFocusedWindowAttribute as CFString, &window)
if result == .success {
var title: CFTypeRef?
AXUIElementCopyAttributeValue(window as! AXUIElement, kAXTitleAttribute as CFString, &title)
print(title)
} else {
print(result.rawValue)
}
return 0
}
_ = main()
import Foundation
import AppKit
func main() -> Int {
let argv = ProcessInfo.processInfo.arguments
let pid = Int32(argv[1])!
print(pid)
let appRef = AXUIElementCreateApplication(pid)
var menubar: CFTypeRef?
AXUIElementCopyAttributeValue(appRef, kAXMenuBarAttribute as CFString, &menubar)
var menuBarItemsRef: CFTypeRef?
AXUIElementCopyAttributeValue(menubar as! AXUIElement, kAXChildrenAttribute as CFString, &menuBarItemsRef)
let menuBarItems = menuBarItemsRef as? NSArray
for item in menuBarItems! {
var menuLabel: CFTypeRef?
AXUIElementCopyAttributeValue(item as! AXUIElement, kAXTitleAttribute as CFString, &menuLabel)
print("menu bar item: \(menuLabel)")
}
var subMenuRef: CFTypeRef?
AXUIElementCopyAttributeValue(menuBarItems?[2] as! AXUIElement, kAXChildrenAttribute as CFString, &subMenuRef)
let subitems = subMenuRef as? NSArray
var subMenuItemsRef: CFTypeRef?
AXUIElementCopyAttributeValue(subitems?[0] as! AXUIElement, kAXChildrenAttribute as CFString, &subMenuItemsRef)
let subMenuItems = subMenuItemsRef as? NSArray
for item in subMenuItems! {
var menuLabel: CFTypeRef?
AXUIElementCopyAttributeValue(item as! AXUIElement, kAXTitleAttribute as CFString, &menuLabel)
print("submenu item: \(menuLabel)")
}
AXUIElementPerformAction(subMenuItems?[0] as! AXUIElement, kAXPressAction as CFString)
return 0
}
_ = main()
//
import Cocoa
class Event: NSObject {
private var runLoopSource: CFRunLoopSource?
private var shouldChangeState: Bool = true
override init() {
let options = NSDictionary(
object: kCFBooleanTrue,
forKey: kAXTrustedCheckOptionPrompt.takeUnretainedValue() as NSString
) as CFDictionary
AXIsProcessTrustedWithOptions(options)
}
}
let keyEvent = Event()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment