How do I append text directly onto a variable.

1,432 views
Skip to first unread message

Steve Waring

unread,
Apr 15, 2020, 3:23:33 PM4/15/20
to Tasker
Suppose I want to set variable %bbb to the value of variable %aaa directly followed by the character x. I can't use %aaax as that will either give the string %aaax or the content of variable %aaax.
I could use %something and assign x to it, and then use %aaa%something or I could use a java scriptlet. However both of these become ponderous if you want to chain quite a few variables and characters together.

Rich D

unread,
Apr 15, 2020, 5:00:57 PM4/15/20
to Tasker Google Groups Post

Suppose I want to set variable %bbb to the value of variable %aaa directly followed by the character x. 

The way I always do it is to simply set the value x to another local variable like %append = x. 

Then it would be variable set %bbb TO: %aaa%append

aa6vh

unread,
Apr 16, 2020, 10:38:09 AM4/16/20
to Tasker
I simply use Variable set with the append checkbox selected.

Variable Set
Name: %bbb
To: X
Append: Yes

Dale Smith

unread,
Apr 16, 2020, 10:44:26 AM4/16/20
to Tasker
Or you could use the apend option with Variable Set.

Apend

A1: Variable Set
Name: %aaa
To: space
Recurse Variables: Off
Do Maths: Off
Append: Off
Max Rounding Digits: 3

A2: Variable Set
Name: %aaa
To: x
Recurse Variables: Off
Do Maths: Off
Append: On
Max Rounding Digits: 3

A3: Flash
Text: %aaa
Long: Off

Steve Waring

unread,
Apr 19, 2020, 5:33:12 PM4/19/20
to Tasker
Yes, I am aware that you can do that, as I tried to point out in my original post. I was hoping for something similar to most Unix shell scripts where you can use ${myvar} instead of $myvar if you need ${myvar}x. As I tried to say in my original post " However both of these become ponderous if you want to chain quite a few variables and characters together"  What would have been one Perform Task statement with an if clause attached, has become ten statements within an if block because there is no way to encapsulate variables in substitution. Using a Java Scriptlet I can condense this down to two statements within an if block.

Marta Hintz

unread,
Apr 21, 2020, 9:47:29 AM4/21/20
to Tasker
Perhaps share the script solution for others then? At least a bare bone example?

Steve Waring

unread,
Apr 21, 2020, 12:53:10 PM4/21/20
to Tasker
Well as an example to concatenate variables with literals, you could use this in a java scriptlet:
var result = horse + "s, " + cow + "s, " + pig + "s, " + dog + "s, " + cat + "s."
Then result will be the result of the concatenations.  Literals can be placed in either single or double quotes, so you could use "can't" or 'Groucho Marx "Outside of a dog, a book is a man’s best friend. Inside of a dog, it’s too dark to read."'
Array indexes start from zero. So myarr[0] is the same as %myarr(1) but arrays only work if there is no base variable.
You can use this to join arrays with nothing between the items (why the splitter is not optional in tasker is a mystery to me). Remember though, no base:
var result = myarr.join("");

Marta Hintz

unread,
Apr 21, 2020, 3:28:28 PM4/21/20
to Tasker
Cool thank you, this helps.

Steve Waring

unread,
May 3, 2020, 2:23:32 PM5/3/20
to Tasker
You can also use JavaScriptlets to do many other things not available in Tasker.

For example I have a task that downloads Json data from the web. It has some dates in that I need to turn into seconds. But the dates are UTC dates (GMT), so I can't use the normal Tasker variable convert action. This JavaScriptlets does it: it takes the text date from a tasker variable called when and stores the result in a tasker variable called seconds.

var seconds = new Date(when + " UTC").getTime() / 1000;

The Date object has a myriad of methods, so you could also use it to turn the same UTC text date into a local time text date, or go the other way.


On Tuesday, 21 April 2020 20:28:28 UTC+1, Marta Hintz wrote:
Cool thank you, this helps.

Steve Waring

unread,
May 6, 2020, 9:53:42 AM5/6/20
to Tasker
You can use JavaScript to do a number of things that should be simple in Tasker, but aren't. For example, if you want to take some text and replace all the new line characters with <br> for use in html, it is not possible to do it safely if you want double newline characters to end up as <br><br> . In a JavaScriptlet it is just:

var text = text..replace(/\n/g, "<br>");


If you are going to add a JavaScriptlet, it's worth thinking about if it can do anything else whilst it runs. For example, if you also want to change & < and > to &amp; &lt; and &gt; to make the html safe, you can use:

var text = text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br>");

Reply all
Reply to author
Forward
0 new messages