Martin Ström

Screencapture in Mac OS X from a SSH session or launchd jobs

If you’d ever need to use the screencapture command from a scheduled job (like launchd in OS X) or from a SSH session, you might find out that it doesn’t allow you to do that since you’re not the logged in user.

To work around that, run the scheduled jobs as root and exec screencapture like this instead:

My Terminal Setup for Rails Development

I’m quite happy with my setup right now for the Rails projects I’m working on and wanted to share the bits and pieces I use.

First, I found a SIMB plugin for Terminal that’ll show the tab’s title in the tab instead of the running process. I use the plugin with a simple title helper in my .bash_profile to set the title simply.

function title () {
  unset PROMPT_COMMAND # more on this later
  echo -ne "\e]0;$1\a"

# Usage:
# title 'my title'

Second, I use a technique I saw on how to automatically set the title to display the current working directory as title, but since I use the plugin and helper function above, I could simplify the PROMPT_COMMAND a bit:

function set_window_and_tab_title {
  local title="$1"
  [[ -z "$title" ]] && title="root"
  echo -ne "\e]0;${title}\a"

PROMPT_COMMAND='set_window_and_tab_title "${PWD##*/}"'

That’s why I unset PROMPT_COMMAND in my title function, so it won’t auto update when I choose to set it manually using the title function.

The last piece is an updated version of my old Rails Term-script for iTerm which has been updated (and improved) for the Terminal in Mac OS X Leopard instead of iTerm.

It will open the tabs I usually use when doing Rails development and set the title of each tab using the above mentioned functions. You need to enter you projects root directory and the projects you want to have easy access to. I’ve added some comments to the AS code to help you out.

Download a zipped version of the AppleScript. Just drag the script to the Script Editor (/Applications/Utilities) to edit it.

All source code for these scripts can also be viewed on Pastie. Let me know if you have any improvements.

./script/server with automatic port finder

This is the script I’m using when I start my local Rails development server. It will automatically detect the next available port starting at the default port 3000 so I can have multiple servers running without remembering what ports that are already busy.

#!/usr/bin/env ruby


puts (`ps aux | grep [r]uby | grep script/server`.split("\n").map { |l|
  server = l.split(/\s+/)[11..-1]
  (port = server.index('-p')) ? server[].to_i : DEFAULT_PORT
}.max || DEFAULT_PORT - 1).next

I aliased it to ss in .bash_profile like this:

alias ss='./script/server -p `~/bin/available_rails_port`'

(Btw, Happy 2008! I haven’t updated this site for a while)

Autonamed dump/temp folder

To keep my desktop nice and clean I have a folder called “Dump” where I put temporary stuff like apps I want to try, PDF:s to read later that day or files I’m currently working with. But sometimes I forget to look inside that folder to find out what’s there so I wrote this Folder Action to let the folders name reflect the number of containing files/folders. (I know I could just enabled “Show item info” from Finders “View Options” but I don’t want to see all info for my hard drives and iPod)


Just create a folder on your desktop, name it something like “Dump (temp)” and attach this Folder Action to it (Folder Actions need to be located in ~/Library/Scripts/Folder Actions).

-- Set item count as folder name.scpt
on adding folder items to this_folder
    my update_name(this_folder)
end adding folder items to

on removing folder items from this_folder
    my update_name(this_folder)
end removing folder items from

on update_name(this_folder)
    set this_name to POSIX path of this_folder
    set sh_script to do shell script "P=\"" & (POSIX path of (path to me) as string) & "\";echo ${P/%.scpt/.sh}"
    set new_name to do shell script "\"" & sh_script & "\" \"" & this_name & "\""
    tell application "Finder" to set name of this_folder to new_name
end update_name

The script needs a shell script to work (since I don’t like working with AppleScript that much). It should be named exacly as the AppleScript but with an .sh extension instead of .scpt and put in the same directory. Remember to make it executable (chmod +x "Set item count as folder").

#!/usr/bin/env bash
# Set item count as folder

# Since the folder has a custom icon and therefore a `Icon?` file I
# decrease the item count by 1. Bad solution but works for now.
NUM_ITEMS=`bc <<< "$(ls "$1" | wc -l) - 1"`
if [[ $NUM_ITEMS == 0 ]]; then
elif [[ $NUM_ITEMS == 1 ]]; then
    ITEMS="$NUM_ITEMS item"
    ITEMS="$NUM_ITEMS items"
BASE=`ruby -e "puts %x(basename \"$1\").match(/(.+) \(/)"[1]`
echo "$BASE ($ITEMS)"

I first renamed the folder from the shell script but then Finder would visually move the folder one step down or left on the desktop for every update. Instead I just let the shell script generatr the new name and pass it back to the AppleScript that will rename the file as I want. usage

The other day, Oskar Karlin asked me how and when I use the OS X Terminal app instead of e.g. the Finder. I’m not by any means a hard core command line guy but ther are often and often situations where the Terminal is faster or/and easier to use for the specific thing.

Pluxemburg upload script

Today when working with the I got really tired of uploading everything with Transmit. Since our host doesn’t have support for rsync or something like that, I made this really easy bash-script that will upload everything that have been changed since last time the script was runned.

cd ~/Projects/Pluxemburg/ #local path to site root
find . -newer ./lastupload -exec open -a Transmit "{}" \;
touch ./lastupload

In Transmit, remember that both local and remote paths have been set, and “Use DockSend” enabled. I saved the script as an application using Automator (Automator –> Run Shell Script), and made an icon in 20 seconds. Now I just run the app from QuickSilver or Finder and it uploads everything really fast and easy.

Update: This is for OS X and only tested in 10.4 Tiger

CSS is in progress