Skip to content

Instantly share code, notes, and snippets.

@marcusps
Last active May 2, 2020 02:26
Show Gist options
  • Save marcusps/a7bf99c699327aa25154 to your computer and use it in GitHub Desktop.
Save marcusps/a7bf99c699327aa25154 to your computer and use it in GitHub Desktop.
Chrome Tabs to Markdown
# coding: utf-8
# Based on https://gist.github.com/netmute/7374150
require 'date'
# Run some AppleScript to fetch open tabs from Chrome
input = %x{osascript -e 'tell application \"Google Chrome\"' -e 'set tabList to every tab of window 1' -e 'set urlList to \"\"' -e 'repeat with aTab in tabList' -e 'set aLink to URL of aTab' -e 'set aTitle to Title of aTab' -e 'set urlList to urlList & aLink & \"$\" & aTitle & ASCII character 10' -e 'end repeat' -e 'return urlList' -e 'end tell' 2> /dev/null }.chomp
d = DateTime.now.strftime("%a %b %d %Y")
puts "\# Chrome tabs for #{d}"
# Format it into markdown
input.split("\n").each do |line|
url, *title = *line.split('$')
title = title.join('$')
title = url if title == ""
puts "[#{title}](#{url})"
puts
end
@TheKnarf
Copy link

Since I'm using the plugin The Great Suspender for Chrome on my mac; I needed to parse those url back into normal urls. So I extended your great script as following:

#!/usr/bin/ruby -w
# Based on: https://gist.github.com/marcusps/a7bf99c699327aa25154
require 'date'
require 'cgi'

# Run some AppleScript to fetch open tabs from Chrome
input = %x{osascript -e 'tell application \"Google Chrome\"' -e 'set tabList to every tab of window 1' -e 'set urlList to \"\"' -e 'repeat with aTab in tabList' -e 'set aLink to URL of aTab' -e 'set aTitle to Title of aTab' -e 'set urlList to urlList & aLink & \"$\" & aTitle & ASCII character 10' -e 'end repeat' -e 'return urlList' -e 'end tell' 2> /dev/null }.chomp

d = DateTime.now.strftime("%a %b %d %Y")
puts "\# Chrome tabs for #{d}"

# Format it into markdown
input.split("\n").each do |line|
  url, *title = *line.split('$')
  title = title.join('$')
  title = url if title == ""
  # If url is suspended using The Great Suspender
  if url.start_with?("chrome-extension://klbibkeccnjlkjkiokjodocebajanakg")
    url_parsed = CGI::parse(url)
    url = url_parsed['uri'][0]
  end
  puts "[#{title}](#{url})"
  puts "\n\n\n"
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment