Help with an expression

General discussions about Automagic and automation in general

Moderator: Martin

Post Reply
robchoc
Posts: 81
Joined: 20 Jun 2018 12:38

Help with an expression

Post by robchoc » 08 Oct 2018 15:40

I'm writing a UI script and it contains:

if (contains(getTextInActiveWindow(), "Games Played 216"))
{
click("Home") //but I really need a way to stop the script, not sure how
}

I want the condition to be true if it's anything over "Games Played 216" so "Games Played 217" would also be true.

Thanks for the help

bogdyro
Posts: 239
Joined: 04 Apr 2015 15:14

Re: Help with an expression

Post by bogdyro » 08 Oct 2018 17:08

You can do it with regular expressions like this.
The list results williams contain all the info you need to check if the condiționa is true.
text='Games Played 216'; //the text to check
results=findAll(text, '(Games Played) (\\d{2,3})',true);// regex to find your number
// you have a match results[0] which contains 3 groups, the third is the number

nr=results[0][2]; //nr=216

if (nr>216)
{
// do your thing
}

User avatar
Desmanto
Posts: 2395
Joined: 21 Jul 2017 17:50

Re: Help with an expression

Post by Desmanto » 08 Oct 2018 17:29

bogdyro's solution will work too, especially if you need to access the 216 as variable. I have typed my answer, so I just post it to give you alternative.

Is the "Games Played" text is unique (only visible at the this part of text) at the visible window? If yes, simply remove the 216.

Code: Select all

if (contains(getTextInActiveWindow(), "Games Played"))
  click("Home"); 
This will return true regardless any number after the Games Played.
For a single line command, you don't need to close it with braces. But if you have 2 lines, braces are mandatory.


If "Games Played" is not unique (there are two or more similar text), and you need the one coupled with number, "Games Played 1", "Games Played 2", etc.... You need to use regex, use matches() if you don't need grouping variable, or findAll() as shown by bogdyro's solution. I will use matches() here.

Code: Select all

if (matches(getTextInActiveWindow(), 'Games Played \\d{1,3}'))
  click("Home"); 
\\d is to match digit
{1,3} is to match 1 to 3 chars before it, in this case 1 to 3 digits.
Pay attention that the regex must use single quote to close it. As using double quote will make Automagic try to to parse the {1,3}, thinking that it is a variable named 1,3
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200117 Official, Android Pie 9.0, Rooted.

robchoc
Posts: 81
Joined: 20 Jun 2018 12:38

Re: Help with an expression

Post by robchoc » 08 Oct 2018 18:48

Thanks for the help.

Regex keeps popping up, is this something that I need to learn?
Any recommended websites to learn more about regex?

User avatar
Desmanto
Posts: 2395
Joined: 21 Jul 2017 17:50

Re: Help with an expression

Post by Desmanto » 09 Oct 2018 11:33

Regex is so powerful for data parsing. If you are into automation, regex is absolutely very useful. It allows you to pick for specific pattern and group similar data from a well-formatted data.

For regex, I suggest to visit regexr.com. Automagic also has built-in regex tester that you can access from action script or condition expression. You can see more example of parsing data at my index, section HTTP request, Parsing data and Regex pattern. No one is expert at using regex, we simply use it more often that we get used to it. It is by more and more usage cases, we can keep getting better at regex.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200117 Official, Android Pie 9.0, Rooted.

Post Reply