Created
February 22, 2016 23:53
-
-
Save rheinardkorf/faf652ef9dba66b2e555 to your computer and use it in GitHub Desktop.
Chrome Tabs to Markdown - Ruby script to grab all open Chrome tabs and present the Markdown links and reference table. Works extra nice as a TextExpander snippet!!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/ruby | |
# A different implementation to Brett Terpstra's Markdown Service Tools: "md - Links - Chrome Tabs" | |
# This version looks at all tabs across all open Chrome windows. | |
# This version produces output as a list of links as well as a reference table including title tags. | |
# $KCODE = 'u' | |
UNICODE_COMPETENT = ((RUBY_VERSION)[0..2].to_f > 1.8) | |
unless UNICODE_COMPETENT # lower than 1.9 | |
class String | |
def utf8_length | |
i = 0 | |
i = self.scan(/(.)/).length | |
i | |
end | |
end | |
else # 1.9 and above | |
class String | |
alias_method :utf8_length, :length | |
end | |
end | |
def chrome_tab_links | |
# Get all tabs from all Chrome windows. Include title and URL in the list. | |
input = %x{osascript -e 'set titleString to ""' -e 'tell application \"Google Chrome\"' -e 'set window_list to every window # get the windows' -e 'repeat with the_window in window_list # for every window' -e 'set tab_list to every tab in the_window # get the tabs' -e 'repeat with the_tab in tab_list # for every tab' -e 'set the_title to the title of the_tab # grab the title' -e 'set aLink to URL of the_tab' -e 'set titleString to titleString & the_title & "^^^" & aLink & "|||"' -e '#set titleString to titleString & the_title & return # concatenate then all' -e 'end repeat' -e 'end repeat' -e 'end tell'}.chomp | |
items = [] | |
# Split the links | |
list = input.split('|||') | |
list.each {|item| | |
# Split the titles from the URLs | |
parts = item.split('^^^') | |
# Get the name part | |
domain = parts[1].match(/https?:\/\/([^\/]+)/) | |
nameParts = domain[1].split('.') | |
name = case nameParts.length | |
when 1 | |
nameParts[0] | |
when 2 | |
nameParts[0] | |
else nameParts[1] | |
end | |
# Add object to the array for processing | |
items << {'title' => parts[0], 'name' => name, 'link' => parts[1] } | |
} | |
# Strings to contain the links and the reference table | |
theLinks = '' | |
theReferences = '' | |
if items.empty? | |
exit | |
else | |
# Sort array based on domain name component | |
items = items.sort {|a,b| a['name'] <=> b['name']} | |
counter = 0 | |
lastName = '' | |
items.each { |x| | |
# Reset the counter if domain names change | |
if x['name'] != lastName | |
lastName = x['name']; | |
counter = 0 | |
end | |
counter += 1 | |
# Build the links | |
theLinks = "#{theLinks}\n[#{x['title']}][#{x['name']}-#{counter}] " | |
# Build the reference table | |
theReferences = "#{theReferences}\n[#{x['name']}-#{counter}]: #{x['link']} \"#{x['title']}\"" | |
} | |
end | |
# Finally, output the Markdown | |
puts "#{theLinks}\n#{theReferences}" | |
end | |
# Call the method | |
chrome_tab_links |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment