Getting Image Dimensions For Sublime

Sublime Text doesn’t have any way to get dimensions for an image in the sidebar yet, though a sidebar API may be coming soon. 1

I’ve found myself writing this simple script a few times on various machines, so I thought I’d jot it down:


FILENAME=`basename $1`
DIMS=`/usr/local/bin/identify -format "width: %wpx;\nheight: %hpx;" $1`
printf "background: url($FILENAME) no-repeat;\n$DIMS\n" | pbcopy

This uses a Homebrew installed Imagemagick’s identify command to toss CSS-ready width/height into your clipboard. I then set up a custom Tool entry inside the-dusty-albeit-still-my-fav-dual-pane-file-manager ForkLift:

ForkLift Tool prefs

And if you want, you can set a keyboard shortcut also. (Note that you have to close prefs after adding your tool, and then cmd-, again to have it show up in the Keyboard Shortcuts pane—one of several minor longstanding bugs I try to ignore.)

ForkLift Keyboard prefs

Now you can select an image in ForkLift, hit the key command you specified, and then cmd-shift-v will paste it into Sublime Text respecting indentation.

Of course you may need to fix the relative path of the image filename. Still somewhat useful until ST3 manages to support this type of functionality from the sidebar.

  1. This is something I miss from TextMate, where you can just drag an image into your code to do this, even in the ancient 1.x releases: TextMate FTW (Granted, it’s getting the relative image path wrong here anyway! You just can’t win.) ↩︎