Repository Public
1-- Runs one JavaScript file in the Safari tab where the owner is already signed in to the Apple2-- Developer portal and returns what the file's main() returns. It checks the origin before running and3-- never reads a cookie, a password or a 2FA code. usage: osascript apple_dev_setup.applescript file.js host path-prefix open-url4on run argv5 if (count of argv) < 4 then return "USAGE: file.js host path-prefix open-url"6 set js to read (POSIX file (item 1 of argv)) as «class utf8»7 set wantedHost to item 2 of argv8 set wantedPath to item 3 of argv9 set openURL to item 4 of argv10 tell application "Safari"11 if (count of windows) is 0 then make new document12 set target to missing value13 repeat with w in windows14 repeat with t in tabs of w15 try16 if (URL of t) starts with ("https://" & wantedHost & wantedPath) then17 set target to t18 exit repeat19 end if20 end try21 end repeat22 if target is not missing value then exit repeat23 end repeat24 if target is missing value then25 -- Background tab: Safari is not activated, the owner's current tab keeps the focus.26 tell front window to set target to make new tab with properties {URL:openURL}27 repeat 40 times28 delay 0.529 try30 if (do JavaScript "document.readyState" in target) is "complete" then exit repeat31 end try32 end repeat33 delay 234 end if35 -- Origin check on the page itself, not on the tab title.36 set origin to do JavaScript "location.protocol + '//' + location.host" in target37 if origin is not ("https://" & wantedHost) then return "STOP: unexpected origin " & origin & " (sign-in or 2FA needed? sign in in Safari, then run again)"38 set hasPassword to do JavaScript "String(document.querySelectorAll('input[type=password]').length)" in target39 if hasPassword is not "0" then return "STOP: sign-in form on screen; the owner must sign in, then run again"40 do JavaScript "window.__spv='PENDING'; (async()=>{try{" & js & "; window.__spv=String(await main())}catch(e){window.__spv='ERR '+e}})();" in target41 repeat 120 times42 delay 0.543 set r to do JavaScript "window.__spv" in target44 if r is not "PENDING" then return r45 end repeat46 return "TIMEOUT after 60 s"47 end tell48end run49