ThisSpace

One of the first things that happened to me in Spaces that irked me was its auto-switching to another space when I didn’t want it to. Sure, that’s probably what I’d want it to do a lot of the time, but if I’m trying to open a new window for an app that already has windows in other spaces, I have to select that app, create a new window, grab its title bar, and move back to the space I was in when I selected the app.

I was wondering what would happen if you could create a new window without activating the app, so I typed this into Script Editor:

tell application "iTerm"
	set term to (make new terminal)
	tell term
		launch session "default session"
		activate
	end tell
end tell

The result is a brand new iTerm window in my current space, without regard to whether there might already be such a window in another space. Switching spaces from a script is pretty easy too — as long as you aren’t holding down any modifiers when the script runs (say, by having used command-r in Script Editor):

tell application "System Events"
	delay 1 -- so I can let go of command
	keystroke "2" using control down
	delay 1 -- so you can see one switch happen and then the other
	key code 125 using control down
end tell

The one missing piece is moving a window — I don’t think it’s possible to drag an object via GUI scripting, so you can’t grab the title bar of a window, switch spaces, and then release it. At least it might be possible to create a fairly complex setup from a login script…if you had a lot of time on your hands. :)

AppleScript | Tagged , | Permalink | 2 Comments

iTerm/TextMate Here

Yes, again. Inspired by other solutions to the same problems, here are updated versions of these scripts. The primary difference is that they’re based on Apple’s droplet sample, so you can either click on them (for the current directory) or drop stuff on them and it’ll work either way. So, why don’t I just use his scripts, like I do his icons? Beats me — I updated these a week or two ago, and I’ve long since forgotten.1

Continue reading…

  1. While I’m at it, I’ve also forgotten what the other thing was I wanted to do to them before posting this. []

AppleScript | Tagged , , , | Permalink | No Comments

Later that year…

A while back, John Gruber posted a FastScripts script to fix a loathsome Apple Mail behavior, which was shortly followed by signature-enabled improvements. Fourteen short weeks later, here’s my own version:

tell application "Mail" to activate
tell application "System Events"
	tell process "Mail"
		-- Run the regular "Reply" command
		tell menu bar 1
			click menu bar item "Message"'s menu "Message"'s menu item "Reply"
		end tell
		delay 1.0 -- Give it a chance to finish
 
		-- Remember what signature was selected and switch to "None"
		set blib to value of (pop up button 1 of window 1)
		if blib is not "None" then
			click pop up button 1 of window 1 -- The Signature popup
			click menu item "None" of menu 1 of pop up button 1 of window 1
			delay 0.2
		end if
 
		-- Delete the return Mail inserted; recreate it at the bottom where it belongs
		key code 117 -- Forward delete
		key code 125 using command down -- Command-down (skip to the end)
		key code 36 -- Return
 
		-- Restore the previously selected Signature
		if blib is not "None" then
			click pop up button 1 of window 1 -- Still the Signature popup
			click menu item blib of menu 1 of pop up button 1 of window 1
			delay 0.2
			key code 117 -- Mail inserts a bonus line here too
		end if
	end tell
end tell

One drawback that I maintain from John’s original is the use of a time delay — I have other scripts that use them, and for each of them I occasionally have the script fail because my machine is a little busier that day.1 That being said, this script usually runs correctly.

My improvement is how I deal with arbitrary signatures. If you pick a different signature (or no signature) from the popup menu in the compose window, Mail updates the message correctly — so I just use that behavior to do the messy bits.

  1. Yes I’m still using a PowerBook — what’s that got to do with anything? []

AppleScript | Tagged , , | Permalink | No Comments

TextMate Here

TextMate Icon

That was actually the first post in a series (of two). This one opens a new TextMate window on the selected item (if there’s a single item selected), or the current folder otherwise. This makes the script a lot shorter (that and there being a simple command-line TextMate launcher that I can use).

The instructions are the same as last time; save it as an application somewhere you can find it, and call it “TextMate Here”. Find it with the Finder, copy and paste TextMate’s icon onto it, and drag it up into your Finder toolbar.

If you have TextMate installed someplace strange (or have an old enough one that it doesn’t have the mate command in it) it will break; if this happens, you get to keep both pieces.

Here’s the script:

