Some TextMate Bundle Shell-Scripts

These are a few (possibly obsolete) examples of TextMate bundle commands that are incorporated with the individually installable bundles I have made available on-line. There is no need to manually enter these, but you can do so to see how easy it is to create your own (or modify mine).

To create your own, go to the TextMate menu, then go to Bundles > Bundle Editor > Edit Commands ... You can create a new command by cutting and pasting one of the following shell scripts in the editor window. You can give it an evocative name and key equivalent. The scope selector in most cases (not the first) should be set to "source.shell". Input should be "none" and output set to "Discard."

From the iTerm Bundle: cd to the directory containing the file displayed in TextMate

The first example is a command to open a new iTerm tab and cd to the directory that contains the file being edited.

        #!/bin/zsh -f
        osascript << END
        tell application "iTerm"
                make new terminal
                tell the first terminal
                        activate current session
                        launch session "Default Session"
                        tell the last session
                                write text "cd \"$TM_DIRECTORY\""
                                write text "clear"
                                write text "pwd"
                        end tell
                end tell
        end tell
        END
        



Modified "Run Script" command from the ZSH Bundle

The second example is a command to run a shell script. You can already do this from within TextMate, but this one gives you a chance to feed a string of arguments and parameters into your shell script (via an AppleScript dialog box), and then it runs your shell script along with the arguments in a new iTerm tab from the directory containing the file. You need to make the script executable first.


        #!/bin/zsh -f
        function GetArgs {
            osascript << eof-1
                tell app "Finder"
                    activate
                    set prompt to "Enter a list of arguments or hit OK to leave blank: "
                    set dialogResult to display dialog prompt default answer ""
                end tell

        eof-1
        }

        arg_array=( $( GetArgs | cut -f 2 -d ":" | cut -f 1 -d "," ))

        # chmod a+x "$TM_FILEPATH"

        ThePath=( "$TM_DIRECTORY" )
        BaseName=( $(basename "$TM_FILEPATH" ) )


        osascript << END
        tell application "iTerm"
                make new terminal
                tell the first terminal
                        activate current session
                        launch session "Default Session"
                        tell the last session
                                write text "cd \"$ThePath\""
                                write text "clear"
                      write text "./\"$BaseName\" $arg_array"
                        end tell
                end tell
        end tell
        END
        



The third example is pretty similar to the second, except it is for running zsh functions. It loads, reloads (just to cover all bases) and then executes a function, again with a dialog box to collect optional arguments.

        #!/bin/zsh -f

        function GetArgs {
            osascript << eof-1
                tell app "Finder"
                    activate
                    set prompt to "Enter a list of arguments or hit OK to leave blank: "
                    set dialogResult to display dialog prompt default answer ""
                end tell

        eof-1
        }

        # chmod a+x "$TM_FILEPATH"

        ThePath=( "$TM_DIRECTORY" )
        BaseName=( $(basename "$TM_FILEPATH" ) )


        arg_array=( $( GetArgs | cut -f 2 -d ":" | cut -f 1 -d "," ))


        osascript << END
        tell application "iTerm"
                make new terminal
                tell the first terminal
                        activate current session
                        launch session "Default Session"
                        tell the last session
                                ---write text "cd \"$ThePath\""
                      write text "autoload -U $BaseName"
                      write text "reload $BaseName"
                      write text "$BaseName $arg_array"
                        end tell
                end tell
        end tell
        END
        


The fourth example is just a re-write of the stock "Run Script" bundle command. It actually doesn't use iTerm, but it steals from the same idea as the run script in iTerm command listed above, namely, it asks for arguments to the commmand before running it'

I'm having trouble formatting it for the web page, so if you want to see it, click here: Run Script (revised)




Example from the Fink Bundle to rebuild and reinstall a package

The fifth example rebuilds and reinstalls a fink package corresponding to the info file being edited. This is probably only of use if you are a fink package developer.

        #!/bin/zsh -f
        source /sw/bin/init.sh
        fink -y rebuild $TM_FILEPATH:r:t
        fink -y reinstall $TM_FILEPATH:r:t
        result=$?
        print ""
        print ""
        fink list  $TM_FILEPATH:r:t
        print ""

        print "Done"
        return $result
        

Return to TextMate -- iTerm Shell Scripts and Command Bundles



Click here for web site index
Valid HTML 4.01!