Any way to differentiate between widget instances of the same script

This is more of a question for @simonbs but if anyone has an idea, do tell.

I’m using @mzeryck’s amazing Invisible Widget script to “pin” static text on the home screen, where the text is widget parameter. But since the background is based on the script’s name, all the scripts are using the same background.

I’m getting around this by passing a JSON string containing an identifier for the widget instance but it would be easier if there is a built-in instance id when using the same script for different widgets.

On the screenshot below, i have 4 widgets, the text you see is the parameter for each one. Without an identifier (top row), both widget can only use the same background.
With an identifier (2nd row), I can choose a different background individually, but it’s cumbersome to enter the parameter as a JSON.

If I’m not wrong, the only way Scriptable knows about multiple widgets is with the parameter you’re already using. I don’t think the app gets to know about individual widget instances.

I think this is more of an issue with the way I wrote my image code. The image name is hard-coded to the script name because it was the easiest way to make it essentially “no setup” - you just run through the steps, and when you’re done, the widget just works.

Probably the easiest thing to do would be what I’ve been doing – just duplicate the script within Scriptable! So have scripts named Invisible-topleft, Invisible-topright, etc. Duplicating a script and changing its name will force it to run through the image cropping steps, and then you’ll have unique scripts to add to your home screen.

Hope that helps!

While that works, I’m working on the idea where I use the same script for multiple widgets, thereby preventing duplication of scripts.
For now, I guess the only way is to differentiate using the parameter.
What I’m doing is appending the id value to the background filename.

Ah, that makes sense. If you haven’t tried it already, I might recommend simplifying your logic so you don’t need JSON. You can use a comma-separated format in the widget parameter, something like t3,text 3, and then in your code use:

let argsArray = args.widgetParameter.toString().split(",")
let widgetID = argsArray[0]
let widgetText = argsArray[1]