[Pythonmac-SIG] trouble getting window bounds via appscript

Todd t.greenwoodgeer at gmail.com
Fri Feb 5 22:32:43 CET 2010


I'd like to perform some window management via appscript on Mac OSX 
(10.6.2).

This is pretty straightforward using AppleScript (see code#1 below). 
However, my attempts to perform the same tasks in python appscript have 
failed:

1. get bounds of display
2. get bounds of foreground process

I can query the 'System Events' for the process list, and get the 
foremost process...however, I cannot get the bounds from the windows. 
(see code#2 below).

The appscript error is:
------------------------------------------------
CommandError: Command failed:
                 OSERROR: -1728
                 MESSAGE: Can't get reference.
                 OFFENDING OBJECT: 
app(u'/System/Library/CoreServices/System 
Events.app').application_processes[u'iTerm'].windows[u'todd at greenmachine: ~/Documents/projects/python/windowmanager'].bounds
                 COMMAND: app(u'/System/Library/CoreServices/System 
Events.app').application_processes[u'iTerm'].windows[u'todd at greenmachine: ~/Documents/projects/python/windowmanager'].bounds.get()
------------------------------------------------

Any advice out there?

-Todd



CODE#1 (AppleScript)
--------------------
"""
# no idea how to do this in appscript
tell application "Finder"
	set _b to bounds of window of desktop
	set display_x_size to item 3 of _b
	set display_y_size to item 4 of _b
end tell

# should be able to do this in appscript, but doesn't work
tell application cur_app
	tell front window
		set {x1, y1, x2, y2} to (get bounds)
	end tell
end tell
"""

CODE#2 (python appscript)
--------------------
"""
#! /Library/Frameworks/Python.framework/Versions/2.6/bin/python

# get the appscript bridge
from appscript import *

# define the service event
se = app(u'System Events')

import pdb


class WindowManager:
         def dumpWindowBounds(self):
                 for app in se.processes.get():
                         for win in app.windows.get():
                                 print win, win.bounds.get()

if __name__ == "__main__":
         wm = WindowManager()
         wm.dumpWindowBounds()

"""


More information about the Pythonmac-SIG mailing list