Everything Posts Notes
mikker·about 12 years ago

Rename current TMUX session to current directory's basename

I like having a seperate TMUX session per project I’m currently working on. So a numbered list of sessions quickly becomes unhandy.

tmux rename-session `basename $(pwd)`

ctrl-b s and we get this:

(0) + angular-firmafon: 1 windows (attached)
(1) + blog: 3 windows (attached)
(2) + wallboard: 2 windows

Bonus tip: Put bind ^S switch-client -l in your .tmux.conf. Now ctrl-b ctrl-s toggles between the current and last session.

mikker·about 12 years ago

Open photo GPS location in Apple Maps using ruby

Dr. Drang posted a lengthy article and script to open a given photo’s EXIF GPS location in Apple Maps. He’s using python and a library called “PIL”. I liked the idea but couldn’t, no matter what I tried, get the damn thing to install and what is python anyway? I like ruby! I’m sure there’s an easier way?

Turns out there was.

The rubygem exifr reads EXIF data like a champ, so let’s get it:

$ sudo /usr/bin/gem install exifr

I’m using the absolute path to gem, because we want to end up using this as a system service, and system services use system ruby.

Now, here’s the script. Save it as something like ~/bin/map.rb:

#!/usr/bin/env ruby
begin
  require 'exifr'
rescue LoadError
  require 'rubygems'
  require 'exifr'
end

usage = <<-USAGE
usage: map.rb IMAGE_PATH
USAGE

path = ARGV.shift

if path.nil?
  puts usage
  exit(0)
end

exif = EXIFR::JPEG.new(path)
if coords = exif.gps
  system "open 'http://maps.apple.com/?q=#{coords.latitude},#{coords.longitude}'"
else
  puts "No GPS data for #{path}"
end

Remember to chmod +x it and call it like the Doctor does in a service like this, substituting the path to where you saved the script:

mikker·over 12 years ago

Update Safari from vim with AppleScript

When Livereload is too much and cmd-tab’ing too little, there’s always this AppleScript.

#!/bin/sh
osascript -e 'tell application "Safari"
  set _url to URL of current tab of front window
  set URL of current tab of front window to _url
end tell'

Put it in your PATH, chmod it +x, and map it to what you like in vim:

map ,r :call system("update_safari.sh")

You could even auto-run it when you save the current buffer:

autocmd BufWritePost <buffer> :call system("update_safari.sh")

Replace <buffer> with * to do it in every buffer.

mikker·over 12 years ago

OS X's open with support for STDIN

OS X’s open command is great and I use it many times a day. But several times I’ve stumbled through some command that outputs something only to be reminded once again that open doesn’t support piping of input. Let’s fix that.

#!/bin/sh
# Put a dash at the end of open to use stdin as arguments
# usage: echo "TextEdit" | open -a -
for last; do true; done
if [ "$last" == '-' ]; then
  arg=`cat`
  cmd=`echo $@ | sed -e "s/-$/${arg}/"`
  /usr/bin/open $cmd
else
  /usr/bin/open $@
fi

This add a check to open: If the last argument is a dash (-), then use STDIN as the argument to the original open.

Put it somewhere that’s in your $PATH before /usr/bin is and you’re good!

mikker·over 12 years ago

Get the true mime type of a file in Ruby

I was kind of disappointed to find out that the ruby mime-type gem just looks at the file extension instead of getting the true mime type. This can cause trouble. Say we’re having the user upload files to our webpage - but we only want .mp3s. Without checking the true mime-type the user will be let through if he just renames his file to .mp3.

If you’re lucky enough to be on Unix though, you can get the true mime type by using the shell command file.

true_mime_type = `file --mime -b mp4-renamed-to.mp3`.chomp
  # => video/mp4; charset=binary

There we are.

mikker·over 12 years ago

MobilePay's undocumented URL schemes

I made a request on
Twitter
for url schemes
in the Danish app for easy mobile money transfers
MobilePay.
Turns out it already had
some.

mobilepay://send?amount=100&phone=88888888
mobilepay://request?amount=100&phone=88888888

Perfect! This makes for perfect Launch Center
Pro
actions.

Action

mikker·over 12 years ago

Completely turn off Mac OS X Dashboard

Mavericks brings yet another version of Apple’s OS X and the continued existence of Dashboard confuses everyone. Here’s how to completely turn it off:

$ defaults write com.apple.dashboard mcx-disabled -boolean YES

Also for the hardcore — Completely turn off Desktop:

$ defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true

The folder will still exist it just wont be your desktop. You might have to re-log in for it to take effect. With these two power-settings you’re ready to concentrate and do your best procrastination on something better than files or widgets – fx Twitter.

mikker·over 12 years ago

