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/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

1

u/[deleted] Nov 03 '21

No worries, I’m trying to see how much I know from this exchange. Evidently, very little. Feels like I’m running it circles almost! You know in the most recent one where there was a parser error, was that after you changed BlurMid to the name of the photo you saved it files? If not, does it work after doing that?

2

u/dpopov44 Nov 03 '21

I changed the name yea, still nothing

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!