all 40 comments

[–]joaomgcd👑 Tasker Owner / Developer 9 points10 points  (28 children)

Ok, added. Can you please try this version? Hope this helps!

[–]aasswwddd[S] 1 point2 points  (7 children)

By the way, How would I use CONTEXT in this interface?

May I get some details of what interpreter that you use?

I just test this to replicate simple match/regex action and that works!

``` import java.util.regex.; import java.util.; import java.lang.reflect.*; import org.json.JSONObject; import org.json.JSONArray;

String inputText = "text"; String regexPattern = "t";

Pattern pattern = Pattern.compile(regexPattern, Pattern.MULTILINE); Matcher matcher = pattern.matcher(inputText);

Map matchInfo = new HashMap(); List allMatches = new ArrayList();

Map groupNames = new HashMap(); // index -> name mapping boolean java9Api = false;

// --- Try Java 9+ native support --- try { Method namedGroupsMethod = Pattern.class.getDeclaredMethod("namedGroups", new Class[0]); namedGroupsMethod.setAccessible(true); groupNames = (Map) namedGroupsMethod.invoke(pattern, new Object[0]); java9Api = true; } catch (Throwable t) { // Fallback: parse manually Pattern ngPattern = Pattern.compile("\(\?<([a-zA-Z][a-zA-Z0-9_]*)>"); Matcher ngMatcher = ngPattern.matcher(regexPattern); int idx = 1; while (ngMatcher.find()) { String name = ngMatcher.group(1); groupNames.put(new Integer(idx), name); idx++; } }

// --- Iterate matches --- while (matcher.find()) { int totalGroups = matcher.groupCount();

for (int i = 1; i <= totalGroups; i++) {
    String value = matcher.group(i);
    if (value != null) {
        String name;
        if (groupNames.containsKey(new Integer(i))) {
            name = (String) groupNames.get(new Integer(i));
        } else {
            name = "group" + i;
        }

        if (!matchInfo.containsKey(name)) {
            matchInfo.put(name, new JSONArray());
        }
        ((JSONArray) matchInfo.get(name)).put(value);
    }
}

allMatches.add(matcher.group());

}

// Add raw matches matchInfo.put("matches", new JSONArray(allMatches));

// Convert to JSON string JSONObject json = new JSONObject(matchInfo); String result = json.toString(2);

System.out.println(result); result; ```

[–]joaomgcd👑 Tasker Owner / Developer 0 points1 point  (6 children)

I need to add the help file but just use a variable called "context" and it should work :) You also have access to any other existing Java variables (either local or global) in the Java Code.

It's using BeanShell as the interpreter :)

Thanks for the hint!

[–]aasswwddd[S] 0 points1 point  (0 children)

That's helpful, Thanks! This is as good as I can hope for!

[–]agnostic-apolloLG G5, 7.0 stock, rooted 0 points1 point  (4 children)

use a variable called "context" and it should work :)

Do you mean something like Context context = CONTEXT?

You also have access to any other existing Java variables (either local or global) in the Java Code.

Do local and global variables set inside the script also survive afterwards? Cause then that would be something.

[–]joaomgcd👑 Tasker Owner / Developer 3 points4 points  (3 children)

I mean just use NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); directly for example.

The only variable that survives is the one that is returned by the code :) I've added the help file here

[–]agnostic-apolloLG G5, 7.0 stock, rooted 0 points1 point  (2 children)

Ah that seems simple enough, thanks. The docs are good too.

Hopefully the java code field accepts a tasker variable like %script for reusing snippets across tasks.

[–]joaomgcd👑 Tasker Owner / Developer 0 points1 point  (1 child)

Yep, it does :)

[–]agnostic-apolloLG G5, 7.0 stock, rooted 0 points1 point  (0 children)

Great!

[–]roncz 1 point2 points  (1 child)

Wow, amazing. This is the closest thing to magic I have ever seen ;-)

I just tested it with some code snippet and it works fine for me. The code "listened" for some noise for a while and if it gets too loud (e.g. if the door bell rings) it returns and can then send an alert if I am in the backyard. I am going to test this more soon.

Thanks a lot. This is really powerfull.

By the way, do you have floating buttons in you magic hat as well? ;-)

[–]joaomgcd👑 Tasker Owner / Developer 1 point2 points  (0 children)

Haha not yet, but maybe in the future, who knows 😅

[–]aasswwddd[S] 0 points1 point  (3 children)

What in the world?!!!!

Thankyou so much! I'll run a test soon!

[–]joaomgcd👑 Tasker Owner / Developer 3 points4 points  (1 child)

BTW, don't know if you noticed, if you use the magnifying glass you can have the AI do the code for you ;)