on currentFolder()
    tell application "Finder"
        try
            return POSIX path of (front window's folder as text)
        on error
            return null
        end try
    end tell
end currentFolder
 
on selectedItem()
    tell application "Finder"
        try
            get the selection
            return POSIX path of (the result's first item as text)
        end try
    end tell
    return currentFolder()
end selectedItem
 
do shell script "/Applications/TextMate.app/Contents/Resources/mate '" & selectedItem() & "'"

Feedback, improvements, etc. still welcome.

AppleScript | Permalink | No Comments

iTerm Here

iTerm Logo

This is, for lack of a better description, my version of the Open a Command Window Here Windows XP PowerToy. I’ve played around with doing this from the context menu, but I eventually decided I’m not fond of that approach. Since I have the toolbar visible in Finder, however, it’s pretty easy to put a script up there and pretend it’s a real toolbar button.

To use it, save it as an application somewhere you can find it, and call it “Term Here”. Find it with the Finder, copy and paste iTerm’s icon onto it, and drag it up into your Finder toolbar (like how you would drag it into the dock).

My version of this will use the path of the selected item in the front finder window if it is a folder (and is the only thing selected). Otherwise, it will use the path of the Finder window itself. If you want to skip the selection business altogether, just replace the call to selectedFolder() before the last (iTerm) stanza to currentFolder(). It then launches iTerm if needed, create’s a new (empty) window if needed, opens a new tab in that window pushd’s to the directory, and lists its contents for you.

Here’s the script:

on currentFolder()
    tell application "Finder"
        try
            return POSIX path of (front window's folder as text)
        on error
            return null
        end try
    end tell
end currentFolder
 
on selectedFolder()
    try
        tell application "Finder"
            get the selection
            set theItem to result's first item as alias
        end tell
        set theInfo to info for theItem
        if theInfo's folder then
            return POSIX path of theItem
        end if
    end try
    return currentFolder()
end selectedFolder
 
set targetPath to selectedFolder()
tell application "iTerm"
    activate
    try
        set term to the front terminal
    on error
        set term to (make new terminal)
    end try
    tell term
        launch session "default session"
        tell current session
            write text "pushd '" & targetPath & "'"
            write text "ls"
        end tell
    end tell
end tell

Feedback, improvements, etc. welcome. Unless you think it should automatically detect whether iTerm or Terminal is your default terminal program, in which case your feedback is only welcome if you can explain a way to detect this from AppleScript that doesn’t double the length of the script. :) I just don’t care enough about this issue to figure out how to do it.

AppleScript | Permalink | No Comments

A Little Bit Slower Now… (a little bit slower now…)

When I log into my user account on my PowerBook, everything stops for a few minutes. Drops of rain stop in mid-air, the wind falls still, the clock stops ticking, and my cat stops preening himself. I’m tired of this.

Spurred on by these hints, I finally got around to do something about it. I created a folder, ~/Library/Login Items/, dropped aliases to applications formerly in my (very lengthy) list of regular login items, and removed them from the OS’s list. Then I saved the following script as Slow Launch.app in that same directory:

-- Delay before the first launch and between subsequent launches
set firstLaunchDelay to 10
set interLaunchDelay to 5
 
tell application "Finder"
	-- Find my list of launch items
	set loginItemsFolder to (container of (path to me) as alias)
	set loginItemsList to loginItemsFolder's items whose kind is "Alias"
 
	-- Set the initial delay
	set launchDelay to firstLaunchDelay
 
	-- Process the list
	repeat with loginItem in loginItemsList
		-- Hurry up and wait
		delay launchDelay
		set launchDelay to interLaunchDelay
 
		-- Open the item; hide it if requested
		open loginItem
		if (loginItem's comment is "hide") then
			delay launchDelay
			set launchDelay to 0
			set (process ( (loginItem's displayed name) as text) )'s visible to false
		end if
	end repeat
end tell

Add Slow Launch.app to the OS’s login items list, and I’m done. I could swear that it even takes less time to launch everything, in spite of all the waiting the script does. The only drawback is that not everything can taken out of the OS’s list; some apps1 helpfully put themselves back again if you take them off the list, and others2 nag you about not being in that list whenever they’re launched. All that being said, it still feels a lot snappier this way.

  1. Witch, for example []
  2. Slim Battery Monitor, for example []

AppleScript, Code | Permalink | No Comments

RDC Shortcut

Since I do occasionally have to fiddle with a Windows server, and there are little things about Microsoft’s Remote Desktop Client that bug me (only one session open at a time, for example), I wondered if the matter was scriptable somehow. Here’s one way (after installing rdesktop via DarwinPorts):

#!/bin/bash
#
RDC="/opt/local/bin/rdesktop -a24 -g 1024x768"
#
set -x
if [ "X$DISPLAY" = "X" ]; then
    open-x11 $0
else
    exec $RDC -ushloob example.com
fi

The interesting thing (to me anyway) is having the script exec itself inside X11, to avoid having two dumb files laying around for each server listed. (A benefit that becomes irrelevant if you wrap it up in a nice, tidy AppleScript bundle so it is doubleclick-able)

If you’re enough of a command-line wonk to think a slew of new scripts in your path is actually a shortcut for something, just put a copy of this in your path for each server and mark them all executable. Building a generic script that creates a temporary file (open, for which open-x11 is simply a wrapper, doesn’t let you specify arguments to the command) and flings that at X11.app is left as an exercise.

Code | Permalink | No Comments

A bash script to mess with the containing Terminal.app window

I found it, but I still don’t have a clue as to why I wrote it to begin with:

function setgeometry
{
    local rows=`expr "$1" : '[0-9]*x([0-9]*)$'`
    local cols=`expr "$1" : '([0-9]*)x[0-9]*$'`
    case $TERM in
        Apple_Terminal)
            window=`osascript -e 'tell app "Terminal" to get first window'`
            #echo $window
            osascript -e 'tell app "Terminal"'
                             -e "set number of rows of $window to $rows"
                             -e "set number of columns of $window to $cols"
                         -e 'end tell'
            return $?
            ;;
        *)
            echo "Sorry, I don't know how to do that in $TERM"
            return 1
            ;;
        esac
}

It doesn’t seem to work anymore, but the concept is still valid (i.e., osascript -e ‘tell app “Terminal”‘ -e ’set blib to first window’ -e “get blib’s number of columns” -e ‘end tell’ will actually tell you something, although that’s hardly a useful example).

AppleScript | Permalink | No Comments

Zot’s JavaScript Status v0.3.1

Here’s an updated one that renders as intended on more platforms. I’ve tested Mac OS X and Windows, and for the moment I’m hoping for the best on Linux (no linux machine handy right this minute).

Install or Download.

Firefox | Permalink | No Comments

Zot’s JavaScript Status v0.3

I eventually got tired of waiting for the Firefox extension I had been using for this to be updated, so I did it myself. I haven’t found the need to tweak this since I rolled it a week or two ago, so hopefully it is stable-ish.

Install or Download.

Firefox | Permalink | 1 Comment