Skip to content

Instantly share code, notes, and snippets.

@jwenerd
Created January 13, 2024 02:03
Show Gist options
  • Save jwenerd/9275a71bae610b6b5c2e3713e844149b to your computer and use it in GitHub Desktop.
Save jwenerd/9275a71bae610b6b5c2e3713e844149b to your computer and use it in GitHub Desktop.
Copy URL text From Chrome via AppleScript - save JSON

Copy a URL Body by Opening It in Chrome

Usage

osascript ./chrome-copy.scpt URL [FILE_TO_SAVE]

i.e:

osascript ./chrome-copy.scpt https://official-joke-api.appspot.com/random_joke
# random joke json is now in your clipboard

osascript ./chrome-copy.scpt https://catfact.ninja/fact ~/cat_fact.json
# random cat fact is now in your clipboard
# and in ~/cat_fact.json

Explanation

I needed a way quickly to get some JSON for automation that I can only access when authorized behind a service that does not offer an API. So I couldn't wget or curl the URL and mocking the session authorization wasn't an option. Instead I now login to the app in my normal Chrome , then run this script to grab the output and then save it to a file on my computer.

How it works:

  • opens the URL in a new Chrome window
  • waits a little bit
  • copies the body text until there text is in the clipboard
    • if reaches the maxTries limit and no text copied yet; it will exit with error.
  • optionally writes the JSON to a file
    • pipes through jq for pretty formatting
  • close the Chrome window and restores the window you had open
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment