Skip to content

Instantly share code, notes, and snippets.

@nicoandmee
Created March 1, 2024 02:26
Show Gist options
  • Save nicoandmee/ecf90ef19b17ed9b2e22a6e0e683d11e to your computer and use it in GitHub Desktop.
Save nicoandmee/ecf90ef19b17ed9b2e22a6e0e683d11e to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
# https://chat.openai.com/share/274e189a-9252-43e2-9ffa-a20f0fa096f0
tempfile=$(mktemp)
current_date=$(date +%Y-%m-%d)
# Run the AppleScript with the temporary file path, and in the background
osascript -e '
on fetchTabsFromChrome(tempFilePath)
tell application "Google Chrome"
set outputBuffer to ""
set tabCounter to 0
repeat with w from 1 to (count of windows)
set currentWindow to window w
repeat with t from 1 to (count of tabs of currentWindow)
set currentTab to tab t of currentWindow
set tabUrl to URL of currentTab
set tabTitle to title of currentTab
set outputBuffer to outputBuffer & tabTitle & "\n" & tabUrl & "\n"
set tabCounter to tabCounter + 1
if tabCounter ≥ 50 then
do shell script "echo " & quoted form of outputBuffer & " >> " & quoted form of POSIX path of tempFilePath
set outputBuffer to ""
set tabCounter to 0
end if
end repeat
end repeat
if outputBuffer ≠ "" then
do shell script "echo " & quoted form of outputBuffer & " >> " & quoted form of POSIX path of tempFilePath
end if
end tell
end fetchTabsFromChrome
on fetchTabsFromSafari(tempFilePath)
tell application "Safari Technology Preview"
set outputBuffer to ""
set tabCounter to 1
set links to ""
repeat with t in every tab in front window
set outputBuffer to outputBuffer & linefeed & the name of t & linefeed & the URL of t & linefeed
set tabCounter to tabCounter + 1
do shell script "echo " & quoted form of outputBuffer & " >> " & quoted form of POSIX path of tempFilePath
set outputBuffer to ""
if outputBuffer ≠ "" then
do shell script "echo " & quoted form of outputBuffer & " >> " & quoted form of POSIX path of tempFilePath
end if
end repeat
end tell
end fetchTabsFromSafari
on run argv
set tempFilePath to item 1 of argv
my fetchTabsFromChrome(tempFilePath)
my fetchTabsFromSafari(tempFilePath)
set endMarker to "EOF"
do shell script "echo " & quoted form of endMarker & " >> " & quoted form of POSIX path of tempFilePath
end run
' "$tempfile" &
applescript_pid=$!
# Initialize a variable to track whether the next line is a URL or Title
expecting_url=false
current_title=""
first_output=true
tail -f $tempfile | while IFS= read -r line; do
if [[ "$line" == "EOF" ]]; then
pkill -P $$ tail
break
fi
# skip empty lines
if [[ -z "$line" ]]; then
continue
fi
if $expecting_url; then
# The current line is a URL, so print the title and URL in markdown format
if $first_output; then
# Output the date heading before printing the first link
echo "## $current_date"
first_output=false
fi
echo "- [$current_title]($line)"
expecting_url=false
else
# The current line is a title, save it and expect the next line to be a URL
current_title=$line
expecting_url=true
fi
done
# Wait for the AppleScript to finish running
wait $applescript_pid
# Copy the output to the clipboard
pbcopy <$tempfile
# Clean up the temporary file
rm -f $tempfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment