|
#!/usr/bin/env osascript |
|
|
|
on parseArguments(arguments) |
|
set argc to count of arguments |
|
if argc is equal to 0 then |
|
error "must provide a URL" |
|
end if |
|
set theUrl to item 1 of arguments |
|
|
|
set destinationFile to missing value |
|
if argc is equal to 2 then |
|
set destinationFile to item 2 of arguments |
|
end if |
|
|
|
set theArgs to { theUrl: theUrl, destinationFile: destinationFile, maxTries:50, initialDelay: 0.35 } |
|
return theArgs |
|
end parseArguments |
|
|
|
on run argv |
|
set args to parseArguments(argv) |
|
|
|
set activeApp to 0 |
|
activeAppState("set") |
|
|
|
set theUrl to args's theUrl |
|
set destinationFile to args's destinationFile |
|
set maxTries to args's maxTries |
|
set initialDelay to args's initialDelay |
|
|
|
|
|
set startTime to currentMillitime() |
|
openChromeWindowWith(theUrl, initialDelay) |
|
lookForTextAndCopy(maxTries) |
|
|
|
log "Found in " & ((my currentMillitime() - startTime) as integer) & " milliseconds." |
|
log "Have something in the clipboard right now -- 🤠 " |
|
|
|
if destinationFile is not missing value then |
|
writeClipboardToFile(destinationFile) |
|
end if |
|
|
|
activeAppState("restore") |
|
end run |
|
|
|
on openChromeWindowWith(thyUrl, delayTime) |
|
log "opening chrome window with " & thyUrl & " then waiting " & delayTime & "s" |
|
tell application "Google Chrome" |
|
make new window |
|
activate |
|
open location thyUrl |
|
delay delayTime |
|
end tell |
|
end openChromeWindowWith |
|
|
|
on lookForTextAndCopy(maxTries) |
|
tell application "System Events" |
|
set pasteLength to 0 |
|
|
|
tell process "Google Chrome" |
|
set i to 0 |
|
|
|
log "Starting look for text to copy with maxTries " & (maxTries) |
|
repeat while (i < my maxTries) |
|
do shell script "printf '' | pbcopy" |
|
|
|
keystroke "a" using command down |
|
delay 0.05 |
|
|
|
keystroke "c" using command down |
|
delay 0.05 -- wait for the text to be copied |
|
|
|
set pasteLength to (do shell script "pbpaste | wc -m") as integer |
|
set pasteLength to pasteLength as integer |
|
if pasteLength > 0 then |
|
log "Found something on try " & i & "..." |
|
exit repeat |
|
end if |
|
set i to i + 1 |
|
end repeat |
|
|
|
if i >= maxTries then |
|
log "❌ Reached the maxTry limit - bailing" |
|
error |
|
end if |
|
end tell |
|
|
|
my closeActiveTab() |
|
end tell |
|
end lookForTextToCopy |
|
|
|
on writeClipboardToFile(destinationFile) |
|
do shell script "touch " & quoted form of destinationFile |
|
set theFile to (do shell script "realpath " & quoted form of destinationFile) |
|
set theFileTmp to (theFile & ".tmp") |
|
|
|
try |
|
do shell script "pbpaste | jq > " & quoted form of theFileTmp |
|
do shell script "rm -f " & (quoted form of theFile) & " &> /dev/null" |
|
do shell script "mv " & (quoted form of theFileTmp) & " " & (quoted form of theFile) |
|
log "\n" & "🟢 outputed to " & destinationFile |
|
on error |
|
do shell script "rm -f " & (quoted form of theFileTmp) & " &> /dev/null" |
|
set message to "❌ Error running the copy script.\n\n❌ You sure you logged in? Network Delay? Check the clipboard and make sure it is JSON. Either way - not happening." |
|
log message |
|
error |
|
end try |
|
end writeClipboardToFile |
|
|
|
on closeActiveTab() |
|
tell application "Google Chrome" |
|
tell front window |
|
close active tab |
|
end tell |
|
end tell |
|
end closeActiveTab |
|
|
|
on currentMillitime() |
|
set timefrom to do shell script "command python3 -c 'import time; print(int(time.time() * 1000))'" |
|
return (timefrom as integer) |
|
end currentMillitime |
|
|
|
on activeAppState(mode) |
|
tell application "System Events" |
|
if (mode is equal to "set") |
|
set my activeApp to name of first application process whose frontmost is true |
|
return |
|
end if |
|
if (mode is equal to "restore") |
|
set frontmost of process (my activeApp) to true |
|
end if |
|
end tell |
|
end activeAppSet |
|
|