Re: [openhab] import in scripts

491 views
Skip to first unread message

Kai Kreuzer

unread,
Jun 29, 2014, 5:05:14 PM6/29/14
to ope...@googlegroups.com
Hi Alexander,

This is indeed an awkward limitation in the scripts - imports simply do not work here.

Instead, you have to use the fully qualified name of classes that you want to use.

Regards,
Kai

Am 29 Jun 2014 um 22:54 schrieb Alexander Yakush <alexande...@firstlinesoftware.com>:

Hi all! I need to use "string.replace" method in my script, but doing "import java.io.*" seems not to be possible in scripts:

00:52:49.461 ERROR o.o.c.s.ScriptExecutionThread[:50] - Error during the execution of rule 'test helper': An error occured during the script execution: The name 'import' cannot be resolved to an item or type.

Am I doing something really wrong or this is scripts limitation?

P.S.
I'm using 1.5 on Windows

--
You received this message because you are subscribed to the Google Groups "openhab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
For more options, visit https://groups.google.com/d/optout.

Alexander Yakush

unread,
Jun 29, 2014, 5:55:38 PM6/29/14
to ope...@googlegroups.com
Hello Kai!

Thank you very much for quick response! I wish I were Java developer to understand how to do that :) Could you please point me in the right direction or give an example of using any fully qualified name of class in scripts?

Kai Kreuzer

unread,
Jun 30, 2014, 4:45:20 AM6/30/14
to ope...@googlegroups.com
Well, that depends on what you actually want to use from java.io. I cannot see this from your script.
Simply remove this import - string operations (like String.replace) should work right away.

Alexander Yakush

unread,
Jun 30, 2014, 10:58:56 AM6/30/14
to ope...@googlegroups.com
Unfortunately it does not work this way (without importing java.io.*) - i got this error message:

18:50:46.671 ERROR o.o.c.s.ScriptExecutionThread[:50] - Error during the execution of rule 'test helper': An error occured during the script execution: Could not invoke method: org.apache.commons.lang.StringUtils.replace(java.lang.String,java.lang.String,java.lang.String) on instance: null

And here is the code of my script: (Var_TextToSay is String item from .items file - using it as a global variable)

var String tts1
var String tts2

//Google TTS Engine
var String TTS_Engine = "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe --play-and-exit"

//Microsoft SAPI Engine
//var String TTS_URL = ""
//var String TTS_Engine = "C:\\Program Files\\Balabolka\\balabolka_console.exe -n \"Microsoft Server Speech Text to Speech Voice (ru-RU, Elena)\" -t"

tts1 = Var_TextToSay.state
tts2 = tts1.replace(' ', '&20')
executeCommandLine(TTS_Engine+" "+TTS_URL+tts2+".")

Baramykov Alexandr

unread,
Jul 9, 2014, 12:29:38 PM7/9/14
to ope...@googlegroups.com
Hi Alexandr,

Seems like your item Var_TextToSay is not initialized, so have the null value as a state.

Bernhard Haumacher

unread,
Jan 8, 2015, 3:15:32 AM1/8/15
to ope...@googlegroups.com

This is indeed an awkward limitation in the scripts - imports simply do not work here.

Being fallen into the same trap - I think it would be great to mention this on the Script documentation page. In this context, it would be also great to mention the different syntax of qualified names in declarations and expressions x.y.z vs. x::y::z!

It seems that there is another subtle difference between code in rules and stand-alone scripts: A top-level "var" declaration in a rule declares a shared variable that keeps its state between different rule executions, while a top-level variable in a stand-alone script seems to be local to the current execution of the script. This makes it hard to pull out common code from rules to scripts, if it depends on shared variables.
Reply all
Reply to author
Forward
0 new messages