Repository Public
1-- SPB Git (iOS)2-- File: scripts/portal_run.applescript3-- Description: Runs one JavaScript file in the Safari tab where the owner is already signed in to the Apple4-- Developer portal or App Store Connect, and returns what the file's main() returns.5-- It opens the page in a background tab when none is open, checks the origin before running6-- anything, and stops on a sign-in page. It never reads cookies, passwords, 2FA codes or keys,7-- never types or clicks, and needs "Allow JavaScript from Apple Events" (Safari → Develop).8-- usage: osascript portal_run.applescript <file.js> <host> <path-prefix> <open-url>9--10-- Author: Simon-Pierre Boucher11-- Contact: contact@spboucher.ai12on run argv13 if (count of argv) < 4 then return "USAGE: file.js host path-prefix open-url"14 set js to read (POSIX file (item 1 of argv)) as «class utf8»15 set wantedHost to item 2 of argv16 set wantedPath to item 3 of argv17 set openURL to item 4 of argv18 tell application "Safari"19 if (count of windows) is 0 then make new document20 set target to missing value21 repeat with w in windows22 repeat with t in tabs of w23 try24 if (URL of t) starts with ("https://" & wantedHost & wantedPath) then25 set target to t26 exit repeat27 end if28 end try29 end repeat30 if target is not missing value then exit repeat31 end repeat32 if target is missing value then33 -- Background tab: Safari is not activated, the owner's current tab keeps the focus.34 tell front window to set target to make new tab with properties {URL:openURL}35 repeat 40 times36 delay 0.537 try38 if (do JavaScript "document.readyState" in target) is "complete" then exit repeat39 end try40 end repeat41 delay 242 end if43 -- Origin check on the page itself, not on the tab title.44 set origin to do JavaScript "location.protocol + '//' + location.host" in target45 if origin is not ("https://" & wantedHost) then return "STOP: unexpected origin " & origin & " (sign-in or 2FA needed? sign in in Safari, then run again)"46 set hasPassword to do JavaScript "String(document.querySelectorAll('input[type=password]').length)" in target47 if hasPassword is not "0" then return "STOP: sign-in form on screen; the owner must sign in, then run again"48 do JavaScript "window.__spk='PENDING'; (async()=>{try{" & js & "; window.__spk=String(await main())}catch(e){window.__spk='ERR '+e}})();" in target49 repeat 120 times50 delay 0.551 set r to do JavaScript "window.__spk" in target52 if r is not "PENDING" then return r53 end repeat54 return "TIMEOUT after 60 s"55 end tell56end run57