Page 1 of 1
JavaScript - assign result of fn to variable robotakst
Posted: Tue Oct 08, 2024 9:10 pm
by felipe
function fn(a, b) {
return a + " " + b;
var result = fn("hello", "world");
RoboTaskApp.AddUserVariable(RoboTaskApp.ExpandText(VariableNameResult, result);
could help format the synthase
tks
Re: JavaScript - assign result of fn to variable robotakst
Posted: Wed Oct 09, 2024 7:48 am
by Oleg
Look at my example below
Code: Select all
;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|444033236
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task1465"
Hide=INTEGER|0
ID=INTEGER|2102349058
LogOnAsUser=INTEGER|1
Name=STRING|"JS Script..."
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0
[Actions]
Action1=FOLDER
Action2=FOLDER
[Actions\Action1]
ActionID=STRING|"A_SCRIPT_JSSCRIPT"
Enabled=INTEGER|-1
Name=STRING|"JS Script"
Params=FOLDER
[Actions\Action1\Params]
expandvars=STRING|"0"
line00000000=STRING|"function myfn(a, b)"
line00000001=STRING|" {"
line00000002=STRING|" return a + "" "" + b;"
line00000003=STRING|" }"
line00000005=STRING|"VariableNameResult = ""ResVar"";"
line00000006=STRING|"result = myfn(""hello"", ""world"");"
line00000008=STRING|"//logmessage(result);"
line00000009=STRING|"RoboTaskApp().SetUserVariable(VariableNameResult, result);"
linecount=STRING|"11"
savescript=STRING|"0"
[Actions\Action2]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log Message"
Params=FOLDER
[Actions\Action2\Params]
message=STRING|"{ResVar}"
type=STRING|"3"
The script is
Code: Select all
function myfn(a, b)
{
return a + " " + b;
}
VariableNameResult = "ResVar";
result = myfn("hello", "world");
RoboTaskApp().SetUserVariable(VariableNameResult, result);
Re: JavaScript - assign result of fn to variable robotakst
Posted: Thu Oct 09, 2025 8:32 pm
by felipe
I'd like to return to this post so I don't have to open another one, in this variable assignment context
in this context the variable is always applying globally, I'd like it to be only within the task
I've already declared it at the beginning of the task with the same name as the variable assigned in the function, the function applies globally, this interferes with another task with the same variable name
Re: JavaScript - assign result of fn to variable robotakst
Posted: Fri Oct 10, 2025 7:57 am
by Oleg
Try to use variable name like this:
"local@ResVar"
Also you can create local variable with necessary name
Look at my example below. I have global variable
MyVar. By the script set value to local variable
MyVar
Code: Select all
;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|307868550
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task1583"
Hide=INTEGER|0
ID=INTEGER|-1222492109
LogOnAsUser=INTEGER|1
Name=STRING|"Global and local variables"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RestrictRESTAPIAccess=INTEGER|0
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0
[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER
[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|0
Name=STRING|"Set variable ""local@myvar"" with value ""Local var"""
Params=FOLDER
[Actions\Action1\Params]
_rt_variables_produced=STRING|"local@myvar"
expand=STRING|"0"
linecount=STRING|"1"
varname=STRING|"local@myvar"
varvalue=STRING|"Local var"
[Actions\Action2]
ActionID=STRING|"A_SCRIPT_JSSCRIPT"
Enabled=INTEGER|-1
Name=STRING|"JS Script"
Params=FOLDER
[Actions\Action2\Params]
expandvars=STRING|"0"
line00000000=STRING|"function myfn(a, b)"
line00000001=STRING|" {"
line00000002=STRING|" return a + "" "" + b;"
line00000003=STRING|" }"
line00000005=STRING|"VariableNameResult = ""local@MyVar"";"
line00000006=STRING|"result = myfn(""hello"", ""world"");"
line00000008=STRING|"RoboTaskApp().SetUserVariable(VariableNameResult, result);"
linecount=STRING|"9"
savescript=STRING|"0"
[Actions\Action3]
ActionID=STRING|"A_DIALOG_MESSAGE"
Enabled=INTEGER|-1
Name=STRING|"Show ""{global@myvar}"""
Params=FOLDER
[Actions\Action3\Params]
icon=STRING|"1"
msg0=STRING|"{global@myvar}"
msg1=STRING|"{local@myvar}"
msgcount=STRING|"2"
playsound=STRING|"0"
showmessage=STRING|"1"
Also read about prefixes here:
https://robotask.com/help/index.html?pr ... iables.htm
Re: JavaScript - assign result of fn to variable robotakst
Posted: Fri Oct 10, 2025 8:24 pm
by felipe
[pyhton]
from robotask import *
a = 23.5
b = 37.8
c = a + b
rt = RoboTask()
rt.set_user_var('local@MyVariable', str(c))
if it is correct, it is not working, it is not applying the variable
Re: JavaScript - assign result of fn to variable robotakst
Posted: Sat Oct 11, 2025 3:01 pm
by Oleg
Unfortunately Python Script and Node.JS script actions can set only local variables now and does not process prefixes.
We'll fix this problem in next release.