[Pythonmac-SIG] Modifying table cell content with python-appscript

Sven A. Schmidt sas at abstracture.de
Fri Nov 21 11:12:43 CET 2008


NB: In the process of writing this plea for help I've come up with a  
workaround. (Don't you love it when you solve your problem while  
asking about it? :) I'm posting this rather elaborate problem  
description anyway, because it might help others and because it is  
after all still a workaround and I'm sure there's room for improvement  
both in the code and my understanding of the very powerful python- 
applescript bridge.

------------------------------------------

Hi,

I hope I'm in the correct place with this problem -- I've found this  
list through a link from the python-appscript page. My problem  
probably falls between python and AppleScript but I've not been able  
to find any clues on the web or in this list's archives. Please let me  
know if there's a better place for me to post this!

I'm trying to script a Cocoa app I wrote to establish user level  
tests. To that end I'm trying to populate a table view (backed by Core  
Data) with entries. Thanks to Prefab's UI Browser I've had little  
trouble identifying and accessing the UI controls from py-appscript.  
However, progress has ground to a halt when I tried to change a value  
in a table view cell. The code is the following:

     ...
     table = scroll_area.tables[1]
     row = table.rows[index]
     text_field = row.text_fields[1]
     text_field.value.set(value)
     assert text_field.value() == value

This fails with:

   File "create_document.py", line 47, in edit_customer
     assert text_field.value() == value
AssertionError

According to the AppleScript code UI Browser auto generates for text  
field value changes I'm doing the right thing by setting the value  
property:

	set value of text field 1 of row 1 of table 1 of scroll area 3
		of splitter group 1 of window "Untitled"  to "<string>"

It's just not working from appscript. I'm at a loss as to how to  
propagate the value change to the model. I've found that I can make  
the table cell highlight and change the actual displayed value by  
inserting the following before "value.set(...)":

     text_field.focused.set(True)

However, this only changes the displayed value. The assert still  
fails. Of course I've tried inserting

     text_field.focused.set(False)

after the calls to 'commit' the change to the table view but this does  
nothing at all! Not even reset focus. I've also tried focusing another  
element to commit the change but no luck.

I was thinking to perhaps send a "Return" key stroke and I've found a  
way to do that (I'm using 'key_code' instead of 'keystroke', because I  
don't know what the return key constant should be in appscript):

     app('System Events').key_code(36)

Doesn't make a difference though :(

On a hunch I then tried the sequence of sending a regular keystroke  
plus return and lo and behold, the following works for some reason  
(does it have to do with the cell editor perhaps?):

     text_field.focused.set(True)
     app('System Events').keystroke(value)
     app('System Events').key_code(36)
     assert text_field.value() == value

This is a bit kludgy but at least it works. If anyone has any tips on  
how to improve this it would be very much appreciated!

Cheers,
Sven




More information about the Pythonmac-SIG mailing list