Skip to main content
Participating Frequently
March 20, 2019
Question

Please help me to resolve this

  • March 20, 2019
  • 2 replies
  • 812 views

I try to do :

myBTN = w.add("button");

myBTN.name = "BTNicon";

myBTN.image = ScriptUI.newImage(this.name);

"this.name" should give the name of myBTN ("BTNicon")... no ?

Thank you

This topic has been closed for replies.

2 replies

Sunil Yadav
Legend
March 20, 2019

You should try this as

var w = new Window ("myWindow", "Test");

// For adding image to button you must have a folder where your image is located//

var myFolderContainingImage = 'C:/Users/sunily/Desktop/Feedbacks/';

myBTN = w.add("button");

myBTN.name = "BTNicon";

// Use folder path and extension on your file as well//

myBTN.image = ScriptUI.newImage(myFolderContainingImage+myBTN.name+'.jpg');

Best

Sunil

Participating Frequently
March 20, 2019

Thank you all, I could have been more precise  !

In fact 'BTNicon', is an embed image, previously declared in my script "var BTNicon='...';"

Is there a way to solve my problem, I really need to find the name property of 'myBTN' in : 'myBTN.image = ScriptUI.newImage(this.name),' without using 'myBTN.name' (which otherwise works) ?

Community Expert
March 20, 2019

Send a complete code snippet and also an explanation on why do you need to use this.name itself. I am not clear why you need to use a specific syntax only.

-Manan
Community Expert
March 20, 2019

No "this" will not correspond to the myBTN object. "this" is set to the object on whose context the current code is running. It seems that your code is not within a function hence in this case the this is set to global object. If you call a method of an object then this inside the method is set to  the calling object, for ex inside a event handler this would be the object on which the event has been raised.

Have a look at the following URL for more details and examples

https://codeburst.io/all-about-this-and-new-keywords-in-javascript-38039f71780c

this - JavaScript | MDN

-Manan

-Manan