2

I'm trying to move around windows programatically from Python on OS X.

I found a snippet of AppleScript here on Stackoverflow which does this, but I'd like to do it in Python or another "real" scripting language.

This is my Python script, which does not work. I wrote the output of print commands below each of them.

#!/usr/bin/python

from Foundation import *
from ScriptingBridge import *

app = SBApplication.applicationWithBundleIdentifier_("com.apple.SystemEvents")

finderProc = app.processes().objectWithName_("Finder")

print finderProc
# <SystemEventsProcess @0x74b641f0: SystemEventsProcess "Finder" of application "System Events" (29683)>

finderWin = finderProc.windows()[0]

print finderWin
# <SystemEventsWindow @0x74b670e0: SystemEventsWindow 0 of SystemEventsProcess "Finder" of application "System Events" (29683)>

print finderWin.name()
# Macintosh HD

finderWin.setBounds_([[20,20],[100,100]])
# no visible result

finderWin.setPosition_([20,20])

The last command (setPosition_) crashes with the following exception.

Traceback (most recent call last):
  File "/Users/mw/Projekte/Python/winlist.py", line 17, in <module>
    finderWin.setPosition_([20,20])
AttributeError: 'SystemEventsWindow' object has no attribute 'setPosition_'

How can I make the setBounds command work?

Community
  • 1
  • 1
Mira Weller
  • 2,406
  • 22
  • 27
  • this is the AppleScript which works: ```tell application "System Events" ; set position of first window of application process "Finder" to {20, 20} ; end tell ``` – Mira Weller May 16 '13 at 22:29
  • Do you just want to do it in Python code? why not just use osascript and do and os.system call? – maranas May 16 '13 at 23:07
  • Because I don't want to use one specific window, but display a list from which the user can select a window. That would probably be easier if I was able to do it directly in Python. – Mira Weller May 20 '13 at 09:48

3 Answers3

3

You don't have to do it via System Events (I doubt that will work). Instead, do it directly on the Finder app:

from ScriptingBridge import *

app = SBApplication.applicationWithBundleIdentifier_("com.apple.Finder")
finderWin = app.windows()[0]
finderWin.setBounds_([[100,100],[100,100]])
finderWin.setPosition_([20,20])

You don't need the Foundation import either.

maranas
  • 1,396
  • 1
  • 12
  • 21
  • I can't do this because I don't want to depend on the target applications scripting support. It should work for all applications. – Mira Weller May 20 '13 at 17:24
  • I Get this - AttributeError: 'SBApplication' object has no attribute 'windows' – softmarshmallow Dec 29 '22 at 18:35
  • @softmarshmallow can you share your code snippet? Not sure if Finder should always have something return from that function, even if there are no Finder windows open. Testing now on my machine and the snippet still seems to work – maranas Dec 30 '22 at 13:58
2

If you want to interact with OS X's Accessibility APIs from Python then try atomac. System Events is just an AppleScriptable wrapper around various system APIs, but PyObjC and other Python libraries already give you extensive access to the system APIs without having to deal with any AS/SB nonsense.

--

p.s You may need to enable the 'assistive devices' option in System Preferences' Accessibility pane, otherwise most accessibility features won't be available.

foo
  • 3,171
  • 17
  • 18
  • Finally I got it using the accessibility APIs, but I switched to native Objective C and modified an Apple-provided example ([UIElementInspector](https://developer.apple.com/library/mac/#samplecode/UIElementInspector/Introduction/Intro.html)). – Mira Weller May 20 '13 at 17:26
  • atomac currently doesn't seem to work with python3: https://github.com/pyatom/pyatom/issues/141 – Sébastien Loisel Aug 04 '16 at 21:17
0

I’ve recently posted a question similar to what you’re trying to achieve but using python atomacos. However I’m facing some other issues:

Python pyatom: setting window size and position

You could have a look at atomacos’s implementation of setting variables through the accessibility API as they use the py-objc library’s.

FYI (atomacos is an updated version of atomac)