Javascript return into variable

348 views
Skip to first unread message

frederico allan

unread,
Mar 25, 2015, 4:41:44 PM3/25/15
to tas...@googlegroups.com
i have a webscene and textedit called txtdate.

how can i run a javascript function and populate txtdate with that?

ps: my js script:

function myresult(){
var d = new Date();
var d= d.getDate();
var m = d.getMonth() + 1;
var y = d.getFullYear();
var num = "8"
var result = "v" + y + "." + m + "." + d + "." + num;
return (result);
}

Jeremy Harris

unread,
Mar 25, 2015, 5:22:58 PM3/25/15
to tas...@googlegroups.com
Here's how I do it, if I understand you correctly:

tk.setLocal("srise", hmstring(suntimes[0]+TZ));

... where 'srise' will be accessible in Tasker as '%srise', and it's populated with the value of first index of the array 'suntimes' which is set earlier in the code, then formatted by the function 'hmstring'.

Jeremy Harris

unread,
Mar 25, 2015, 5:27:26 PM3/25/15
to tas...@googlegroups.com
So, I think yours would be:

tk.setLocal("textdate", result);

frederico allan

unread,
Mar 25, 2015, 5:47:27 PM3/25/15
to tas...@googlegroups.com
when and where i put that code "tk.setLocal("textdate", result);"?

where my javascript function would be stay? into a task javascriptlet before show myscene?

Jeremy Harris

unread,
Mar 25, 2015, 6:10:24 PM3/25/15
to tas...@googlegroups.com
i have a webscene and textedit called txtdate.

I am understanding that to mean a scene with a Web View element that uses a javascript html and a Text Edit element. Is that incorrect?

frederico allan

unread,
Mar 25, 2015, 7:01:15 PM3/25/15
to tas...@googlegroups.com
correct.



when scene show i want txtedrelease populate with result of my javascript return.

I dont now where i put my javascript or how can i store the result into a variable.

Ruslan Alcotana

unread,
Mar 25, 2015, 7:09:04 PM3/25/15
to tas...@googlegroups.com
Your js script has errors! Fixed ver:
function myresult(){
var date = new Date();
var d = date.getDate();
var m = date.getMonth() + 1;
var y = date.getFullYear();

Ruslan Alcotana

unread,
Mar 25, 2015, 7:11:31 PM3/25/15
to tas...@googlegroups.com
var num = "8";

Ruslan Alcotana

unread,
Mar 25, 2015, 7:26:43 PM3/25/15
to tas...@googlegroups.com
Fix your script. After the task that shows your scene:
1. Set a new tasker variable %textdate to "null"
2. Add javascriptlet:
Code: textdate=myresult();
Libraries (path to your script): file:///storage/emulated/0/Tasker/myresult.js
3. Action-> Scene -> Element text -> %textdate

Odysseos

unread,
Mar 26, 2015, 6:09:54 AM3/26/15
to tas...@googlegroups.com
Your task should be something like this:
Date (412)
A1: JavaScriptlet [ Code:function myresult() {
date = new Date();
d = date.getDate();
m = date.getMonth();
y = date.getFullYear();
}
myresult()
res = d + "_" + m + "_" + y;
tk.setLocal("text", res); Libraries: Auto Exit:On Timeout (Seconds):45 ]
A2: Flash [ Text:%text Long:On ]

Ruslan Alcotana

unread,
Mar 26, 2015, 9:26:37 AM3/26/15
to tas...@googlegroups.com
I forgot. As for my answer, you need to use Global variables. A variable should have one or more capital letter a in its name. So you should use a variable like textDate, for example.

frederico allan

unread,
Mar 27, 2015, 3:09:01 PM3/27/15
to tas...@googlegroups.com
tks all,

JavaScriptlet with tk.setLocal(varname, result) works for me

Danielo515

unread,
Mar 28, 2015, 9:26:37 AM3/28/15
to tas...@googlegroups.com
If you are using a javascriptlet you don't need to use the setLocal thing. Just declare the variable inside the javascript code and it will be available on Tasker as a local variable automatically. Use all lowercase letters or it will not work. This is simpler than you are doing!

Ludovico D'Argenio

unread,
Mar 28, 2015, 10:12:24 AM3/28/15
to tas...@googlegroups.com

Yes,  you are right.  But in this case you first have to declare the variable in Tasker. So,  you task will be:

Date2 (419)
A1: Variable Set [ Name:%res To:0 Do Maths:Off Append:Off ]
A2: JavaScriptlet [ Code:function myresult() {


date = new Date();
d = date.getDate();

m = date.getMonth() + 1;
y = date.getFullYear();
}
myresult()
res = d + "-" + m + "-" + y; Libraries: Auto Exit:On Timeout (Seconds):45 ]
A3: Flash [ Text:%res Long:On ]


Il 28/Mar/2015 14:26 "Danielo515" <rdan...@gmail.com> ha scritto:
>
> If you are using a javascriptlet you don't need to use the setLocal thing. Just declare the variable inside the javascript code and it will be available on Tasker as a local variable automatically. Use all lowercase letters or it will not work. This is simpler than you are doing!
>

> --
> You received this message because you are subscribed to a topic in the Google Groups "Tasker" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/tasker/EbVgNQUTs30/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to tasker+un...@googlegroups.com.
> Visit this group at http://groups.google.com/group/tasker.
> For more options, visit https://groups.google.com/d/optout.

Danielo515

unread,
Mar 28, 2015, 12:42:06 PM3/28/15
to tas...@googlegroups.com
That javascript code is wrong. The variables d, m, and y are local variables of myresult function. You can not access them outside that function. What you should do is to add a return statement inside that function that returns the composed string.

Again talking about your example you don't need the first task if you declare the variable inside the javascriptlet. Try removing the first task (A1) and add this to your js code :

var result = myresukt();

The flash will work, I promise.
Reply all
Reply to author
Forward
0 new messages