How to run a shortcut every 30min

The 5 seconds is for testing aiming for 30 min anyways
If i leave this out.


const SHORTCUTNAME = "Random-Wallpaper-Home";
const BASEURL = "shortcuts://run-shortcut?name=";
Safari.open(BASEURL + encodeURI(SHORTCUTNAME));

The

Console.log(‘test’);

Displays every 5 sec but for some reason if i put in the code above. Every thing goes blank.

"use strict" 
let wv = new WebView();
await wv.loadHTML("");
let js = `
const run = setInterval(runShortCut, 5000);
function runShortCut() {
const SHORTCUTNAME = "Random-Wallpaper-Home";
const BASEURL = "shortcuts://run-shortcut?name=";
Safari.open(BASEURL + encodeURI(SHORTCUTNAME));
Console.log(‘test’)
}
`;

 let result = await wv.evaluateJavaScript(js, true) ;


I don’t think you can run Safari.open in the context of the browser. Safari.open is a scriptable api method. That javascript you send to the browser has to match the browsers API. You can use window.open in the actual browser

There’s a Timer class that you can use instead of setInterval.
The code below worked for me but I haven’t tested for longer periods.
I’m also not sure if you need to keep Scriptable open while this is running. Maybe add an Open App action to your shortcut to go back to Scriptable after the shortcut completes.


const SHORTCUTNAME = 'MyShortcut'

await repeat(5000, 5, () => {
  runShortcut(SHORTCUTNAME)
})

function runShortcut(name) {
  const cb = new CallbackURL('shortcuts://run-shortcut')
  cb.addParameter('name', name)
  cb.open()
}

function repeat(interval, max, func) {
  // interval in milliseconds
  // max is number of repetitions. set to -1 for infinite
  // function to run for each repetition
  return new Promise((resolve, reject) => {
    let repetitions = 0
    const t = new Timer()
    t.timeInterval = interval
    t.repeats = true
    t.schedule(() => {

      repetitions = repetitions + 1

      func()

      // stop the timer of max is reached
      // will never evalues to true if max = -1
      if (repetitions == max) {
        t.invalidate()
        resolve()
      }

    }) // schedule
  }) // Promise
} // function repeat