4

I am trying to use shell command notify-send -t 2000 "Title" "Message" from groovy in next way "notify-send -t 2000 \"Title\" \"Message\"".execute() and it works perfect. But when i am trying to put exprsession in place of message it seems nothing works. here is broken code below:

def todayDate = new Date()

def title = 'Title'
def message = " Message ${todayDate}"
println(title + message)
"notify-send -t 2000 \"${title}\" \"${message}\"".execute()

can you help me with understanding?

Connected to the target VM, address: '127.0.0.1:40305', transport: 'socket' TitleFri May 06 13:41:43 CEST 2016 groovy.lang.MissingPropertyException: No such property: execute for class: DUMMY at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:51) at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:295) at DUMMY$_closure1.doCall(DUMMY.groovy:1) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90) at org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod.invoke(ClosureMetaMethod.java:81) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1208) at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1111) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1017) at test.notification.run(notification.groovy:28)'

4
  • 1
    Is there an error message?
    – tim_yates
    May 6, 2016 at 9:54
  • 1
    You can make it neater by using slashy strings: /notify-send -t 2000 "${title}" "${message}"/.execute() Also, you don't need to call toString() on message. But these are just decorations, what you say you have should work... Capture the output and show what it says
    – tim_yates
    May 6, 2016 at 9:58
  • Sorry, but there is no output. it looks okay, but without notification window which should contains a text. You can have a look on expected result here picture
    – delovepr
    May 6, 2016 at 10:20
  • @tim_yates it seems groovy expected just one value, becouse if I am printing message, which is consist of one world(example 6May2016) it works ok and presenting notifications, otherwise (two or three words wrapped with ' or ") do nothing. :-(
    – delovepr
    May 6, 2016 at 13:01

3 Answers 3

3

So the problem occurs when you have a space in your parameter.

So for example this will not work:

def command = "send_mail.sh \"Some text\""
command.execute();

send_mail.sh will receive 2 parameters instead of 1.

So to avoid problems split your string by arguments. So something like this:

def command = ["send_mail.sh", "Some text"];
command.execute();

First item (0 index) is a command and then arguments follow.

1

You need to declare the command as a variable beforehand and then execute the command.

You can do it like this:

def todayDate = new Date().toString()
def title = 'Title'
def message = " Message ${todayDate.toString()}".toString()
println(title + message)
def command = "notify-send -t 2000 \"${title}\" \"${message}\""
command.execute()
1

It seems this issue was mostly related to string formatting and misunderstanding "Message 9 May 2016" as one String(argument). For me it works in next way:

def command = new String[3]
command[0] = "notify-send"
command[1] = "Title"
command[2] = "\"Message ${todayDate}.toString()\"".toString()
def process = new ProcessBuilder(command).start()

Anyway, thanks to everyone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.