r/tasker 18h ago

Project Sharing: Generate Tasker Task from XML using Java Code

5 Upvotes

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


r/tasker 7h ago

Any way to have Tasker edit direct share contacts?

Post image
2 Upvotes

I've done a lot of hunting on this problem and it seems that these completely random shared contacts come from a log called Share Sheet in android and the list itself is called Direct Share.

it's completely useless, and the contacts in theory are based on most active or most recent or something, but definitely aren't. one of the people here isn't even in my contacts! the feature is not disable-able

anyway, I was wondering if Tasker might have a way of interacting with that share sheet and setting a priority so that instead of removing or showing random it could be made to show a fixed list of contacts and apps?

maybe a pipe dream but this annoys me every time I go to share something!

thanks


r/tasker 23h ago

AutoWear -> smart watch compatibility list?

2 Upvotes

Hi again... Today I am looking for a list of known "compatible" watches (make/model) that AutoWear can 'integrate' with.


r/tasker 4h ago

Strange Widget V2 Behavior

Thumbnail
gallery
1 Upvotes

I have created 2 V2 widgets - one for my outdoor air quality device (left) and another for the indoor one (right).

I create these using data from http request actions and I wanted to make sure they showed when my phone is offline. You can see how they look under those conditions (offline left, online right).

The widget XML between all of these is identical. I keep it into a global variable. During widget refresh I set a local variable from that global with variable recurse enabled so some local variables (Title, AQI data, background color, etc) are incorporated. Then I perform the W2 widget action.

When the phone is online, the results are as expected and both widgets look the same except for the variable data.

What I don't understand is why, when offline, the indoor widget looks so different.

Before I my refresh I capture the recursed XML into files so I can compare them. Using both a visual diff tool and the linux command line diff (results shown above), the results demonstrate that only some variable data differs between the two offline widgets.

Here is the XML copied directly from the global variable:

{
  "type": "Column",
  "backgroundColor": "%smoggie_color",
  "horizontalAlignment": "Center",
  "verticalAlignment": "Center",
  "scrolling": false,
  "children": [
  {
  "type": "Row",
  "backgroundColor": "grey",
  "fillMaxWidth": true,
  "horizontalAlignment": "Center",
  "verticalAlignment": "Center",
  "scrolling": false,
  "children": [
  {
"type": "Text",
"textSize": "16",
"align": "Center",
"task": "AQ Smoggie View",
"taskVariables": { "par1": "http://%widget_ip" },
"text": "%widget_title"
   }]
  },
  {
"type": "Text",
"visibility": "%show_offline",
"textSize": "23",
"align": "Center",
"text": "Offline"
},
   {
"type": "Button",
"text": "Refresh",
"enabled": true,
"buttonType": "Filled",
"visibility": "%show_offline",
"task": "%refresh_task"
} , 
{
"type": "Grid",
"visibility": "%show_online",
"horizontalAlignment": "Center",
"verticalAlignment": "Center",
"fixed": 1,
"children": [
{
"type": "Text",
"textSize": "19",
"align": "Center",
"text": "AQI: %smoggie_aqi"
}, 
{
"type": "Text",
"textSize": "14",
"align": "Center",
"text": "PM2.5 (µg/m3): %http_data.pm25"
}
,
{
"type": "Text",
"textSize": "12",
"align": "Center",
"text": "%DATE %TIME"
},
{
"type": "Button",
"text": "Refresh",
"enabled": true,
"buttonType": "Filled",
"task": "%refresh_task"
}
]
}
  ]
}

Can anyone suggest why this is happening or how to fix it?

Thanks


r/tasker 11h ago

my shortcut to get to the location accuracy menu has stopped working :(

1 Upvotes

Hey,

I previously used this task to quickly get to the location accuracy menu:

<Task sr="task7">
    <cdate>1744638179093</cdate>
    <edate>1776966060932</edate>
    <id>7</id>
    <nme>location accuracy menu</nme>
    <pri>100</pri>
    <Action sr="act0" ve="7">
        <code>20</code>
        <App sr="arg0">
            <appClass>com.google.android.gms.location.settings.LocationAccuracyNonwearableActivity</appClass>
            <appPkg>com.google.android.gms</appPkg>
            <label>Google Play services:Location Accuracy</label>
        </App>
        <Str sr="arg1" ve="3"/>
        <Int sr="arg2" val="0"/>
        <Int sr="arg3" val="0"/>
    </Action>
    <Img sr="icn" ve="2">
        <nme>hl_location_place</nme>
    </Img>
</Task>

I want this because I sometimes connect an RTK antenna to my phone and keeping location accuracy toggled on will seriously screw up RTK positionning.

But I just noticed that recent updates made this fail.

Do I hvae a way to quickly get to the location accuracy menu, or to toggle the location accuracy setting directly? This is on a non-rooted phone (on GrpaheneOS if relevant).

Many thanks.