Opening a new terminal from the command line and running a command on Mac OS X?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island
--
Chapters
00:00 Opening A New Terminal From The Command Line And Running A Command On Mac Os X?
00:22 Answer 1 Score 8
00:59 Answer 2 Score 3
01:39 Accepted Answer Score 45
01:52 Answer 4 Score 6
02:13 Thank you
--
Full question
https://superuser.com/questions/174576/o...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#macos #commandline #terminal
#avk47
ACCEPTED ANSWER
Score 45
osascript -e 'tell app "Terminal"
do script "echo hello"
end tell'
This opens a new terminal and executes the command "echo hello" inside it.
ANSWER 2
Score 8
You can do it in a roundabout way:
% cat /tmp/hello.command
#! /bin/sh -
say hello
% chmod +x /tmp/hello.command
% open /tmp/hello.command
Shell scripts which have the extension .command
and which are executable, can be double-clicked on to run inside a new Terminal window. The command open
, as you probably know, is equivalent to double-clicking on a Finder object, so this procedure ends up running the commands in the script within a new Terminal window.
Slightly twisted, but it does appear to work. I feel sure there must be a more direct route to this (what is it you're actually trying to do?), but it escapes me right now.
ANSWER 3
Score 6
This works, at least under Mountain Lion. It does initialize an interactive shell each time, although you can replace that after-the-fact by invoking it as "macterm exec your-command". Store this in bin/macterm in your home directory and chmod a+x bin/macterm:
#!/usr/bin/osascript
on run argv
tell app "Terminal"
set AppleScript's text item delimiters to " "
do script argv as string
end tell
end run
ANSWER 4
Score 3
#!/usr/bin/env ruby1.9
require 'shellwords'
require 'appscript'
class Terminal
include Appscript
attr_reader :terminal, :current_window
def initialize
@terminal = app('Terminal')
@current_window = terminal.windows.first
yield self
end
def tab(dir, command = nil)
app('System Events').application_processes['Terminal.app'].keystroke('t', :using => :command_down)
cd_and_run dir, command
end
def cd_and_run(dir, command = nil)
run "clear; cd #{dir.shellescape}"
run command
end
def run(command)
command = command.shelljoin if command.is_a?(Array)
if command && !command.empty?
terminal.do_script(command, :in => current_window.tabs.last)
end
end
end
Terminal.new do |t|
t.tab Dir.pwd, ARGV.length == 1 ? ARGV.first : ARGV
end
You need ruby 1.9 or you will need to add line require 'rubygems'
before others requires and don't forget to install gem rb-appscript
.
I named this script dt
(dup tab), so I can just run dt
to open tab in same folder or dt ls
to also run there ls
command.