Saturday, March 29, 2014

Add a Keyboard Shortcut to Toggle WiFi On and Off

Clicking the WiFi icon in OS X on and off when you're trying to troubleshoot Internet connectivity or AirPort issues can be annoying and disruptive to your workflow. Here's how to add a global keyboard shortcut to do it:

  • Copy the script from here and paste it into a new document in the AppleScript Editor. Save it as ToggleWifi (or whatever you want). Save it with Application as the file format.
  • Since there's no built in way to create a keyboard shortcut to launch an application in the Keyboard system preferences panel, first create a Service in Automator.
  • This post shows screenshots of the steps to create the Service. Open Automator, go to File->New and choose Service from the options that appear. Set "Service receives selected" to 'no input' and leave "in" set to 'any application'. Then search for the Launch Application action in the Actions menu on the left, and drag it to the open area that says "Drag actions or files here to build your workflow". Choose your AppleScript application from the Launch Application dropdown. Click compile then save the Service with a descriptive title, like "Toggle WiFi".
  • Open the Keyboard in System Preferences. Go to the Shorcuts tab, then go to Services and scroll down until you see the one you added in the last step. Make sure it's selected, then to the right of its name, click the grayscale "none" and a button to 'add shorcut' will appear.
  • Enter a unique keyboard shortcut. I chose control-option-command-9 so it wouldn't conflict with any other global or application shortcuts.

The above instructions should take effect immediately, so if your chosen keyboard shortcut doesn't work, try a different one. You might also need to modify a line of the AppleScript as this comment suggests. 

Sunday, March 16, 2014

February #TMIL

Although it's already halfway through March, I'm not quite ready to give up on my resolution to collect a few of the things I've been learning each month as an opportunity to review the material. Don't worry, it's all a healthy exercise in non-competitive professional development. I haven't gone crazy but the risks of losing balance described here are very real, especially when you enjoy what you're doing. OK, on to the good stuff.

Vim

I've been meaning to learn more than the basics of Vim for years and finally dug in last month. Like so many things, getting your environment set up properly in the first place will make things much easier. And repeatable. I began with Vundle, a plug-in manager for Vim. Although the project's founder recently announced he'd be stepping down, it looks like a few people have stepped up to work on it, so it seems solid for now. Grab it here.

To further ease the transition, I downloaded and expanded MacVim then moved the mvim executable into /bin so I could trigger it from the command line. To get around, I knew I needed NERDTree. After adding the bundle to my ~/.vimrc file, (based on the sample one here) then running :BundleInstall in MacVim. Next up was basic navigation. The :NERDTree command opened a sidebar file explorer, and :NERDTreeToggle to show/hide it. While you can launch a project from the terminal with mvim . you can also get there from within MacVim with :NERDTree path/to/myawesomeapp. This post from Thoughbot shows the basics of the vim-rails plugin, such as commands like :Rmodel mymodel to jump right to app/models/mymodel in edit mode. The plugin also adds syntax highlighting (set `syntax on` in vimrc), which is always helpful. There are bundles for Coffeescript and Javascript, too.

To make these more easily available to me at work or wherever, I created a dotfiles repo. Now on whatever machine I'm using, I can install vundle, download my repository and create a symlink to the .vimrc from the user home path, then :BundleInstall.

Here are a couple of other essential vim shortcuts in normal mode:
  • gg=G to tidy up the indentation of a file
  • zt to put the line the cursor is on at the top of the screen
  • :%s/searchterm/replacementterm/g to search and replace 
  • $ or 0 to get to end/beg of line, hjkl for left, down, up, right
  • dd to delete a line
  • gg or 1G jumps to top of file, can use 2G to jump to start of line 2, G to jump to the end of the file
  • :set number to add line
And a few for visual block mode (control-v to get into it):
  • 5j$ will select five lines and select to end of each line
    • replace by hitting r after a selection, then the next key will replace 
    • or delete with d
  • when you have an area of text selected, the letter o jumps to the opposite corner to more quickly resize the selection
  • search and replace in a selection by pressing : to enter a command, then s/searchterm/replaceterm/g and hit enter
  • ctrl-V then G to select column down to end of the file


