r/Scriptable • • Nov 03 '21

Solved Help with setting an image as widget background

Not much of a coder and this is referring to the Daily Logs V2 widget (created by u/randomo_redditor). I’m trying to set a background image for the widget without being prompted to select it from my camera roll every time I run it. Is this possible and if so what’s the code I need? Here is the area of the code that I think controls this: https://imgur.com/a/3BwVqsY .

Thanks in advance

P.S (Lines 182-184 is me trying something with no luck, given a ‘Cannot parse response to an image’ error)

3 Upvotes

42 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Nov 03 '21 edited Nov 03 '21

Fixed it! Turns out it wasn’t actually in that part of the code. Just change this section to:

if (USE_BACKGROUND_IMAGE == false) {
  const bgColor = new LinearGradient();
  bgColor.colors = [new Color("#29323c"), new Color("#1c1c1c")];
  bgColor.locations = [0.0, 1.0];
  widget.backgroundGradient = bgColor;
  widget.setPadding(PADDING, PADDING, PADDING, PADDING);
};   

Also, this is using the code without the changes you added (I forgot to add them). Wait…didn’t I do exactly what you did

1

u/dpopov44 Nov 03 '21

Yes LOL, did it stop asking u to pick a picture u wanted tho after u ran it from the widget?

1

u/[deleted] Nov 03 '21 edited Nov 03 '21

I really thought I figured it out :( Nah it didn’t, if you don’t click on the widget it’s fine, but, to run it I guess you’d need to and then it goes through the whole thing again. So the crux of the issue is getting it to make exist == true. Edit: And I don’t see how we’d do that because by running it, by default it will run in app not widget so will skip over those parts

1

u/dpopov44 Nov 03 '21

Yea Okok, it’s just annoying to pick the pic every time I wanna update the log. According to the comments in the code, it should stop asking u for u to choose right (if u run it from the widget)?

2

u/[deleted] Nov 03 '21

See my edit

2

u/[deleted] Nov 04 '21 edited Nov 04 '21

I just spent all this time making an alert that asks if you want to change it, only to realise if you press no, the background will not stay as it was because it hasn’t been saved in the first place. I wanna give up

1

u/dpopov44 Nov 04 '21

LOL ohh nooo

2

u/[deleted] Nov 04 '21

Finally done it man here ya go:

else if (config.runsInApp && !args.widgetParameter){
    const bcg = new Alert();
    bcg.title = "Change background?";
    bcg.addTextField("Yes or No?");
    await bcg.present();
    if (bcg.textFieldValue(0) == "No") {
    widget.backgroundImage = files.readImage(path)
  } else {
    const img = await Photos.fromLibrary();
    widget.backgroundImage = img;
    files.writeImage(path, img);
  }    

Replace the last else if in the USE_BACKGROUND_IMAGE if statement with this

1

u/dpopov44 Nov 04 '21

Where should I add it?

1

u/[deleted] Nov 04 '21

Here. Just make sure no curly brace gets left out

1

u/dpopov44 Nov 04 '21

Got it, nice work around!

2

u/[deleted] Nov 04 '21 edited Nov 04 '21

My self confidence really was like 📈📉📉📉📈📉📉📈.

2

u/[deleted] Nov 04 '21

Don’t forget to change flair to solved :)

1

u/Spxrkeyy Jul 25 '22

Mind sending the full code, I’m having the same struggle for days now.

1

u/[deleted] Jul 25 '22

I must’ve deleted it, mind sending me yours so I can make the changes? I tried the github link to the code above but it isn’t working

2

u/Spxrkeyy Jul 26 '22

I fixed it myself, i just copied you’re code in the last row, thanks for you’re fast help.

→ More replies

1

u/[deleted] Nov 04 '21

Does it work…I did check it multiple times :|