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
Help with an expression
Moderator: Martin
Re: Help with an expression
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
}
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
}
Re: Help with an expression
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.
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.
\\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
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");
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");
{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 20200310 Official, Android Pie 9.0, Rooted.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Re: Help with an expression
Thanks for the help.
Regex keeps popping up, is this something that I need to learn?
Any recommended websites to learn more about regex?
Regex keeps popping up, is this something that I need to learn?
Any recommended websites to learn more about regex?
Re: Help with an expression
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.
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 20200310 Official, Android Pie 9.0, Rooted.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.