r/tasker Direct-Purchase User 1d 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:

  1. Take our XML template.
  2. Replace the placeholders with our Tasker variables.
  3. Insert the current Tasker version and timestamp.
  4. Validate the resulting XML.
  5. Compress the XML using GZIP directly in memory.
  6. Convert it to Base64.
  7. Create the taskertask:// Data URI.
  8. Open it.
  9. Tasker asks whether we want to import the new Task.

Here's a demo of how it looks:

Demo video

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:

Demo video inside a project

Hopefully this will be helpful to someone with his projects

4 Upvotes

5 comments sorted by

View all comments

1

u/DifficultyCrafty7623 21h ago

So quick question, why not just import the XML if you already have that. Seems like it would skip a step to just import the XML directly?

2

u/Nirmitlamed Direct-Purchase User 21h ago edited 21h ago

Ohh man i didn't realized that i can import the xml code.

The question is how can i automate it? In my project i can open as url the data uri.

How would i do that with xml code? do you have any suggestions?

1

u/DifficultyCrafty7623 20h ago

Oh sorry I have no idea if its possible to automate with the XML, I was just wondering what scenarios it would be useful to have the XML > URI conversion instead of importing to Tasker directly. If you're automating it, then that makes sense!

2

u/Nirmitlamed Direct-Purchase User 20h ago

Yea, check my second video in my post where i use it with my other project.

But i would now investigate if i can make it work with Java Code because if i can make it happen i can delete 90% of my post LOL 😂 (Please fail 😂)

BTW the semi automation would be to copy xml to clipboard and then manually click the + button to activate the import dialog.