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

**URL:** <https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531>\
**Category:** Ignition\
**Created:** [June 11, 2020, 5:02am UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531 "2020-06-11T05:02:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdamairq](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mdamairq](https://forum.inductiveautomation.com/u/mdamairq)\
**Post date:** [June 11, 2020, 5:02am UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/1 "2020-06-11T05:02:03Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/6/606a459271119782ed06550637c641622a9342c4.png)  
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.

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [June 11, 2020, 2:04pm UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/2 "2020-06-11T14:04:32Z")

</div>

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

```auto
# 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.

---

<div class="post-metadata">

**Author:** ![mdamairq](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mdamairq](https://forum.inductiveautomation.com/u/mdamairq)\
**Post date:** [June 11, 2020, 2:43pm UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/3 "2020-06-11T14:43:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mdamairq](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mdamairq](https://forum.inductiveautomation.com/u/mdamairq)\
**Post date:** [June 11, 2020, 2:53pm UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/4 "2020-06-11T14:53:51Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/0/0ccbc6f559b2a64f08eb785d1efca3f3d343a91d.png)

---

<div class="post-metadata">

**Author:** ![Stevenson.Yuan](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Stevenson.Yuan](https://forum.inductiveautomation.com/u/Stevenson.Yuan)\
**Post date:** [June 11, 2020, 2:56pm UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/5 "2020-06-11T14:56:31Z")

</div>

writeblocking is expecting a list object type, not a string

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

```

see: [https://docs.inductiveautomation.com/display/DOC80/system.tag.writeBlocking](https://docs.inductiveautomation.com/display/DOC80/system.tag.writeBlocking)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [June 11, 2020, 2:59pm UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/6 "2020-06-11T14:59:16Z")

</div>

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:

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

```

@Stevenson.Yuan beat me to it. 🙂

---

<div class="post-metadata">

**Author:** ![mdamairq](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mdamairq](https://forum.inductiveautomation.com/u/mdamairq)\
**Post date:** [June 11, 2020, 3:06pm UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/7 "2020-06-11T15:06:47Z")

</div>

Thanks, life got easier 🙂

---

<div class="post-metadata">

**Author:** ![mdamairq](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mdamairq](https://forum.inductiveautomation.com/u/mdamairq)\
**Post date:** [June 11, 2020, 3:49pm UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/8 "2020-06-11T15:49:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Stevenson.Yuan](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Stevenson.Yuan](https://forum.inductiveautomation.com/u/Stevenson.Yuan)\
**Post date:** [June 11, 2020, 3:52pm UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/9 "2020-06-11T15:52:47Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [June 11, 2020, 3:53pm UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/10 "2020-06-11T15:53:55Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/a/a103b769c17514b227beb049fab643badd6cf896.png)

@Stevenson.Yuan beat me to it too!

---

<div class="post-metadata">

**Author:** ![mdamairq](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mdamairq](https://forum.inductiveautomation.com/u/mdamairq)\
**Post date:** [June 11, 2020, 4:15pm UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/11 "2020-06-11T16:15:18Z")

</div>

Thank you so much 🙂

---

<div class="post-metadata">

**Author:** ![mdamairq](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mdamairq](https://forum.inductiveautomation.com/u/mdamairq)\
**Post date:** [June 11, 2020, 4:16pm UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/12 "2020-06-11T16:16:04Z")

</div>

Thanks again

---

<div class="post-metadata">

**Author:** ![mdamairq](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mdamairq](https://forum.inductiveautomation.com/u/mdamairq)\
**Post date:** [June 15, 2020, 4:53am UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/13 "2020-06-15T04:53:19Z")

</div>

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).

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/3/38d742c7bdd1ecec25dad379670618a6d628f7dc.png)  
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

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 15, 2020, 5:25am UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/14 "2020-06-15T05:25:39Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![mdamairq](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mdamairq](https://forum.inductiveautomation.com/u/mdamairq)\
**Post date:** [June 15, 2020, 5:31am UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/15 "2020-06-15T05:31:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 15, 2020, 5:33am UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/16 "2020-06-15T05:33:26Z")

</div>

Does this work?

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

```

---

<div class="post-metadata">

**Author:** ![mdamairq](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mdamairq](https://forum.inductiveautomation.com/u/mdamairq)\
**Post date:** [June 15, 2020, 5:39am UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/17 "2020-06-15T05:39:46Z")

</div>

Thank you so much. It is working 😀 👏 👏

---

<div class="post-metadata">

**Author:** ![mdamairq](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mdamairq](https://forum.inductiveautomation.com/u/mdamairq)\
**Post date:** [June 17, 2020, 2:28am UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/18 "2020-06-17T02:28:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 17, 2020, 2:43am UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/19 "2020-06-17T02:43:04Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 17, 2020, 2:53am UTC](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531/21 "2020-06-17T02:53:15Z")

</div>

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

```auto
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

[Next page](https://forum.inductiveautomation.com/t/need-help-with-script-to-read-component-button-when-click-and-placing-it-in-tag-is-there-any-possibility/35531.md?page=2)
