User defined function has been requested. Meanwhile, you can use eval() as stated by anuraag. You can check this thread for the concept :
viewtopic.php?f=4&t=7011
I almost use this in my last GCam mode tester flow, but cancelled it because the timing is not uniform. (BTW, check out the lastest GCam night sight to see if it is available to your device
https://forum.xda-developers.com/showpo ... stcount=27)
Using this concept, you can even pass argument or retrieve result. Here is example of aritmethic function to calculate 1 + 2 + 3 + ... until the param1
Code: Select all
arit =
"for(i in [1 to param1])" +
" result = result + i;" ;
result = 0;
param1 = 100;
eval(arit);
a = result;
result = 0;
param1 = 200;
eval(arit);
b = result;
We initialized result as 0 and pass the param1 as the parameter. Invoke the stored user function and retrieve back the result.
This seems to be longer than script you want to paste, but it is because I create a base for the script pattern. When you script is more than 20 lines, this seems to be shorter then. And if your script doesn't need result (such as in control UI), then you don't need to initialize the result or retrieve it, only need parameter lines and the eval(). Next if your script already retrieve the param from certain value (example, after input dialog, to use {value}), then no param needed anymore.
To create the defined script, you have to test is first that it is working for single instance. Then quote each line, append + at each end to make it easier to read later. If you have quote inside the script, you need to escape it, or use single quote instead. Pay attention too to braces, as you need to use single quote to make sure it is not intepreted (or vice versa, if you want it to be interpreted).