Tap to refresh Widget

Hi all,

I am new to scriptable/widget/coding, basically I am a newbie.

I have developed the following widget to hopefully get a transfer rate from Transferwise between GBP and USD. And what I want to do with the widget is that, whenever I tap the widget, it refresh the widget by “createWidget” again. But even through I set it to “Run Script” it is still redirecting me back to Scriptable.

Is there anyway we can tap and refresh it without redirecting me to somewhere else?

Thanks

let url = "https://api.sandbox.transferwise.tech/v2/quotes/";
let body = {};
//Source Current of the transfer : USD
body.sourceCurrency = "USD";
//Target Current of the transfer : GBP
body.targetCurrency = "GBP";
//change the amount
body.sourceAmount = 60000;
let imgURL = "https://cdn.iconscout.com/icon/free/png-256/transferwise-3089509-2567361.png"
const image = await new Request(imgURL).loadImage()
const resultJSON = await wiseCall(url,body)
let result = resultJSON.paymentOptions[0].targetAmount
console.log(result)
let resultDate = resultJSON.rateTimestamp
console.log(resultDate)

if (config.runsInWidget){
let widget = createWidget()
Script.setWidget(widget)
Script.complete()
}


async function wiseCall(url,body) {
  let request = new Request(url)
request.headers = {"Content-type" : "application/json"}
request.method = "POST"
request.body = JSON.stringify(body)
console.log(body)
let resultJSON = await request.loadJSON()
return resultJSON
}


function createWidget() {
const v = new ListWidget()
v.backgroundColor = new Color("FFFFFF00")

const w = v.addStack()
// w.layoutHorizontally()

const stackImg = w.addStack()
let imageBlock = stackImg.addImage(image)
imageBlock.imageSize = new Size (50,50)
stackImg.topAlignContent()

// Old stack to an empty line between logo
let stackSpace = w.addStack().addText("  ")

//New stack to contain the text
const stackVert = w.addStack()
stackVert.layoutVertically()

const stack = stackVert.addStack()
stack.addText("USD 60,000 = GBP ").textColor = Color.white()
let resultText = stack.addText(String(result))
resultText.textColor = Color.green()
stack.addSpacer()

// Time format 2021-06-27T21:56:37Z

const df = new DateFormatter()
df.useShortDateStyle()
df.useShortTimeStyle()
resultDate = new Date(resultDate)
log(df.string(resultDate))


const stackDate = stackVert.addStack()
stackDate.addText("Updated : "+df.string(resultDate)).textColor = Color.white()
stackDate.addSpacer()
return v
}

Unfortunately not, the current implementation of widgets in iOS requires all apps to launch upon interaction of its widget.

The exception of this is Apple’s own apps, namely Shortcuts. Hopefully this limitation changes next year in iOS 16.

ok thanks! Can we leverage shortcut to do this?

Only Scriptable can modify a Scriptable widget.

I think the Shortcuts point was probably more that Shortcuts can act to do something without opening the app (much like a share sheet extension or third-party software keyboard).

Scriptable has a “Refresh All Widgets” action in Shortcuts. Running this will launch the Scriptable app though.

However, what you can do is use this with @supermamon ’s SpringBoard shortcut to refresh your Scriptable widgets then go back to the Home Screen. This shortcut can be run when you tap the Scriptable widget (by v.url = “shortcuts://run-shortcut?name=[SHORTCUT-NAME]”). (This will open the Scriptable app, then redirect to Shortcuts, then go back to the Home Screen) A slightly more streamline process is running the shortcut from a Shortcuts widget, which just opens Scriptable then closes it.

2 Likes

thanks all! I guess the only real solution is to hope Apple to allow this in the coming iOS then…what ashamed!

Wow! This worked!

(please ignore the anime wallpaper, I like demon slayer)

1 Like