Insert All Open Safari Tabs into a TaskPaper doc

While researching a particular topic, I often find myself with like thirty Safari tabs open. If I don’t have time to finish what I was working on or need to switch to something else, these open tabs can be really distracting and they consume system resources for no good reason.

I wrote the following script because I wanted to be able to just dump all the tabs in my topmost Safari window into TaskPaper as tasks, so I could close the window, but still keep track of them and find a better time to finish what I was working on. It’s a good way for me to do a literal context switch - save all the working memory to TaskPaper, and move on to the next thread :grinning:

Thanks to @jessegrosjean and @complexpoint for helping me resolve an issue with this script.

The script will add a list of your open tabs as children of the current item in your TaskPaper doc. Each item has a subnote that contains the URL. An example screenshot is below. It seems to work pretty reliably, but if you run into an issue where no content gets added to your document even though tabs are definitely open, just give the focus to Safari and then try the script again.

Chrome seems to use the exact same interface. Just replace the word “Safari” with “Google Chrome” in the second line (I have not tested this)

Here’s the code:

function extractSafariTabs() {
  const safari = Application("Safari");
  const window = safari.windows[0];

  return window.tabs().map(tab => 
  	Object({name: tab.name(), url: tab.url()})
  );
}

function saveTabs(editor, options) {
  'use strict';

  const safariTabs = options.tabList;

  editor.outline.groupUndoAndChanges(function() {
  	const current = editor.selection.endItem;
  
	const items = safariTabs.map(tabEntry => {
		const entry = editor.outline.createItem(`- ${tabEntry.name}`);
	
		const urlNote = editor.outline.createItem(tabEntry.url);
		entry.appendChildren([urlNote]);
	
		return entry;
	});

	current.appendChildren(items);
	editor.setCollapsed(items);
  })

  return true
}

const docs = Application("TaskPaper").documents;

docs[0].evaluate({
  script: saveTabs.toString(),
  withOptions: { 
    tabList: extractSafariTabs(), 
  }
});
4 Likes

Added to Wiki.

@batkins: this script is awesome. Thanks for sharing with the community. I confirm it works perfectly with Chrome as well (by replacing “Safari” with “Chrome”).

I was looking for a way to get this great script to work with Firefox but it’s not possible.

Instead you can use the add-on Export Tabs URLs that takes all the browser tabs and creates a formatted list which then can be copy-pasted into TaskPaper.

With this custom format “- {title}\r\n {url}\r\n” in the add-on settings you get the same result as with the script.

@anselmo and @gustafekeberg, I hope that you also already checked the counterpart of this script. Script to open all urls in browser from TaskPaper document. @batkins did something truly useable.

1 Like