colourandcode
colourandcode

Reputation: 471

Open a terminal from ruby code?

Are there any system libraries that will allow me to open groups of terminals (as tabs not multiple windows) from ruby? I don't want to use the exec() method to open the terminal app... For instance I'm running about 5 different terminals in my environment (mongodb, redis, daemons, etc) and I want to write a script that will open up that group of windows and execute commands to startup up all of those processes. Any ideas? I'm thinking I might only be able to do it with Objective-C or MacRuby.

Upvotes: 7

Views: 3620

Answers (6)

Vasily  Bodnarchuk
Vasily Bodnarchuk

Reputation: 25294

Solution

class Terminal
    def self.runInNewWindow(command)
        `osascript -e 'tell app "Terminal"
        do script "#{command}"
        end tell'`
    end
end

class File
    def self.create(filename, text)
        fo = File.open(filename, "w+")
        File.chmod(0777, filename)
        fo.puts text
        fo.close
    end
end

Usage

filename = "file"
#closeWindowCommand = "osascript -e 'tell app \"Terminal\" to close first window' & exit"
removeFileCommand = "rm #{filename}"
command = "#{RUBY_VERSION}"
path = File.expand_path('../', __FILE__)
File.create(filename, "echo #{command}; #{removeFileCommand}")
Terminal.runInNewWindow("cd #{path}; ./#{filename}")

Full sample

file "run.rb"

class Terminal
    def self.runInNewWindow(command)
        `osascript -e 'tell app "Terminal"
        do script "#{command}"
        end tell'`
    end
end

class File
    def self.create(filename, text)
        fo = File.open(filename, "w+")
        File.chmod(0777, filename)
        fo.puts text
        fo.close
    end
end

filename = "file"
#closeWindowCommand = "osascript -e 'tell app \"Terminal\" to close first window' & exit"
removeFileCommand = "rm #{filename}"
command = "#{RUBY_VERSION}"
path = File.expand_path('../', __FILE__)
File.create(filename, "echo #{command}; #{removeFileCommand}")
Terminal.runInNewWindow("cd #{path}; ./#{filename}")

file "run"

cd "$(dirname "$0")"
ruby run.rb

Execute the sample

Open file run

Results

enter image description here enter image description here

Upvotes: 1

Achilles
Achilles

Reputation: 766

Elscripto is a gem that allows you to automate the opening of Terminal tabs running pre-specified scripts: https://github.com/Achillefs/elscripto

Upvotes: 0

jerseyboy
jerseyboy

Reputation: 1326

If you go to Terminal -> Preferences -> Settings Tab -> "+" (to create a new setting), name the setting "mongo", Click on the "Shell" menu item in the pane, Check the "Startup" checkbox and enter a shell command to start and/or monitor logs Set other options to taste Go to the Gear menu item (next to the +, -, Default), select "Export" Save as a file "mongo.terminal" in your repo.

To open when you're already in a terminal, type open mongo.terminal (insert proper path as needed)

Now here's the kicker: you can go into the Window Groups tab and collect the special purpose terminal configs under one project name, export that window group to a .terminal file, and get them all launched together by opening it.

  • OSX has a command called "open" which will open the primary application associated with a given file, as if the user clicked on its Desktop icon. This works on .terminal files too. There are other ways, and it can get hijacked if someone alters the associations to ".terminal" files, but that's unlikely and fairly easy to detect (your terminals won't launch).

  • You can more easily select color/background/font etc. from the Terminal user interface this way, and get them into a repo for sharing and reuse.

  • The exported *.terminal files are plist documents in XML. Most of the important data fields look like they are base64 encoded, so not very editable but there are a few things you can change if you know what you're doing.

Upvotes: 1

Augusto
Augusto

Reputation: 29827

The terminitor gem does exactly what you want and it uses rb-appscript behind scenes.

Upvotes: 4

Michael Kohl
Michael Kohl

Reputation: 66837

Personally I'd say forget about Ruby for this, just script tmux:

http://onethingwell.org/post/455644179/tmux

Example from the post above:

#!/bin/sh
tmux new-session -d -s main
tmux new-window -t main:1 alpine
tmux rename-window -t main:1 mail
tmux new-window -t main:2 'newsbeuter -r'
tmux rename-window -t main:2 news
tmux select-window -t main:0
tmux attach -t main

Upvotes: 4

Pablo Castellazzi
Pablo Castellazzi

Reputation: 4184

I had a similar setup using screen. You need to write a very simple .screenrc with the commands you want to run, and some specific screen commands to create and split windows.

Upvotes: 0

Related Questions