Archive for November, 2007

Tabs in Terminal using Applescript on Leopard 1

I use screen to maintain open sessions to our servers at work. I’ve used iTerm in the past, but now that Terminal.app (on Leopard) has tabs I’d rather use it instead.

The script first checks if you have the window open already, and if you do, it’ll apply focus. If you don’t, then it launches a new Terminal window, puts 4 tabs in the window, and in each tab executes the screen command.

Note: Wordpress converted all my comments to start with a single dash, so if you’re copying-and-pasting this code you’ll need to fix the comments. All comments are meant to start with a double-dash.

There’s some bad coding conventions in there, like using global variables and not breaking out of the loop when we find what we want, but I simply don’t know how to do this in Applescript. This is my first real attempt at AS, and it has taken me long enough… though I will accept patches against this script if you want to clean it up ;)

-- `menu_click`, by Jacob Rus, September 2006
-- 
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item.  In this case, assuming the Finder 
-- is the active application, arranging the frontmost folder by date.

on menu_click(mList)
  local appName, topMenu, r

  -- Validate our input
  if mList's length < 3 then error "Menu list is not long enough"

  -- Set these variables for clarity and brevity later on
  set {appName, topMenu} to (items 1 through 2 of mList)
  set r to (items 3 through (mList's length) of mList)

  -- This overly-long line calls the menu_recurse function with
  -- two arguments: r, and a reference to the top-level menu
  tell application "System Events" to my menu_click_recurse(r, ¬
    ((process appName)'s (menu bar 1)'s (menu bar item topMenu)'s ¬
    (menu topMenu)))
end menu_click

on menu_click_recurse(mList, parentObject)
  local f, r

  -- `f` = first item, `r` = rest of items
  set f to item 1 of mList
  if mList's length > 1 then set r to (items 2 through (mList's length) ¬
    of mList)

  -- either actually click the menu item, or recurse again
  tell application "System Events"
    if mList's length is 1 then
      click parentObject's menu item f
    else
      my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
    end if
  end tell
end menu_click_recurse

-- Matthew Lambie, November 2007
-- http://lambie.org

-- Opens a Terminal with four tabs that then each reconnect to named screen 
-- sessions or if that window is already opened, reapply focus

global window_id
global window_name
set window_id to 0
set window_name to ""
set user to "mlambie"
set server to "hotrod"
set user_at to user & "@" & server

-- MAINLINE
tell application "Terminal"
  activate

  -- get every window id
  set w_ids to (id of every window)

  -- with each window id...
  repeat with w_id in w_ids

    -- have we found our target window_id?
    if window_id is equal to 0 then

      -- load this window's name
      set w_name to name of window id w_id

      -- is this the window we're looking for?
      if (texts 1 thru (count user_at) of w_name) is equal to user_at then
        set window_id to w_id
        set window_name to name of window id window_id
      end if
    end if
  end repeat

  -- if we have a window_id then we can give that window
  -- the focus, otherwise we need to make a new window
  -- with four tabs and execute the ssh/screen command in
  -- each of those four tab 

  if (window_id is not equal to 0) then
    -- give that window the focus
    set frontmost of window id window_id to true
  else

    -- make a new window with the execution of a trivial command
    do script "clear"

    -- load up the window id of the window we just created
    set window_id to id of first window whose frontmost is true

    -- make tabs 2, 3 and 4
    repeat with i from 1 to 3
      my menu_click({"Terminal", "Shell", "New Tab", "Pro"})
    end repeat

    -- for each of the four tabs we've now made
    repeat with i from 1 to 4

      -- build the command, then execute it
      set cmd to "clear; ssh -t " & server & " 'screen -d -R tfg_" & i & "'"
      do script cmd in tab i of window id window_id of application "Terminal"

    end repeat
  end if
end tell

More on Flash video players 0

I found another great site that we’re using for some client’s Flash video (FLV) files. It has a nifty configuration page that generates the HTML code you need to embed in your page to make it all work.

What’s really cool is that it relies on a remote SWF object, so you only need to upload the video file to your web site.

51 minutes 0

My order of four t-shirts from America was shipped 51 minutes after I placed it online. It’s estimated that it’ll take 3-5 days with express international shipping. That’s almost as fast as my going to the shops and buying them.

I love the internets.

Boot Camp with OS X 2

I know I’m about 18 months too late, but I thought I’d write about my experiences with Apple’s Boot Camp now that it’s out of beta and is officially supported.

As you’d expect, the Apple-side of things is fantastic. Their application lets you resize your hard disk without losing any data. I chose to have a 10GB partition for Windows and it did everything for me.

In the past you’ve needed to make a drivers disc, but with Leopard the drivers are included on the DVD. After making the partition I booted up the Windows install CD and stepped through the install as normal. Man, looking at the Windows XP installer was painful, especially considering I’d just used the Leopard installer days beforehand. Granted it’s 7 years old… but it’s pretty ugly now. After the installation finished I jammed in the Leopard DVD and it installed the Windows-side of Boot Camp, which includes all the drivers for the hardware in the MacBook Pro.

Windows on Mac

I then used the latest beta (v1.6 supports Leopard; the stable release doesn’t) of Winclone to take a complete snap-shot of the NTFS partition from within OS X. It smashed the 10GB partition (which was 3GB full) into a 700MB file. Go team compression!

I threw in “2004 game of the year” (which I’ve played before and quite liked) to see how it handles games and it’s absolutely smashed it. The graphics card in my laptop is pretty decent, considering it’s a mobile machine (which incidentally is why Leopard runs better than Tiger: a lot of the CoreAnimation is put out to the GPU over the CPU now, as well as it being Apple’s second run at an Intel-compatible OS). Sure, the game’s a few years old, but it’s still running like a dream.

The only problem I have is that there’s no right mouse button, and tapping on the trackpad doesn’t work. Aaron lent me a USB mouse. Normally I use a two-finger tap to get a context-menu in OS X, but on Windows XP I have to hold down both fingers and press the trackpad button, which is kinda annoying when I’m trying to smash some Orks.

Lastly, a huge thanks to Tony for helping me acquire some of the software used in this experiment ;)

And the winner of “Best Product Name, 2007″ is… 5

Yesterday I started using GMail again, solely for the purpose of email mailing lists. I logged in and deleted the 10,000 emails that had built up since I last had this great idea, and then signed up to Apple’s applescript-user mailing list. I also cleared out my spam folder, which only had about 600 entries, though I think it automatically deletes them after 30 days.

This evening I’ve logged in to see if anyone has replied to my request (no luck) and saw I had 6 new spam emails. I wondered how good the filter was, so I checked it out. One was advertising a men’s health product called “MegaDick.”

Seriously. That’s the best product name ever. Is there even any question about what their claiming?

On another note, I checked out the site, and at US$60 for some herbal tablets, someone’s making a killing. So many stupid people… I’ll put this on the list of sites we need to knock off at some point (after strippersnow.com and myjobsearch.com)

(And now I wait for the jokes to start about why I was at the site… I’m pretty comfortable with myself, and I think Maggie is too, but don’t let that stop you ;)

« Previous Page