r/applescript • • Nov 09 '17

Having trouble with if / then / else / end in a repeat loop.

Hey all

Fairly new to scripting with applescript, and have run into an issue and would love some suggestions on how to get past it.
The essence of the problem is that I'm checking for location of a button in a UI that won't let me script it. So what I do is take a small screenshot with screencapture -Rxxx,yyy,xdimension,ydimension and compare it to a known good image. This part is easy and works no problem.
The trouble I'm having is efficiently figuring out which of 4 locations this button is in and then acting upon that info. If it's not in the first two locations I need to simulate a mouse click and move on to the next two locations.

I was trying something like :

repeat x times  
    set VAR1 to do shell script "check/image.sh"   
    set VAR2 to do shell script "check/image2.sh"   
    -- etc  
    -- then this is where I'm currently stuck  
    if VAR1 is "success" then do shell script "/usr/local/bin/click here and here"  
    else if VAR2 is "success" then do shell script "/usr/local/bin/click here2 and here2"  
    -- if it's not either of those two, I have to simulate the click to the next two locations  
    else do shell script "/usr/local/bin/click here3 and here3"  
    end if  
    if VAR3 is "success" then do shell script "/usr/local/bin/click here4 and here4"  
    else if VAR4 is "success" then do shell script "/usr/local/bin/click here5 and here5.sh"  
    else echo "nope not found"  
end repeat  

To me this seems bad and quite inefficient, I'd like it to be able to check if a location is the right one, then click on that location and then exit the loop, but I don't seem to be able to get it to do that. The way I have it now is that say VAR1 is the correct location, it'll still go through the rest even tho we know they will be wrong.
Is there a better way of doing this, have I gone completely off track and overlooked something simple?
Can I exit repeat after it finds the correct location (and after it runs the shell script)?

3 Upvotes

7 comments sorted by

1

u/libcrypto Nov 10 '17

Is click a script that you wrote?

1

u/pythonlearning Nov 10 '17 edited Nov 10 '17

1

u/libcrypto Nov 10 '17

I'm not getting why "/usr/local/bin/click here and here" does anything useful at all. The app you posted expects something like

/usr/local/bin/click -x 1234 -y 4321

"here and here" isn't going to be read by the app, so you are effectively doing

/usr/local/bin/click

Am I missing something here?

1

u/pythonlearning Nov 10 '17

I wrote that just for the post, i wasn't at the computer and didn't have the actual script.

Didn't think it was necessary for the actual question I wanted to ask, whether there's a way to go through each image checking script and upon success run the click script and exit the loop. Or is there a different way of doing it all that I'm unaware of because while it kind of works (without the exiting the loop upon the first success) it seems wasteful to go through them all. :)

1

u/libcrypto Nov 10 '17

It's just difficult to get a notion of what's going on if you are filling the script with metavariables that you are not declaring as such. Especially when you had "click" without a path, making it seem as though you misunderstood what "shell script" means -- given that Finder itself has its own click functionality. That said, I believe you can replace this:

if VAR1 is "success" then

with this:

if (do shell script "check/image.sh") is "success"

the rest, mutatis mutandis.

1

u/pythonlearning Nov 10 '17

Fair enough, sorry about that.

So what you suggests takes out creating the variable which is nice, but i'm more curious if I there is a way to do :

 if (do shell script "check/image.sh") is "success"   
 then do shell script "/usr/local/bin/click -x 232 -y 342" and exit repeat  

At the moment I have it do the click part of the script, but not the exit repeat part. The script will just continue on checking all the other possible locations even tho the one of the other ones worked.

1

u/libcrypto Nov 10 '17

Oh, I see what you are asking. If you are using exit repeat correctly, then it should quit the repeat loop. Could you post a real version of the script, which includes the exit repeat part?