Need help with script to read component( button) when click and placing it in tag. Is there any possibility


Object name means button which is clicked.
Iam new to ignition and can’t find a way to script step 1 shown in the image.

Something like this in the button’s mouse event handler would write the object name to a tag:

# Write the name of clicked object to tag.
system.tag.writeBlocking('[tagProviderName]FolderPathIfAny/object_name', event.source.name)

However, based on what you posted I’m not sure why you’d want to write it to a tag. It looks like all you really need is to supply the object name as a parameter to your global script.

1 Like

Exactly, I have to use the object name as parameter to use in global script. Thanks for reply, will try it and update.

Getting this 2 errors
1: raceback (most recent call last):
File “event:mouseClicked”, line 1, in
java.lang.ClassCastException: java.lang.ClassCastException: Cannot coerce value ‘[tagProviderName]FolderPathIfAny/object_name’ into type: interface java.util.List

caused by ClassCastException: Cannot coerce value '[tagProviderName]FolderPathIfAny/object_name' into type: interface java.util.List

Ignition v8.0.12 (b2020042115)
Java: Azul Systems, Inc. 11.0.6
2.

writeblocking is expecting a list object type, not a string

system.tag.writeBlocking(['[tagProviderName]FolderPathIfAny/object_name'], [event.source.name])

see: https://docs.inductiveautomation.com/display/DOC80/system.tag.writeBlocking

2 Likes

WirteBlocking uses lists in it’s parameters, even if there’s just one item in a list. Lists are surrounded by brackets, so you should have:

system.tag.writeBlocking(['[tagProviderName]FolderPathIfAny/object_name'], [event.source.name])

@Stevenson.Yuan beat me to it. :slight_smile:

3 Likes

Thanks, life got easier :slightly_smiling_face:

I am using tag data type as short and I think it is wrong if i wanna write the name of clicked object to tag. Can u help me with this.
Thanks

So event.source.name will always be a string. Easiest is to make the tag a string type.

If all of your object names are going to be numeric, you can try casting the name into an int then writing that to the tag:

objname = event.source.name
try:
	iObjName = int(objname)
	system.tag.writeBlocking(['[tagProviderName]FolderPathIfAny/object_name'],[iObjName])
except:
	print "objectname is not a number!"

2 Likes

You’ll need a string tag if you want to write the name (string) to a tag.

@Stevenson.Yuan beat me to it too!

1 Like

Thank you so much :slight_smile:

Thanks again

Good morning Stevenson and witman,
In step 1 I was able to copy the the component name(file1) to the tag named as button. As seen in figure below the name file 1 is stored in tag (button).


I need some help with step 2 in which i want to open file 1 stored in my computer using this same tag. i used this code
system.tag.writeBlocking([’[default]SIM/B3/button’], [event.source.name])
#fileopen
#def button():
system.util.execute([‘notepad.exe’,‘c:\users\losto\desktop\’,button,’.txt’])

For example, if tag (button)= file 1 it should open file 1.txt document stored in my computer. however I am unable to define parameter button. can you please help me with this.
Thanks

I may be missing something, but you don’t need to write anything to a tag, you just want to open a text file on your desktop with the filename the same name as your button component name?

For that, you just need to use this on the actionPerformed event handler of the button:

componentName = event.source.name
system.util.execute(['notepad.exe', 'c:\\users\\losto\\desktop\\%s.txt' % componentName])

Also, if this client is run on any other computer without the ‘losto’ user, then this won’t work. Consider replacing c:\\users\\losts\\desktop with %UserProfile%\Desktop to get the current logged in user’s desktop. Of course, they would also need to have your text files on there… you’re really better off storing these on a fileshare.

Also note, you need to use double backslashes. A backslash in Python is a special character and needs to be escaped with a second backslash. Alternatively, you can also use single forward slashes.

1 Like

Thanks, this is another and easiest way of making it work. However I am looking out for something that works by storing value in tag and will be grateful if I find a help to do so.

Does this work?

componentName = event.source.name
system.tag.writeBlocking(['[default]SIM/B3/button'], [componentName])
system.util.execute(['notepad.exe', 'c:\\users\\losto\\desktop\\%s.txt' % componentName])
1 Like

Thank you so much. It is working :grinning: :clap: :clap:

hello everyone,
Can we use this script as global script. For example, I am using 100 buttons in my project and want to use the same script and action for all this button. The action is copying component name to tag value and opening the text file same as the component name. So is there any way to make global script which enable me script all button at once rather than script all the buttons separately. Is it possible via script console.

Thanks

Sure you can. Just pass in the event as an argument to the function. It’s not possible via the script console unless you manually punch in the name of the button (there is no event in the script console)

1 Like

Sorry, I had to scoot off before I could add any code.
Example below. Add to a script library in your project / the global scripting project

def openTextFile(event):
	componentName = event.source.name
	system.tag.writeBlocking(['[default]SIM/B3/button'], [componentName])
	system.util.execute(['notepad.exe', 'c:\\users\\losto\\desktop\\%s.txt' % componentName])

When you call it just pass in the event object: yourLibrary.openTextFile(event)
Not sure how calling global/project scripts has changed in v8 so you will need to check that

1 Like