VDict() error when trying to do a find/comparison

Asked by LP81

In an effort to circumvent to inability to paste a value to a variable, I am trying to use the VDict() function to find the image, and complare it against 8 known images.

Essentially i do a "Get Info" on a file, look for the Dimensions area, and try to compare it to my internal dictionary.

size = VDict()
size[<img>] = "88x31"
size[<img>] = "97x61"
size[<img>] = "120x30"
size[<img>] = "120x60"
size[<img>] = "120x90"
size[<img>] = "300x100"
size[<img>] = "300x250"
size[<img>] = "680x180"

switchApp("Finder.app")
click(<img>)
type(file)
type("i", KEY_CMD)
img = find(<img>)
result = size[img]

It pops back errors in the "__getitem__"

I followed the example basically swapping out a "find" for the "capture" I don't understand all of the the documentation for VDict()... can somoen help out?

[sikuli] [Error] source lineNo: 16
[sikuli] [Error] Traceback (innermost last):
  File "/var/folders/OI/OIujfWUTG3CWtdB94mnpjk+++TM/-Tmp-/sikuli-tmp6983520504390998998.py", line 16, in ?
  File "/Users/XXXXX/Desktop/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/python/edu/mit/csail/uid/Sikuli.py", line 202, in __getitem__
  File "/Users/XXXXX/Desktop/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/python/edu/mit/csail/uid/Sikuli.py", line 236, in get
  File "/Users/XXXXX/Desktop/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/python/edu/mit/csail/uid/Sikuli.py", line 172, in _getInBundle
AttributeError: __getitem__

Question information

Language:
English Edit question
Status:
Expired
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Sergey Darovskih (darovskih) said :
#1

why don't use

result = size[<img>]

instead of

img = find(<img>)
result = size[img]?

maybe find returns None (if it finds no matches) and command size[None] returns getItem error.

Revision history for this message
LP81 (pjbreen) said :
#2

If I do that, I get an error at the end when I try to call result back up, as it thinks I'm passing a string were as I want it to type out the text supplied in the VDict()... 120x90, 680x180 etc...

the last chunk of my code is:

type("\t")
type(file)
type("\t")
type(biz)
type("\t")
type(file)
type("\t")
type(alt)
click(<dropdown menu img>)
type(result)

error is:
[sikuli] [Error] source lineNo: 32
[sikuli] [Error] Traceback (innermost last):
  File "/var/folders/OI/OIujfWUTG3CWtdB94mnpjk+++TM/-Tmp-/sikuli-tmp6300377443247439282.py", line 32, in ?
  File "/Users/patrickbreen/Desktop/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/python/edu/mit/csail/uid/Sikuli.py", line 514, in type
TypeError: type(): 2nd arg can't be coerced to String

Revision history for this message
RaiMan (raimund-hocke) said :
#3

I guess, at the end you want to know, which of the stored images you have found?
It would be helpful, if you give some info on your expectations, what a statement should do after a # (python comment)

1. in the IDE you should check wether all your images you have stored are found correctly. Make the screen, that one ore more of your pics are visible, in the IDE click on an image (in this case open all possible info-windows) - a window pops up where you can check, wether the pic was found and play around with the simularity slider, to get exactly what you want. I guess for your intention you should screenshot your images as exact as possible and use simularity 1.

2. I guess, file contains a filename (previous input()?) that you want to search and open its info. you need not switch to finder here, just type CMD-Space to get spotlight. file should contain a filename, that leads to an exact find.

# Until now we have: (for exact timing sleep() may be necessary in between))
file = "my-file-to-search.xxx"
type(" ", KEY_CMD) # open spotlight
type(file) # search
type("i", KEY_CMD) # open info window for top-match

# Now we have to try to find out the dimension of this image
pics = size.key() # list of the stored image-filenames
picFound = None
for p in pics: # lets loop thru
     if find(p): # look for it
        picFound = p save if found and step out
        break
if picFound:
   # here goes further processing
   sizeFound = size[picFound][0] # here you have your text to type() (the extra [0] is needed because of a "bug" in VDict)
else:
   pupup("Sorry, no success!)

To make the find(p) above more precise, you could try find(Pattern(p).simular(1)) as mentioned above.
Another option for even more precision is to capture a sufficient area left of the "dimension-picture-area" and then use find(...This-is-only-insinde-info-windo...).right().find(Pattern(p).simular(1)) - this would jump into the info-window and look to the right for the match (its exactly what you want).

Revision history for this message
RaiMan (raimund-hocke) said :
#4

Sorry, for misleading:

   sizeFound = size[picFound][0] # here you have your text to type() (the extra [0] is ...

its not a bug. Saying VDict[...image...] returns a list by intention, that may contain only one item!

in your case just use:
   sizeFound = size.get_exact(picFound) # which returns the value directly, since only one or no item possible

to make it secure, you should check sizeFound - maybe None in some unexpected cases

Revision history for this message
Launchpad Janitor (janitor) said :
#5

This question was expired because it remained in the 'Needs information' state without activity for the last 15 days.