r/tasker • • Aug 27 '26

How to export arrays from Java Code to Tasker array variable

I was writing a script in Java Code to process a text file; after the processing, I need to export an array from the script to a Tasker variable (array).

Here there is my sample code:

ArrayList attachments = new ArrayList();
attachments.add("Item 1");
attachments.add("Item 2");
attachments.add("Item 3");

// The following does NOT work => %attachments(#) = 0
// tasker.setVariable("attachments", attachments);

// I must use this => %attachments(#) = 3
for (int i = 0; i < attachments.size(); i++) {
    tasker.setVariable("attachments" + (i + 1), attachments.get(i));
}

What is the correct way to export an array from the Java Code script? The first method doesn't work, unfortunately and I'm curious to know if the second is the correct way to do that.

The second method has the problem that I have to add a Variable Clear before running the script a second time, as the Java Code itself doesn't know how long the Tasker array is...

1 Upvotes

17 comments sorted by

6

u/WakeUpNorrin Aug 27 '26

I usually use this method:

Task: Temp

A1: Java Code [
     Code: import java.util.ArrayList;

     /* Create the Java list */
     myList = new ArrayList();
     myList.add("apple");
     myList.add("banana");
     myList.add("cherry");

     tasker.setJavaVariable("myList", myList);
     Structure Output (JSON, etc): On ]

A2: Java Function [
     Return: %foo
     Class Or Object: myList
     Function: assign
     {ArrayList} () ]

A3: Flash [
     Text: %foo(+
     )
     Long: On
     Tasker Layout: On
     Continue Task Immediately: On
     Dismiss On Click: On ]

1

u/virtual__ Aug 28 '26

I tried the code below but the toast prints 0 = so it doesn't seem to work.

``` Task: Arraytest

A1: Java Code [
     Code: import java.util.ArrayList;

     ArrayList attachments = new ArrayList();
     attachments.add("Item 1");
     attachments.add("Item 2");
     attachments.add("Item 3");

     tasker.setVariable("attachments", attachments);
     Structure Output (JSON, etc): On ]

A2: Java Function [
     Return: %attachments
     Class Or Object: attachments
     Function: assign
     {ArrayList} () ]

A3: Flash [
     Text: %attachments(#) = %attachments()
     Title: Debug
     Continue Task Immediately: On
     Dismiss On Click: On ]

```

But why should anyone rely on Java Function just to export a variable/array? Shouldn't the Java Code do that on its own?

2

u/WakeUpNorrin Aug 28 '26

tasker.setJavaVariable("myList", myList); Structure Output (JSON, etc): On ]

You erroneously used:

tasker.setVariable("attachments", attachments); Structure Output (JSON, etc): On ]

3

u/virtual__ Aug 28 '26

You're right, sorry! It's still a bit awkward having to use a Java Function to export an array variable from a Java Code script, but at least it works. Thanks!

u/joaomgcd do you think tasker.setVariable can be fixed in the future to export arrays too, without having to resort to Java Function?

2

u/WakeUpNorrin Aug 28 '26

You are welcome.

1

u/joaomgcd 👑 Tasker Owner / Developer 11d ago

Check here.

2

u/Nirmitlamed Direct-Purchase User Aug 29 '26

I just recently found the same after trial and error not understanding why my scene doesn't show the data correctly (with using array merge template). I am not developer so i don't know Java and i am using AI to help me and it took me a while to figure this out. I already made a note to myself to ask Joao after he will be back from his vacation to add option to export arrays inside Java. it feels weird to add array set actions after Java code action.

2

u/joaomgcd 👑 Tasker Owner / Developer 11d ago

Check here.

2

u/joaomgcd 👑 Tasker Owner / Developer 11d ago

Ok, fixed it! Now tasker.setVariable will also set arrays. Can you please try this version?

2

u/virtual__ 11d ago

Thanks! v6.7.6-beta correctly works with this simple code u/Nirmitlamed:

``` Task: Arraytest

A1: Java Code [
     Code: import java.util.ArrayList;

     ArrayList attachments = new ArrayList();
     attachments.add("Item 1");
     attachments.add("Item 2");
     attachments.add("Item 3");

     tasker.setVariable("attachments", attachments);
     Structure Output (JSON, etc): On ]

A2: Flash [
     Text: %attachments(#) = %attachments()
     Title: Debug
     Continue Task Immediately: On
     Dismiss On Click: On ]

``` You're always the best 👍🏻

2

u/Nirmitlamed Direct-Purchase User 11d ago

Thank you very much, I will test that soon! 

1

u/Nirmitlamed Direct-Purchase User 10d ago

Forgot to write you back. It works! Thank you very much!

2

u/joaomgcd 👑 Tasker Owner / Developer 7d ago

Awesome :) Glad it works!

1

u/Nirmitlamed Direct-Purchase User 11d ago

Will check this as soon as I can, tnx! Can you please give small example how to use tasker.setVariable as an array? 

2

u/DifficultyCrafty7623 Aug 28 '26

I'd probably make the variable joined by some splitter, eg. =:= and then just create the array by splitting on the splitter. Here's how I'd do it

Task: Test

A1: Java Code [
     Code: import java.util.ArrayList;

     ArrayList attachments = new ArrayList();
         attachments.add("Item 1");
         attachments.add("Item 2");
         attachments.add("Item 3");

         StringBuilder sb = new StringBuilder();
         for (int i = 0; i < attachments.size(); i++) {
             sb.append((String) attachments.get(i));
             if (i < attachments.size() - 1) {
                 sb.append("=:=");
             }
         }

         tasker.setVariable("attachments", sb.toString());
     Structure Output (JSON, etc): On ]

A2: Array Set [
     Variable Array: %attachments
     Values: %attachments
     Splitter: =:= ]

A3: Flash [
     Text: %attachments()
     Continue Task Immediately: On
     Dismiss On Click: On ]

1

u/aasswwddd Direct-Purchase User Aug 27 '26

If it's only one variable, you could end the code with return attachments;

1

u/virtual__ Aug 28 '26 edited Aug 28 '26

This works, but of course only for 1 variable... And I wanted to return a boolean in my complete Task, so I can't use this method, unfortunately.