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

2008-11-21 Thread Sven A . Schmidt
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 ""

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


___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


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

2008-11-22 Thread Sven A. Schmidt

has, thanks a lot for your reply!

Should work, although hard to say why it isn't without some more  
detail. A couple pointers:


Right, after years of replying to bug reports with "what version are  
you using" I forget to include version info... I'm using python 2.5  
and appscript 0.18.1, both from fink:


 i   appscript-py25  0.18.1-1   High level Apple event bridge  
for Python
 i   python251:2.5.2-1  Interpreted, object-oriented  
language


- If you're on 2.5 or earlier, post the complete appscript code  
you're using to set the text field's value and the Python value you  
get when you get it so we can take a look, e.g. something like:


   ref = app('AppName').windows['Untitled'].splitter_groups[1] \
   .scroll_areas[3].tables[1].rows[1].text_fields[1]

   s = ''
   ref.value.set(s)
   t = ref.value()
   print (s, t, s == t)
   # Result: ...


Ok, I've tried your example:

sysevents = app('System Events')
hg = sysevents.processes['Hourglass']

ref = hg.windows['Untitled'].splitter_groups[1] \
.scroll_areas[3].tables[1].rows[1].text_fields[1]

s = ''
ref.value.set(s)
t = ref.value()
print (s, t, s == t)

Here's what I get:

[eris:sas/Hourglass-trunk/Tests] sas% python2.5 hg_test.py
('', u'unnamed customer', False)

BTW, your code "app('AppName').windows['Untitled']" fails with

AttributeError: Unknown property, element or command: 'windows'

and I have to use the process from sysevents. I suppose this is  
because my app is not scriptable and I'm using UI scripting? Is that  
essentially the difference between app('xxx') and  
sysevents.processes['xxx']? (I guess this has nothing to do with my  
actual problem, I'm just curious.)


Cheers,
Sven

___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


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

2008-11-24 Thread Sven A. Schmidt



Ok, I've tried your example:

sysevents = app('System Events')
hg = sysevents.processes['Hourglass']

ref = hg.windows['Untitled'].splitter_groups[1] \
.scroll_areas[3].tables[1].rows[1].text_fields[1]

s = ''
ref.value.set(s)
t = ref.value()
print (s, t, s == t)

Here's what I get:

[eris:sas/Hourglass-trunk/Tests] sas% python2.5 hg_test.py
('', u'unnamed customer', False)


Any idea where the 'unnamed customer' is coming from? Is the  
appscript 'set' command setting the text field's value? Can you post  
both your working AppleScript and non-working Python script for  
comparison? Also, if you can run your application with AEDebug  
enabled:


The 'unnamed customer' is the original field value, sorry for not  
mentioning that. It never changes. I don't have an AppleScript, I'm  
just doing this with appscript.


The AEDebug output is rather long, I'll send you the full output off- 
list.


Cheers,
Sven

___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


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

2008-11-24 Thread Sven A. Schmidt

On Nov 24, 2008, at 22:13, has wrote:


On 24 Nov 2008, at 09:15, Sven A. Schmidt wrote:


Here's what I get:

[eris:sas/Hourglass-trunk/Tests] sas% python2.5 hg_test.py
('', u'unnamed customer', False)


Any idea where the 'unnamed customer' is coming from? Is the  
appscript 'set' command setting the text field's value? Can you  
post both your working AppleScript and non-working Python script  
for comparison? Also, if you can run your application with AEDebug  
enabled:


The 'unnamed customer' is the original field value, sorry for not  
mentioning that. It never changes. I don't have an AppleScript, I'm  
just doing this with appscript.



Ah, right. I misunderstood - I thought you had it working in  
AppleScript but not appscript. Quick question: is the table field in  
question editable? (i.e. Can you click in it and type text manually?)


Yes, it is. I can even make it editable via code (see original  
description) by setting the focused property. But I can't make the  
change stick then.


Cheers,
Sven

___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig