Using code of a JavaScript file in a following JavaScriptlet action

417 views
Skip to first unread message

francwalter

unread,
Aug 30, 2016, 8:34:55 AM8/30/16
to Tasker
Hallo

I have a first action where I include a JavaScript File moment.js into my task. Now I want to use all these functions in the javascript library, but when I try to use them in a second action JavaScriptlet none of these variables or functions are available.
Is this normal behaviour?
Do I need to write all my stuff into the js-file so I only can use them?

E.g.: the js-file contains a function test(){alert'test ok';}
In my first action I bind this file, in my second action I have a Javascriptlet with just: test();
But nothing happens.
Not possible this way? Don't find it in the userguide.

Thank

frank

Abdullah Alahdal

unread,
Aug 30, 2016, 9:29:01 AM8/30/16
to Tasker
If I understood your case, you are putting the using the Java library path in one action and trying to execute its functions in another action. As far as I know, you have to put the path to the library and functions in the same action.

francwalter

unread,
Aug 30, 2016, 9:33:45 AM8/30/16
to Tasker
Am Dienstag, 30. August 2016 15:29:01 UTC+2 schrieb Abdullah Alahdal:
If I understood your case, you are putting the using the Java library path in one action and trying to execute its functions in another action. As far as I know, you have to put the path to the library and functions in the same action.

I tried a JavaScriptlet in only one action where I put my moment.js in the Libraries field, but still not doing it.
If I add the exact code of my JSlet into the js-file at the very end, the code is working though.
 

Abdullah Alahdal

unread,
Aug 30, 2016, 9:36:53 AM8/30/16
to Tasker
Could you share your task description here? It will be easier to help.

Frank Röhm

unread,
Aug 30, 2016, 9:39:31 AM8/30/16
to tas...@googlegroups.com


Am 30. August 2016 15:36:53 MESZ, schrieb Abdullah Alahdal <alahda...@gmail.com>:
>Could you share your task description here? It will be easier to help.

Test (630)
A1: JavaScriptlet [ Code:var result = moment('2016-12-31').diff(moment('2016-01-01'), 'days'); Libraries:Tasker/JavaScript/moment.js Auto Exit:On Timeout (Seconds):10 ]


Abdullah Alahdal

unread,
Aug 30, 2016, 9:48:35 AM8/30/16
to Tasker
What happen if you click the Play button at the top right corner?

I cannot really judge about this.

francwalter

unread,
Aug 30, 2016, 9:51:43 AM8/30/16
to Tasker
Am Dienstag, 30. August 2016 15:48:35 UTC+2 schrieb Abdullah Alahdal:
What happen if you click the Play button at the top right corner?


It says: "OK". But the var result is unset (I didnt post the second action where I have a popup-flash with %result).

Abdullah Alahdal

unread,
Aug 30, 2016, 10:16:14 AM8/30/16
to Tasker
I've just downloaded moment.js from momentjs.com and tried your code, flashed %result and it works it flashed 365

Abdullah Alahdal

unread,
Aug 30, 2016, 10:17:48 AM8/30/16
to Tasker
I got it. Maybe the path to library is wrong. You may need to put full path. In my case "/storage/emulated/0/Tasker/Scripts/moment.js" without double quotes.

francwalter

unread,
Aug 30, 2016, 11:01:18 AM8/30/16
to Tasker
Am Dienstag, 30. August 2016 16:17:48 UTC+2 schrieb Abdullah Alahdal:
I got it. Maybe the path to library is wrong. You may need to put full path. In my case "/storage/emulated/0/Tasker/Scripts/moment.js" without double quotes.

YES!!! Thank! Now it works.
So if I need this library in a JavaScriptlet action I need to load it each time, or better, I put all code in one JavaScriptlet action.
Thank you!!!

francwalter

unread,
Aug 31, 2016, 10:44:22 AM8/31/16
to Tasker
Am Dienstag, 30. August 2016 17:01:18 UTC+2 schrieb francwalter:
Am Dienstag, 30. August 2016 16:17:48 UTC+2 schrieb Abdullah Alahdal:
I got it. Maybe the path to library is wrong. You may need to put full path. In my case "/storage/emulated/0/Tasker/Scripts/moment.js" without double quotes.

YES!!! Thank! Now it works.


But unfortunately JavaScriptlet ignores my Tasker variables :(

I did this:

Test (202)
    A1
: Variable Set [ Name:%start_date To:'01.01.2016' Do Maths:Off Append:Off ]
    A2
: Variable Set [ Name:%end_date To:'31.12.2016' Do Maths:Off Append:Off ]
    A3
: JavaScriptlet [ Code:var result = moment(end_date).diff(moment(start_date), 'days'); Libraries:/storage/sdcard0/Tasker/JavaScript/moment.js Auto Exit:On Timeout (Seconds):10 ]
    A4
: Flash [ Text:%result Long:Off ]
Code hier eingeben...

and the answer is nan (not a noun), doesnt work. I also tried it without the ' marks and also with " instead of '.
But when I put the var. into the Javascriptlet:

Test (202)
    A1
: JavaScriptlet [ Code:var start_date = '01.01.2016';
var end_date = '31.12.2016';
var result = moment(end_date).diff(moment(start_date), 'days'); Libraries:/storage/sdcard0/Tasker/JavaScript/moment.js Auto Exit:On Timeout (Seconds):10 ] 
    A2
: Flash [ Text:%result Long:Off ] Code hier eingeben...

The result is 365, which is right. 
What is this?

I tested it the other way, without the moment.js, just a Javascriptlet which uses Tasker variables, this works without problems.

Is this my fault, am I using the Javascriptlet wrongly? 

Abdullah Alahdal

unread,
Aug 31, 2016, 12:41:26 PM8/31/16
to Tasker
I think the issue is with the date format, I tried your example with different date format and it works.

Test (469)
A1: Variable Set [ Name:%start_date To:2016-01-01 Do Maths:Off Append:Off ]
A2: Variable Set [ Name:%end_date To:2016-12-31 Do Maths:Off Append:Off ]
A3: JavaScriptlet [ Code:var result = moment(end_date).diff(moment(start_date), 'days');  Libraries:/storage/emulated/0/Tasker/Scripts/momentl.js Auto Exit:On Timeout (Seconds):45 ]

francwalter

unread,
Sep 1, 2016, 1:28:51 AM9/1/16
to Tasker
Am Mittwoch, 31. August 2016 18:41:26 UTC+2 schrieb Abdullah Alahdal:
I think the issue is with the date format, I tried your example with different date format and it works.


Oh man! Yes, that is right. It works like that. I searched in the total wrong direction and was blind of this obvious and visible difference in my example.
Thanks again, now all clear :)

frank
Reply all
Reply to author
Forward
0 new messages