Ctags

One feature I'd really like to master is jumping around with ctags to explore source code and documentation. Here's are some common commands:
:tag [identifier]Jump to the identifier.
:tagsList the tag stack.
Control-]Jump to the tag under the cursor.
:tselectSelect which tag location to go to for the current tag.
Control-tJump back from the current tag.
:stjump [identifier]Jump to the identifier in a new split window.
(chart source and how to get setup)

This link shows how to set them up in a Rails project (ctags -R . $(bundle list --paths)), then usage is simple as, for example, :tag link_to to show show the source of the link_to helper. The commands in the chart above work as well. control-G will show current file's location and you can use use :!echo % | pbcopy to copy the path to your system clipboard. Pry has some great features for jumping around documentation as well, and I've been using Pry-rescue much more after watching this great video (slides).


Misc.

-Check out Divvy, which has become an essential part of my workflow. It allows you to auto-resize and move windows around your screen using only keyboard shortcuts.
-I completed Metaprogramming in Ruby, a book I'd highly recommend. I'll look at some examples from the book in next month's post after digesting it a bit more.

Thursday, February 6, 2014

January #TMIL

Here's to keeping up with one New Year's resolution!

Documenting and collecting resources has always helped me learn, not only for the occasional refresher but also a reminder of the progress I'm making (or not). For some of us, giving oneself a pat on the back every now and then isn't as forthcoming as it should be. Amidst the countless blog posts, tutorials, books, projects and rabbit holes, I'd like to begin collecting them a bit better. Just kidding, I'm kind of an information pack rat as anyone who's seen the multiple browsers on my computer, each with multiple windows containing dozens of tab, can attest. I'd like to start sharing more as a way to help others and help keep myself organized and focused. With that, here's the first This Month I Learned.

Ruby and Rails bits:
Route globbing and wildcards in Rails
-A colleague recently pointed out this feature of Rails which I hadn't used. By including a wildcard in your route, you can grab the matched value as a param. Here's one of the examples from the RailsGuide:
get 'books/*section/:title', to: 'books#show'
would match books/some/section/last-words-a-memoir with params[:section] equals 'some/section', and params[:title] equals 'last-words-a-memoir'.

Cleaner metaprogramming
-Ruby methods defined using define_method show up when you call #methods on an object even though ones defined at runtime with method_missing don't (unless you define a responds_to that basically matches your method_missing). This makes sense - defining it versus rescuing an error with method_missing, though I didn't expect it.

-The Code School Ruby Bits 1 and 2 courses helped me really understand what's going on with class_eval and instance_eval - the exercise of rolling your own DSL was lots of fun. Like many/most things in Ruby, knowing what self is is the key. (Isn't 'is is' fun?) I've also been working through the Metaprogramming Ruby book. One of my broader 2014 goals is to go from (feeling like) an intermediate to advanced Rubyist -- although like most things, I'll likely always choose the beginner's mindset -- and this book seems highly recommended across the board. (Although it had some competition from Ruby Under a Microscope.) Contrasting the use of yield and instance_eval also helped understand both better. This post is a good tl;dr, and the Recipes DSL from this page provides a fuller example.

Non-Ruby misc.
I decided February would be vim month. While I've used the editor of the beast for basic editing, I'm still mostly a n00b when it comes to fluently switching between editing modes, using buffers and registers, knowing the plugin landscape, and other everyday editor tasks. While I'm not about to give up my precious Sublime Text (which has a vintage mode too) just yet, I'm trying to spend more of my time in vim. The main benefit I hope to realize is having a more integrated development environment by switching contexts less and keeping things in the terminal. Also to better follow colleagues' use of it.

These CSS animations were from December but get honorary mention since they were fun to make and I can't think of anything else to include right now. The code is here or in your friendly neighborhood web inspector.

Although most of my work is in Rails these days, I'd like to learn a Javascript framework well this year. While I've started poking around with Backbone via the Railscasts and Code School course, it's too early to tell which one I'll prefer. I've also been seeing a lot about the MEAN stack, not to mention the neverending comparisons of the Javascript MV* options. The solution? As always, learn enough about all of them to know what to choose when the right projects presents itself.