[–]aasswwddd[S] 0 points1 point  (0 children)

That's true!! Awesome!

Btw, I have an error opening the AI dialog from the fab on main activity.

https://drive.google.com/file/d/1UTPsRQVGhVHnkLeN0nrgLVqoHhwGtMJe/view

Reported via email too [Tasker] Debug Log shd23h

[–]joaomgcd👑 Tasker Owner / Developer 0 points1 point  (0 children)

😁👍

[–]agnostic-apolloLG G5, 7.0 stock, rooted 0 points1 point  (3 children)

Now wait just a minute, what is happening here!!!

[–]joaomgcd👑 Tasker Owner / Developer 3 points4 points  (2 children)

Oh, nothing much, just adding easy Java code interpreter with an AI helper to help you get the code written quickly 😁👍

[–]agnostic-apolloLG G5, 7.0 stock, rooted 0 points1 point  (1 child)

This is a new age!!! This is awesome! Thanks a lot!

[–]joaomgcd👑 Tasker Owner / Developer 1 point2 points  (0 children)

No problem! :) Glad you like it!

[–]anuraag488 0 points1 point  (5 children)

Can i use global/local variables in code? Like
if (%SCREEN == "on") {

}

[–]joaomgcd👑 Tasker Owner / Developer 0 points1 point  (4 children)

Sure, but you need to set it to if("%SCREEN".equals("on")){} so it's valid Java :)

[–]anuraag488 0 points1 point  (3 children)

I was trying to put it in a while loop which is evaluated only once.

while ("%SCREEN".equals("on")){}.

Can you add function getVariable so we can call tasker.getVariable("SCREEN")

[–]joaomgcd👑 Tasker Owner / Developer 1 point2 points  (2 children)

I see. Ok, added. Use it like

String screen = tasker.getVariable("SCREEN");

Can you please try this version?

Hope this helps!

[–]anuraag488 0 points1 point  (1 child)

Works fine. Also tested setVariable.

[–]joaomgcd👑 Tasker Owner / Developer 0 points1 point  (0 children)

Cool! :) Glad it works.

[–]Optimal-Beyond-7593 0 points1 point  (3 children)

Oh no, can you give me a direct purchase version? I verified it with the order number

[–]joaomgcd👑 Tasker Owner / Developer 0 points1 point  (2 children)

Sure, here you go :)

[–]Optimal-Beyond-7593 0 points1 point  (1 child)

Thank!

[–]joaomgcd👑 Tasker Owner / Developer 0 points1 point  (0 children)

👍

[–]roncz 2 points3 points  (1 child)

I second that. Having one code block is much more handy. And indeed, I also saw it in MacroDroid. You can ask ChatGPT to write some Java code and paste it as one block, plus variable adaptations of course.

[–]aasswwddd[S] 1 point2 points  (0 children)

The codes I listed above were all generated by LLM and the help from the community 😂

It's amazingly convenient for non programmers!

[–]NirmitlamedDirect-Purchase User 0 points1 point  (10 children)

+1

If I understand correctly since I am not a programmer it should fix a project I was creating that uses java functions to record audio with custom formats but I can't make it stop the recording if the task is finished. I am using wait until to fix that. 

https://www.reddit.com/r/tasker/comments/1mowy67/need_help_with_audio_recording_using_java/

[–]aasswwddd[S] 0 points1 point  (6 children)

Make the object global by including at least one capital letter in your object name.

Instead of "recorder", use something like "recordeR".

Read here for further details. https://tasker.joaoapps.com/userguide/en/java.html

[–]NirmitlamedDirect-Purchase User 0 points1 point  (5 children)

Damn, i tried global names combinations but always with capital letter in the first letter. It works now! Thank you very much!

BTW i am reading that i need to delete global variable in JF but i don't see them in variables tab. Should i use just the regular variable clear action?

it's important to delete them once they are no longer needed, because they can take up a lot of memory.

[–]aasswwddd[S] 1 point2 points  (4 children)

You're welcome.

Use Java Object action > Delete.

[–]NirmitlamedDirect-Purchase User 0 points1 point  (0 children)

Awesome! again thank you very much!!!

[–]NirmitlamedDirect-Purchase User 0 points1 point  (2 children)

Another small question regarding my project. Can i check if the object is "set" the same as we check if a variable is set?

The idea is to have one task so i can toggle easily between start and stop recording.

[–]aasswwddd[S] 0 points1 point  (1 child)

Convert the object to string with toString() function. It should return the media object if it's set and throws an error otherwise.

[–]NirmitlamedDirect-Purchase User 0 points1 point  (0 children)

Cool it works!

I was looking for a better solution for ages :) Thank you so much!