Last active
February 23, 2016 13:30
-
-
Save rheinardkorf/0a33d577c5fe3ed9c6e0 to your computer and use it in GitHub Desktop.
Chrome Tabs to Markdown - Redo of https://gist.github.com/rheinardkorf/faf652ef9dba66b2e555, but this time using Swift. Again, best used 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/swift | |
// @author: Rheinard Korf | |
import Foundation | |
// We need this struct to make life easier | |
struct TabItem { | |
var title: String | |
var name: String | |
var link: String | |
} | |
// Yup, no easy shell method and no =~, gotta do it the hard way. Yay NSTask. | |
func shell(arguments: [String]) -> String | |
{ | |
let task = NSTask() | |
task.launchPath = "/usr/bin/env" | |
task.arguments = arguments | |
let pipe = NSPipe() | |
task.standardOutput = pipe | |
task.launch() | |
let data = pipe.fileHandleForReading.readDataToEndOfFile() | |
let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)! as String | |
return output | |
} | |
// A little helper method, because its crazy enough already to try and use AppleScript in Swift | |
func appleScriptToArguments( script: String ) -> [String] | |
{ | |
var components: [String] = ["osascript"] | |
let parts = script.componentsSeparatedByString("\n") | |
for part in parts { | |
components.append("-e") | |
components.append(part.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())) | |
} | |
return components | |
} | |
func chromeTabLinks() { | |
// Building the command we need to send to shell. This block is AppleScript executed on the shell via `osascript` | |
let input = shell( appleScriptToArguments("set titleString to \"\"" + "\n" + | |
"tell application \"Google Chrome\"" + "\n" + | |
"set window_list to every window # get the windows" + "\n" + | |
"repeat with the_window in window_list # for every window" + "\n" + | |
"set tab_list to every tab in the_window # get the tabs" + "\n" + | |
"repeat with the_tab in tab_list # for every tab" + "\n" + | |
"set the_title to the title of the_tab # grab the title" + "\n" + | |
"set aLink to URL of the_tab" + "\n" + | |
"set titleString to titleString & the_title & \"^^^\" & aLink & \"|||\"" + "\n" + | |
"end repeat" + "\n" + | |
"end repeat" + "\n" + | |
"end tell" | |
) | |
) | |
// This will be all the tabs in a nice array | |
var items: [TabItem] = [] | |
// Separate the items using the ||| that was added in the AppleScript | |
let list = input.componentsSeparatedByString("|||") | |
for item in list { | |
// Now separate title from url | |
let parts = item.componentsSeparatedByString("^^^") | |
if parts.count < 2 { | |
break | |
} | |
// Some Swift regex to get the domain name (dots replaced with dashes). These will be our Markdown references. | |
let range = parts[1].rangeOfString("https?://([^/]+)", options: .RegularExpressionSearch) | |
var name = parts[1][range!] | |
let regex = try! NSRegularExpression(pattern: "https?://", options: .CaseInsensitive) | |
name = regex.stringByReplacingMatchesInString(name, options: [], range: NSRange(0..<name.utf16.count), withTemplate: "") | |
name = name.stringByReplacingOccurrencesOfString(".", withString: "-") | |
// Building a list | |
items.append(TabItem(title:parts[0], name:name, link:parts[1])) | |
} | |
// Sort the list by domain names (keeps it all together nicely) | |
items.sortInPlace({ $0.name < $1.name }) | |
var theLinks: String = "" | |
var theReferences: String = "" | |
if( items.isEmpty ) { | |
return; | |
} else { | |
var lastName = "" | |
var counter = 0 | |
for item in items { | |
// Reset counter if domain name changes | |
if( item.name != lastName ) { | |
lastName = item.name | |
counter = 0 | |
} | |
counter += 1 | |
// Build the links | |
theLinks = "\(theLinks)\n[\(item.title)][\(item.name)-\(counter)] " | |
// Build the reference table | |
theReferences = "\(theReferences)\n[\(item.name)-\(counter)]: \(item.link) \"\(item.title)\"" | |
} | |
// Outputting the link map | |
print("\(theLinks)\n\(theReferences)") | |
} | |
} | |
// [Press Start] | |
chromeTabLinks() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment