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

2

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

If you run the script in the app, it will make you select a photo. After that, once you set your widget to run this script on your home screen, it shouldn’t ask; it will use the one you last selected in the app. I’ve actually used this bit of code it another script and it works fine if you do as above. No need to change anything

2

u/dpopov44 Nov 03 '21

Thanks for the reply, it still asks me select a picture even when I run it on the Home Screen.

1

u/[deleted] Nov 03 '21

Oh dear that’s rather weird! I’ll take a look at mine and compare to try figure out the issue. Mind you I’m not experienced either though I have a bit of background knowledge, dunno if it’ll help though

1

u/dpopov44 Nov 03 '21

Appreciate it thanks

1

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

If you change it to this does it work? I’d assume you’d have to save the image you want to files and then put the name of it in the speech marks. For credit purposes I’m really sorry I don’t know who made it. I changed the widget name too so can’t figure out from there :(

(I dunno how to make the code block work)

  • BACKGROUND DISPLAY
  • ================== */

// If it's an image background, display it. if (imageBackground) {

// Determine if our image exists and when it was saved. const path = files.joinPath(files.documentsDirectory(), "weather-cal-image") const exists = files.fileExists(path)

// If it exists and an update isn't forced, use the cache. if (exists && (config.runsInWidget || !forceImageUpdate)) { widget.backgroundImage = files.readImage(path)

// If it's missing when running in the widget, use a gray background. } else if (!exists && config.runsInWidget) { widget.backgroundColor = Color.gray()

// But if we're running in app, prompt the user for the image. } else { const img = await Photos.fromLibrary() widget.backgroundImage = img files.writeImage(path, img) }

// If it's not an image background, show the gradient. } else { let gradient = new LinearGradient() let gradientSettings = await setupGradient()

gradient.colors = gradientSettings.color() gradient.locations = gradientSettings.position()

widget.backgroundGradient = gradient }

// Finish the widget and show a preview. Script.setWidget(widget) if (widgetPreview == "small") { widget.presentSmall() } else if (widgetPreview == "medium") { widget.presentMedium() } else if (widgetPreview == "large") { widget.presentLarge() } Script.complete()

2

u/Normal-Tangerine8609 Nov 03 '21

Just so you know for the future, code and code blocks are created with the backtick (`). If your on a Apple phone it can be created by holding on the apostrophe and sliding left.

Inline Code

This will `make` some inline code. -> This will make some inline code.

Code Blocks

```

console.log("Hello, world!)

```

console.log("Hello, world!)

For code blocks you also have to make sure you put a new line above and below the ticks to separate it from the other text. Reddit uses basic markdown syntax for comments. To see more check out https://www.reddit.com/wiki/markdown.

2

u/[deleted] Nov 03 '21

Ah I was missing the lines above and below. Thanks!

1

u/dpopov44 Nov 03 '21

When I copy the code exactly it won’t run, says the variable ‘files’ is not declared. The codes that we have are pre much identical tho, the issue could be somewhere else idk

2

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

// Set to true for an image background, false for no image. const imageBackground = true const files = FileManager.local() const forceImageUpdate = false // If it's an image background, display it. if (imageBackground) {

// Determine if our image exists and when it was saved. const path = files.joinPath(files.documentsDirectory(), "BlurMid") const exists = files.fileExists(path)

// If it exists and an update isn't forced, use the cache. if (exists && (config.runsInWidget || !forceImageUpdate)) { widget.backgroundImage = files.readImage(path)

// If it's missing when running in the widget, use a gray background. } else if (!exists && config.runsInWidget) { widget.backgroundColor = Color.gray()

// But if we're running in app, prompt the user for the image. } else { const img = await Photos.fromLibrary() widget.backgroundImage = img files.writeImage(path, img) }

// If it's not an image background, show the gradient. } else { let gradient = new LinearGradient() let gradientSettings = await setupGradient()

gradient.colors = gradientSettings.color() gradient.locations = gradientSettings.position()

widget.backgroundGradient = gradient }

1

u/dpopov44 Nov 03 '21

Parser error :(

2

u/[deleted] Nov 03 '21

Ah sorry mate I really don’t know what to do. Are all the “const”s on new lines and that or has the format been messed up. I’m on mobile. Edit: Like this

2

u/dpopov44 Nov 03 '21

No problem, thanks for trying

→ More replies

1

u/dpopov44 Nov 03 '21

Back to my original code, if it’s keeps asking that means there’s no picture being loaded from cache right?

1

u/[deleted] Nov 03 '21

Yep

1

u/[deleted] Nov 03 '21

Ok imma check the one I tweaked and see if I changed anything. Done, let me know if it works, hopefully it should now!