Present webview when run through shortcut

hi, i have a script that logs data to an api that needs authentication. currently that script will,

  • check for a key in keychain
    • if not there, try and login with a webview
  • attempt to log the data
    • if this fails (ie, key expired) it will try and login with a webview
    • attempt to log again with the new key

this works fine if the key is there and valid because it never needs a webview. but as soon as it needs a webview it never opens and the script times out. if i change the shortcut action to be “run in app” it works great but i dont want to open the app for no reason 90% of the time. is there a way to get webview.present to work when a script is called through a shortcut without the app open?

failing that, is there a way i can have the script open the app if it needs to authenticate?

The only way I see is if you move the logic that determines if you need to login into a separate call from shortcuts.
So you call your script once from shortcuts and only get back if you need to login. You then call the script again doing the normal thing and pass the value from earlier to “Run in App” (long press on it to set it to a variable).

thats my absolute fallback, although i would need to use a return value of some kind because the script always needs to return to the shortcut so it can log the data to the health app and do a few other things

Writing to a text file is a good way to share data between Shortcuts and Scriptable. It sounds like one issue is that you never know if the key will be expired until you try it.

I don’t know what kind of key you’re working with; with OAuth you get back an expiration date, for example. If you can determine when the key expires, you could write that data to a text file. Then Shortcuts could read that file and determine whether or not it’s necessary to run the script in Scriptable.

Just a thought, it may not be applicable in your case.

it is unfortunatly not oauth, it is my own server that is acting as a much easier to use interface for the fitbit api. and while i probably could get the expiration of the fitbit key and pass that through the keys also expire if my server goes down (and it has been pretty unstable lately) so there is a good chance that it would fail anyway.

using text file for that is a good idea though, ill try and remember it

1 Like

From your initial post I read that you have some kind of logic to determine whether to log in or not. In the case that you need to log in you open a WebView. Have I got this right?

If I’ve got this right, then instead of opening the WebView you simply return true from your script to shortcuts. Shortcuts then runs the script again, but with the “Run in App” toggle enabled.

If you don’t have to log in then you return false to shortcuts and it runs it again but without the toggle enabled.

In the end you have two “Run Script” actions in your shortcut.

To use the same script you could pass a parameter to the script on the first call so that the script knows that it should only check the login.

In my opinion this is easier than managing files with dates where the dates night not even be accurate to determine if you need to log in.

the current logic is “try and log data, did it work? if no, login and try again”

if(!await log_weight()) {
	await authorise();
	await log_weight();
}

is there a way for the script to tell if it is being run in the app or not? or can i import functions from other scripts and have 3 js files, one for the functions, one for log and one for login and log. im already using the parameter to pass in the data to log and from what i can tell passing in more data there will make things complex

Check out the Scriptable documentation for …

runsInApp.

importModule.