System.util.execute error

Hello,

I am trying to script a check box to find the correct text file using system.util.execute. I have two errors. Here is the code I have for the ‘property change’ event handler.

val = event.source.parent.getComponent(‘StationType_Lbl’).text
system.util.execute(‘C:\Printing Templates’’ + val + ‘.txt’)

If I don’t have the second ’ after Templates\ is throws the error ‘no viable alternate at input’ like it doesn’t close the end of the string.

The second is when I run the above code it gives this error:
Traceback (most recent call last):
File “event:propertyChange”, line 13, in
TypeError: execute(): 1st arg can’t be coerced to String[]

Thank you for any help that can be provided.

What is the content of val?

How’s this work

val = event.source.parent.getComponent(‘StationType_Lbl’).text
execute_string = "C:\Printing Templates%s.txt"%(val)
system.util.execute(execute_string)

Can you show your full script? You are just expecting system.util.execute to open the text file correct?

You have a few issues here.

  1. The “\” character is the escape character in python and jython by extension
  2. system.util.execute() expects a list of strings, where the first string is the command, and the following strings are any arguments

As can be seen in the example found in the manual your code will need to look something like this:

val = event.source.parent.getComponent('Station Type_Lbl').text
system.util.execute(['notepad.exe','C:\\Printing Templates' + val + '.txt'])

That can be made a bit more pythonic if you use string substitution. Like so:

val = event.source.parent.getComponent('Station Type_Lbl').text
system.util.execute(['notepad.exe','C:\\Printing Template%s.txt' % (val)])
2 Likes

Thank you for the reply, the value of ‘val’ is the type of station the user is currently on. There is a corresponding text file with it in a folder, the text file has zebra printing language code in it (Model, newDate, Printer1IP are all variables inside the zpl code).

Here is the full script with your code input into it.

Count = 0

if event.source.selected is True:
Count = Count + 1
if event.source.parent.getComponent(‘PrinterAActive’).selected is True and event.source.selected is True:
if Count == 1:
Count = Count + 1
Model = event.source.parent.getComponent(‘Dropdown’).selectedStringValue
date = system.date.now()
newDate = system.date.format(date, “MM/dd/yyyy HH:mm:ss”)
Printer1IP = event.source.parent.getComponent(‘Printer1IP’).text
val = event.source.parent.getComponent(‘StationType_Lbl’).text
execute_string = “C:\Printing Templates%s.txt”%(val)
system.util.execute(execute_string)

		if Count != 1:
			Count = 0
			event.source.selected = False

else:
Count = 0

Use triple backticks at the beginning and end of your code to format it

```

like this

```

Awesome, thank you so much this is exactly what I wanted. Thank you also @bkarabinchak.psi for your help as well.