Editorial workflow: extend selection one word backwards

Editorial is my favourite iPad editor. Here’s a workflow that I made. It extends the current selection one word backwards.

Screenshot

Because of how Editorial workflows work I had to do a bit of hackery to make it work. The idea is to find the starting position of the word just before the current selection. My first version looped through every single word up till the selection but that was unusably slow on large documents. So this version only loops through every word in the 50 letters leading up to the selection. A hack, but it works really well.

mikker·over 12 years ago

A favourite from my vimrc: Full-height and full-width splits

I use vim split windows all the time. Instead of tabbing between files I just open another split. Especially if the change is small.

Here’s to of my favourties from my vimrc: <c-w>S to open a full-width, horizontal split topmost and <c-w>V to open a full-height, vertical split rightmost.

" Open splits at top level
map <c-w>V :botright :vertical :split<cr>
map <c-w>S :topleft :split<cr>

This is what it could look like:

Vim

Pretty simple. Pretty neat.

mikker·over 12 years ago

nb - a zsh function to change or create new git feature branches (with completion)

nb (new branch) is my very simplified method of creating new feature og fix branches in git.

When I’m working on fixes or features in an existing project, I check out a new branch. This also makes it way easier to create easily understood pull-requests when you need to get it reviewed.

# nb.zsh
nb () {
  if [[ $# > 0 ]]
  then
    if [[ $(git branch | tr -d '* ' | grep "$1") != "" ]]
    then
      git checkout $1
    else
      git checkout master && git checkout -b $1
    fi
  else
    git branch | tr -d '* '
  fi
}
# completion
_nb() { reply=($(git branch | tr -d '* ' | xargs echo)) }
compctl -K _nb nb

Without any arguments it just lists the current, existing, local branches. These are the ones it will tab-complete. If the argument is an existing branch then check it out, if it’s not then create it based on master.

I’m using zsh and I’m not sure what it would take to make it work in bash. Probably not too much.

mikker·over 12 years ago

One-shot command to create a Github pull-request and open it in your browser

At Firmafon we almost always concentrate updates into Github’s pull reqeusts. This provides a nice overview of the changes and an obvious place to discuss them. Here’s two scripts that I use to make this process even easier.

The first one’s called last_commit_message:

#!/bin/sh
git --no-pager log -1 --pretty=%B | sed -e "s/^ *//g" -e "s/ *$//g" | tr -d "\n"

This outputs the last commit’s message, inline without anything else but the message.

The other is called prl:

#!/bin/sh -x
set -e

last_msg="`last_commit_message`"
git push -u
url=`hub pull-request "$last_msg"`
open "$url"

This get’s the last commit’s message for use as the pull request’s title. Then it pushes to a remote branch.

Then using Github’s hub command (brew install hub) it creates a pull request from the current HEAD, catches the returned url and open’s it.

mikker·over 12 years ago

Copy the URL of the current page and return - using Drafts for iOS and this bookmarklet

When I’ve read something and want to link to it or send it to a friend I need to grab the URL from Safari. This consists of tapping the minimized address bar, then tap the address bar, then tap a million times to make the Copy/Paste popup show, then copy. This is not easy.

With this bookmarklet in your Favorites folder, it’s as easy as tap, tap, tap and you’re done.

var a=encodeURI(window.location.href);window.location='drafts://x-callback-url/create?text='+a+'&action=Copy%20to%20Clipboard&x-success='+a;

Drafts opens up, copies the url and sends you back to Safari.

mikker·over 12 years ago

Upload to imgur from Quicksilver

I’ve become very fond of attaching animated GIF’s of new UI elements when I create pull requests at Firmafon. Recording a part of the screen is very easy using Quicksilver’s built-in screen recording and trimming features. Then when done, open the file in GIF Brewery and make it a gif. I usually lock the FPS to 15 and use the Simple Palette.

So far so good, we have a gif. Let’s put it somewhere we can reference. Imgur is great and keeps the images around for a long enough time for our colleagues to see it on Github. Luckily a command-line client for imgur already exists so grab imguru from Github and put it in ~/bin (or anywhere you’d like).

Quicksilver knows Applescript and supports custom actions in ~/Library/Application Support/Quicksilver/Actions, so open up AppleScript Editor.app and make a script with the following:

using terms from application "Quicksilver"
  on open theFile
    try
      set filePath to POSIX path of theFile
      set theCommand to "~/bin/imguru '" & filePath & "'"
      return do shell script theCommand
    on error e number n
      return e
    end try
  end open

  -- Tell Quicksilver we only work with files
  on get direct types
    return {"NSFilenamesPboardType"}
  end get direct types
end using terms from

Save it as Upload to imgur.scpt or something like it and you might have to restart Quicksilver and you’re there.