Text 14 Jul Scripting Chrome and Kaizen

One technique or philosophy that I find very useful is the idea of kaizen: Continous small improvements. I find that looking for small inefficiencies in ones workflow and figuring out how to address them a great pastime, as not only do you make your life that little bit easier, but you tend to learn some neat things in the process.

The bit of friction that inspired this post is something that most people may not frequently run into, but happens a lot to me. In order to explain, I need to briefly digress into my standard working habits.

How I Get Shit Done

As I’m usually working on at least two or three projects in addition to whatever random thing I’m doing just for fun, I find partitioning resources essential. To this end, I use multiple desktops (or Spaces on OS X) with each project having its own desktop. Since virtually everything these days requires a web browser, even if it’s just to look up docs, I’ll have a separate Chrome window for each project. However, sometimes I don’t realize that a little idea is becoming a stand-alone project, and I’ll want to take the tabs I have open in my primary browsing window, split them off to their own window, and move them to another space. This tends to be fairly awkward though, as I have to either manually pull each tab off and painstakingly drag them pack together or create a new window and repeatedly copy URL, switch windows, open a new tab, paste, URL, and switch back. Rather inefficient both ways.

Applescript to the Rescue

Although the documentation for it is somewhat sparse, Google Chrome now supports Applescript, which is not the best language, but has unparalleled integration with OS X. After a bit of experimentation, I was able to write up the following script:

applescript:
tell application "Google Chrome"
  set openTabs to {}
  set openTabsRef to a reference to openTabs
  set w to front window
  repeat with t in every tab of w
    copy URL of t to the end of openTabsRef
  end repeat
  choose from list openTabs with multiple selections allowed
  if the result is not false then
    set tabsToTransfer to the result
    repeat with t in every tab of w
      if tabsToTransfer contains URL of t then
        tell t
          close
        end tell
      end if
    end repeat
    make new window
    set newWindow to front window
    repeat with t in tabsToTransfer
      make new tab at the end of tab of newWindow with properties {URL:t as text}
    end repeat
    close tab 1 of newWindow
  end if
end tell

Simple enough: Running this script gives you a list of URLs from which you can choose the ones you want moved into a new window. Didn’t take me that long to write and now I’ve both banished that little piece of annoyance from my life forever and learned a little more about how to use Applescript.


Design crafted by Prashanth Kamalakanthan. Powered by Tumblr.