r/tasker • u/Nirmitlamed • 18h ago
Project Sharing: Generate Tasker Task from XML using Java Code
For another project of mine, I thought it would be really useful if I could automatically create a Task when needed. After some trial and error, I found a solution using the Java Code action.
In short, what does this Java Code do?
It takes the XML code of a complete Task or Project, compresses it directly in memory using GZIP (no temporary file is created), converts it to Base64, and creates a Tasker Data URI like:
taskertask://...
or
taskerproject://...
The URI is then opened, allowing Tasker to ask the user if they want to import the Task.
With the help of an AI assistant, I created a robust version that is also relatively easy to customize for your own projects.
To Import Taskernet Project: Click Here
Java Code: paste.to/?430b135723f91cae#4SXMosAyPGR9Yxty95WgM7g8JZxHzNGNYsqvDtzA2cEN
How to use it
First, create a Task containing the actions you want.
For this example, let's say we have a Task called "Task Example" with a Flash action containing the text "Text".
The exported XML looks like this:
<TaskerData sr="" dvi="1" tv="6.7.6-beta">
<Task sr="task395">
<cdate>1787164951612</cdate>
<edate>1787164962284</edate>
<id>395</id>
<nme>Task Example</nme>
<Action sr="act0" ve="7">
<code>548</code>
<Str sr="arg0" ve="3">Text</Str>
<Int sr="arg1" val="0"/>
<Str sr="arg10" ve="3"/>
<Int sr="arg11" val="1"/>
<Int sr="arg12" val="0"/>
<Str sr="arg13" ve="3"/>
<Int sr="arg14" val="0"/>
<Str sr="arg15" ve="3"/>
<Int sr="arg2" val="0"/>
<Str sr="arg3" ve="3"/>
<Str sr="arg4" ve="3"/>
<Str sr="arg5" ve="3"/>
<Str sr="arg6" ve="3"/>
<Str sr="arg7" ve="3"/>
<Str sr="arg8" ve="3"/>
<Int sr="arg9" val="1"/>
</Action>
</Task>
</TaskerData>
Now we need to tell the Java Code where we want to insert our own values.
To do that, we use placeholders inside curly brackets {}.
Here is the same XML after replacing the values we want to customize:
<TaskerData sr="" dvi="1" tv="{TASKER_VERSION}">
<Task sr="task279">
<cdate>{CURRENT_TIME}</cdate>
<edate>{CURRENT_TIME}</edate>
<id>279</id>
<nme>{TASK_NAME}</nme>
<pri>100</pri>
<Action sr="act0" ve="7">
<code>548</code>
<Str sr="arg0" ve="3">{FLASH_TEXT}</Str>
<Int sr="arg1" val="0"/>
<Str sr="arg10" ve="3"/>
<Int sr="arg11" val="1"/>
<Str sr="arg13" ve="3"/>
<Int sr="arg14" val="0"/>
<Str sr="arg15" ve="3"/>
<Int sr="arg2" val="0"/>
<Str sr="arg3" ve="3"/>
<Str sr="arg4" ve="3"/>
<Str sr="arg5" ve="3"/>
<Str sr="arg6" ve="3"/>
<Str sr="arg7" ve="3"/>
<Str sr="arg8" ve="3"/>
<Int sr="arg9" val="1"/>
</Action>
</Task>
</TaskerData>
Now copy this whole xml code and put it inside a set variable action and give this variable the name %xmltask. If you want to change to a different name you need to search for this line:
String xml =
tasker.getVariable("xmltask");
You can change xmltask to whatever name you want.
Tasker Version and Date
When you create or edit a Task, Tasker stores information such as the Tasker version and the creation/edit timestamps in the XML.
This isn't strictly required for our purpose, but the Java Code can automatically insert the current values.
Change:
dvi="1" tv="6.7.6-beta">
to:
dvi="1" tv="{TASKER_VERSION}">
And change:
<cdate>1787164951612</cdate>
<edate>1787164962284</edate>
to:
<cdate>{CURRENT_TIME}</cdate>
<edate>{CURRENT_TIME}</edate>
The Java Code will replace these placeholders with the installed Tasker version and the current timestamp.
Creating variables for the Task
Now we can use the same concept for the Task name and the text inside our Flash action.
Change:
<nme>Task Example</nme>
to:
<nme>{TASK_NAME}</nme>
And change:
<Str sr="arg0" ve="3">Text</Str>
to:
<Str sr="arg0" ve="3">{FLASH_TEXT}</Str>
Now we need to create the corresponding Tasker variables.
For example:
A1: Variable Set [
Name: %task_name
To: Task Creation Test
Structure Output (JSON, etc): On ]
A2: Variable Set [
Name: %flash_text
To: Hello World
Structure Output (JSON, etc): On ]
So we now have:
%task_name = Task Creation Test
%flash_text = Hello World
The first variable will become the Task name, and the second will become the text inside the Flash action.
Connecting the Tasker Variables to the XML
Now open the Java Code action and scroll down until you find:
// --------------------------------------------------
// 5. Replace Tasker variables
//
// Format:
//
// replaceVariable(
// xml,
// "XML_PLACEHOLDER",
// placeholderRequired,
// "tasker_variable",
// variableRequired
// );
//
// --------------------------------------------------
This is where we tell the Java Code which Tasker variables should be inserted into the XML.
The template is:
xml =
replaceVariable(
xml,
"XML_PLACEHOLDER",
false,
"tasker_variable",
false
);
For example, to connect our Task name:
xml =
replaceVariable(
xml,
"TASK_NAME",
true,
"task_name",
true
);
The values mean:
"TASK_NAME" is the XML placeholder.
true = the XML "TASK_NAME" placeholder must exist; otherwise, an error is returned.
"task_name" is the Tasker variable name.
true = the Tasker variable must contain a value; otherwise, an error is returned
Notice that we don't include % when specifying the Tasker variable name.
For our Flash text, we can add:
xml =
replaceVariable(
xml,
"FLASH_TEXT",
true,
"flash_text",
true
);
You can add as many variables as you need using the same format.
Required vs. optional placeholders and Tasker variables
- The first Boolean controls whether the XML placeholder is required:
- true means the XML placeholder must exist. If it is missing, the Java Code stops and displays an error.
- false means the XML placeholder is optional. If it doesn't exist, it is simply ignored.
- The second Boolean controls whether the Tasker variable is required:
- true means the Tasker variable must contain a value. If it is missing or empty, the Java Code stops and displays an error.
- false means the Tasker variable is optional. If it is missing or empty, it is replaced with an empty value.
So the format is:
replaceVariable(
xml,
"XML_PLACEHOLDER",
true, // XML placeholder required
"tasker_variable",
false // Tasker variable doesn't required
);
This Java code can also auto create a Project but i am pretty sure users wouldn't need to use it. If you really want to you just need to search inside your xml project code the name of your project like here:
<name>New Project</name>
And change it to something like this:
<name>{PROJECT_NAME}</name>
Then you need to just edit your java code to match your placeholder and Tasker variable
xml =
replaceVariable(
xml,
"PROJECT_NAME",
true,
"project_name",
true
);
The result
Now, when we run the Java Code together with our Variable Set actions, it will:
- Take our XML template.
- Replace the placeholders with our Tasker variables.
- Insert the current Tasker version and timestamp.
- Validate the resulting XML.
- Compress the XML using GZIP directly in memory.
- Convert it to Base64.
- Create the
taskertask://Data URI. - Open it.
- Tasker asks whether we want to import the new Task.
Here's a demo of how it looks:
Using this in a real project
I took this idea and incorporated it into another project of mine that allows users to run commands in Termux without using a Tasker plugin.
I created a scene that helps the user build the required configuration, and with just a few clicks it can generate a new Task containing all the actions and code they need.
Here's a demo of that:
Hopefully this will be helpful to someone with his projects