Created
February 20, 2012 04:28
-
-
Save mwylde/1867867 to your computer and use it in GitHub Desktop.
OS X window management in MacRuby
This file contains hidden or 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
framework "Carbon" | |
framework "ApplicationServices" | |
framework "AppKit" | |
class Application | |
def self.active | |
info = NSWorkspace.sharedWorkspace.activeApplication | |
pid = info["NSApplicationProcessIdentifier"] | |
new pid | |
end | |
def self.all | |
NSWorkspace.sharedWorkspace.runningApplications.map{|app| | |
new app.processIdentifier | |
} | |
end | |
def initialize pid | |
@pid = pid | |
@app = AXUIElementCreateApplication pid | |
end | |
def focused_window | |
window = Pointer.new(:id) | |
err = AXUIElementCopyAttributeValue(@app, | |
NSAccessibilityFocusedWindowAttribute, | |
window) | |
Window.new window[0] | |
end | |
def windows | |
windows = Pointer.new(:id) | |
err = AXUIElementCopyAttributeValue(@app, | |
KAXWindowsAttribute, | |
windows) | |
(windows[0] || []).map{|w| Window.new w} || [] | |
end | |
def title | |
title = Pointer.new(:id) | |
err = AXUIElementCopyAttributeValue(@app, "AXTitle", title) | |
title[0] | |
end | |
end | |
class Window | |
attr_accessor :window | |
def self.active | |
Application.active.focused_window | |
end | |
def self.all | |
Application.all.map(&:windows).flatten | |
end | |
def initialize window | |
@window = window | |
end | |
def position | |
position_ref = Pointer.new(:id) | |
err = AXUIElementCopyAttributeValue(@window, | |
NSAccessibilityPositionAttribute, | |
position_ref) | |
pos_pt = Pointer.new("{CGPoint=dd}") | |
err = AXValueGetValue(position_ref[0], KAXValueCGPointType, pos_pt) | |
pos_pt[0] | |
end | |
def position= a | |
x, y = a | |
position = Pointer.new("{CGPoint=dd}") | |
position.assign(NSPoint.new(x, y)) | |
position_ref = AXValueCreate(KAXValueCGPointType, position) | |
AXUIElementSetAttributeValue(@window, | |
NSAccessibilityPositionAttribute, | |
position_ref) | |
end | |
def size | |
size_ref = Pointer.new(:id) | |
err = AXUIElementCopyAttributeValue(@window, | |
NSAccessibilitySizeAttribute, | |
size_ref) | |
size_pt = Pointer.new("{CGSize=dd}") | |
err = AXValueGetValue(size_ref[0], KAXValueCGSizeType, size_pt) | |
size_pt[0] | |
end | |
def size= a | |
w, h =a | |
size = Pointer.new("{CGSize=dd}") | |
size.assign(NSSize.new(w,h)) | |
size_ref = AXValueCreate(KAXValueCGSizeType, size) | |
AXUIElementSetAttributeValue(@window, | |
NSAccessibilitySizeAttribute, | |
size_ref) | |
end | |
def title | |
title = Pointer.new(:id) | |
err = AXUIElementCopyAttributeValue(@window, "AXTitle", title) | |
title[0] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment