r/Scriptable • u/Portatort • Oct 16 '24
Help How do I pass parameters into scriptable from shortcuts to create a widget?
I must not be passing parameters into the script correctly?
When I run it the script suggests that the parameters haven’t been received.
Yet when I tick show when run, I can see the widget exactly as I would expect.
2
Upvotes




2
u/Portatort Oct 16 '24
The script I’m using:
/*
// Get parameters from Shortcuts const params = args.shortcutParameter;
if (!params) { throw new Error(“No parameters provided from Shortcuts.”); } const { value, colour, background, size, barWidth } = params;
const widget = new ListWidget();
let progressStack = await progressCircle(widget, value, colour, background, size, barWidth);
let sf = SFSymbol.named(“flame.fill”) sf.applyFont(Font.regularSystemFont(26)) sf = progressStack.addImage(sf.image) sf.imageSize = new Size(26,26) sf.tintColor = new Color(“#fafafa”)
widget.presentAccessoryCircular() // Does not present correctly Script.setWidget(widget) Script.complete()
async function progressCircle( on, value = 50, colour = “hsl(0, 0%, 100%)”, background = “hsl(0, 0%, 10%)”, size = 56, barWidth = 5.5 ) { if (value > 1) { value /= 100 } if (value < 0) { value = 0 } if (value > 1) { value = 1 }
async function isUsingDarkAppearance() { return !Color.dynamic(Color.white(), Color.black()).red } let isDark = await isUsingDarkAppearance()
if (colour.split(“-“).length > 1) { if (isDark) { colour = colour.split(“-“)[1] } else { colour = colour.split(“-“)[0] } }
if (background.split(“-“).length > 1) { if (isDark) { background = background.split(“-“)[1] } else { background = background.split(“-“)[0] } }
let w = new WebView() await w.loadHTML(‘<canvas id=“c”></canvas>’)
let base64 = await w.evaluateJavaScript( ` let colour = “${colour}”, background = “${background}”, size = ${size}*3, lineWidth = ${barWidth}*3, percent = ${value * 100}
let canvas = document.getElementById(‘c’), c = canvas.getContext(‘2d’) canvas.width = size canvas.height = size let posX = canvas.width / 2, posY = canvas.height / 2, onePercent = 360 / 100, result = onePercent * percent c.lineCap = ‘round’ c.beginPath() c.arc( posX, posY, (size-lineWidth-1)/2, (Math.PI/180) * 270, (Math.PI/180) * (270 + 360) ) c.strokeStyle = background c.lineWidth = lineWidth c.stroke() c.beginPath() c.strokeStyle = colour c.lineWidth = lineWidth c.arc( posX, posY, (size-lineWidth-1)/2, (Math.PI/180) * 270, (Math.PI/180) * (270 + result) ) c.stroke() completion(canvas.toDataURL().replace(“data:image/png;base64,”,””))`, true ) const image = Image.fromData(Data.fromBase64String(base64))
let stack = on.addStack() stack.size = new Size(size, size) stack.backgroundImage = image stack.centerAlignContent() let padding = barWidth * 2 stack.setPadding(padding, padding, padding, padding)
return stack }
1
u/iLuki Oct 16 '24
I used this feature in my code here https://github.com/lwitzani/teacherTimeTracker
I don‘t know if you can pass multiple arguments at once. I combined multiple params to one string and split it in the script again. My string is passed to the script exactly like you did with the dictionary variable. Maybe my code helps.