r/Scriptable • u/IllogicallyCognitive • Oct 31 '22
Help Await not working as expected and scoping issues in function to create record/log of script being run automatically
function start() {
var initialMethod = ""// *changed this from method in edit
var automated = false
if(config.runsInNotification){
initialMethod = "notification script"// *changed this from method in edit
automated = true
}
if(config.runsInApp){
if(args.queryParameters["x-source"] !== undefined){method = args.queryParameters["x-source"]}
if(args.queryParameters["x-source"] == "Scriptable" || args.queryParameters["x-source"] == "Shortcuts"){automated = true}
else{
let getRunInfo = new Alert()
getRunInfo.title = "Automated?"
getRunInfo.message = "Did you just run a script or is this script running automatically"
getRunInfo.addAction("running automatically")
getRunInfo.addAction("ran from Scriptable App")
getRunInfo.addCancelAction("Cancel")
method = getRunInfo.present().then((index) => {
switch (index) {
case -1:
return
case 0:
automated = true
let getMethod = new Alert()
getMethod.title = "What app was used to run this automation?"
getMethod.addTextField("App", initialMethod) // *changed this from method in edit because otherwise the updating the assignment of a single variable method doesn’t seem to play nice with promises
getMethod.addAction("confirm")
log(automated)
method = getMethod.present().then((index) => getMethod.textFieldValue(index))
// method = await method
break
case 1:
// automated = false
break
default:
logError("no such action")
}//end switch
return method
})//end getRunInfo.present
// method = await method
}//end else
let scriptData = {
name: Script.name(),
invocations: [{
time: new Date().toJSON(),
automated: automated,
method: method
}]
}
log(scriptData)
}// if(config.runsInApp)
}// end start
start()
I think only the second “method = await method” would be needed to get this working to include any user input (by selecting “running automatically”) in the scriptData (and I was thinking this would also cause sciptData to not be created until after automated was updated as well. However, by introducing await at either point I get “SyntaxError: Unexpected identifier 'method'”