Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Open iTerm bookmarks from the iTerm command line Apps
I wanted to be able to launch iTerm bookmarks from within an iTerm shell. Using two files, a shell script and an AppleScript, I can now do this. The first file, called it, is a simple shell script that calls the AppleScript. Here it is:
#!/bin/bash

if [[ $# == 0 ]]
then  
  echo "Usage: it "
  exit
fi

osascript ~/bin/it.scpt $*
Here's the AppleScript itself, called it.scpt:
on run argv
  tell application "iTerm"
    activate
    tell the first terminal
      launch session (item 1 of argv)
    end tell
  end tell
end run
The shell script makes certain you specified a bookmark to open. Then it calls it.scpt, passing in the session name you asked for. it.scpt will tell iTerm to open the appropriate bookmark in a new tab, which is exactly what I wanted. You can see the code snippets in all their syntax-highlighted glory here if you want, with a bit more info. Note that I have both files living in my ~/bin directory, but you can put them wherever you want, changing the shell script as needed.
    •    
  • Currently 3.60 / 5
  You rated: 5 / 5 (5 votes cast)
 
[14,293 views]  

Open iTerm bookmarks from the iTerm command line | 9 comments | Create New Account
Click here to return to the 'Open iTerm bookmarks from the iTerm command line' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Open iTerm bookmarks from the iTerm command line
Authored by: craigw on Apr 11, '07 09:52:17AM
Cool Script! Thanks.

I got it all to work in 1 shell script:

#!/bin/sh

if [ "$#" = "0" ]; then
    echo "Usage: it bookmarkname" && exit 1
else
osascript <<ENDSCRIPT
on run argv
  tell application "iTerm"
    activate
    tell the first terminal
      launch session "$1"
    end tell
  end tell
end run
ENDSCRIPT
fi


[ Reply to This | # ]
OOps
Authored by: craigw on Apr 11, '07 10:58:49AM
oops it doesn't need this line:
on run argv

[ Reply to This | # ]
OOps
Authored by: craigw on Apr 11, '07 11:01:27AM
or this one either obviously:
end run



[ Reply to This | # ]
Open iTerm bookmarks from the iTerm command line
Authored by: joeyGibson on Apr 11, '07 11:31:03AM
Someone sent me a modified version that is all in one file (he had changed it to open a new window, but I modified it to still open in a tab. Here it is:
#!/bin/bash

if [[ $# == 0 ]] ; then
        echo "Usage: $0 " >&2
        exit 1
fi

osascript -e 'on run argv
        tell application "iTerm"
                activate

                tell the first terminal
                        launch session (item 1 of argv)
                end tell
        end tell
end run' $@


[ Reply to This | # ]
Open iTerm bookmarks from the iTerm command line
Authored by: craigw on Apr 11, '07 12:30:12PM
Hey try this one out, now you can see a list of all your choices:
#!/bin/sh

arg=$1;
if [[ "$#" = "0" ]]; then
    echo "Usage: 'it bookmarkname' or 'it list'" && exit 1
elif [[ "$1" = "list" ]]; then
    defaults read ~/Library/Preferences/iTerm|grep Name|awk '{$1="";$2=""; print $0}'|tr -d ';'
else 
osascript <<ENDSCRIPT
on run argv
  tell application "iTerm"
    activate
    tell the first terminal
      launch session "$1"
    end tell
  end tell
end run
ENDSCRIPT
fi


[ Reply to This | # ]
better
Authored by: craigw on Apr 11, '07 01:32:26PM
you might want to change line 7:
    defaults read ~/Library/Preferences/iTerm|grep Name|grep -v NSColorPicker|awk '{$1="";$2=""; print $0}'|tr -d ';'
Cool Deal!

[ Reply to This | # ]
Open iTerm bookmarks from the iTerm command line
Authored by: rwmitchell on Apr 12, '07 03:02:43AM

I used the final script listed and it worked great, until I went to a system with multiple desktops. The command didn't seem to be doing anything, until I discovered it was adding the tabs to an iTerm window on another desktop. I changed:

tell the first terminal

to:

tell the current terminal

And now it works like I expect. YMMV.



[ Reply to This | # ]
Combined script
Authored by: mjg on Jun 28, '07 05:54:04AM
Here's the script I used, combining all the material from above along with a couple other tricks:

#!/bin/sh

arg=$1
me=`basename $0`
if [[ "$#" = "0" ]]; then
  echo "Usage: '$me bookmarkname' or '$me list'" && exit 1
elif [[ "$1" = "list" ]]; then
  defaults read iTerm|grep Name|grep -v NSColorPicker|awk '{$1="";$2="";print $0}'|tr -d ';'
else
osascript <<ENDSCRIPT
  tell application "iTerm"
    activate
    tell the current terminal
      launch session "$1"
    end tell
  end tell
ENDSCRIPT
fi


[ Reply to This | # ]
Open iTerm bookmarks from the iTerm command line
Authored by: geolaw on May 28, '12 10:16:56AM

I am using iTerm2 - so the latest version adapted for iTerm2 is :


#!/bin/sh

arg=$1
me=`basename $0`
if [[ "$#" = "0" ]]; then
echo "Usage: '$me bookmarkname' or '$me list'" && exit 1
elif [[ "$1" = "list" ]]; then
defaults read com.googlecode.iterm2|grep " Name"|grep -v NSColorPicker|awk '{$1="";$2="";print $0}'|tr -d ';'
else
osascript <<ENDSCRIPT
tell application "iTerm 2"
activate
tell the current terminal
launch session "&#36;1"
end tell
end tell
ENDSCRIPT
fi



[ Reply to This | # ]