回答:
osascript -e 'tell app "Terminal"
do script "echo hello"
end tell'
これにより、新しいターミナルが開き、その内部でコマンド「echo hello」が実行されます。
osascript -e 'tell app "Terminal" to do script "echo hello"'
"$@"
、シェルではなくosaスクリプトの行に沿って引数を渡すにはどうすればよいですか?
あなたは回り道でそれを行うことができます:
% cat /tmp/hello.command
#! /bin/sh -
say hello
% chmod +x /tmp/hello.command
% open /tmp/hello.command
拡張機能.command
があり実行可能なシェルスクリプトをダブルクリックすると、新しいターミナルウィンドウ内で実行できます。open
おそらくご存じのとおり、このコマンドはFinderオブジェクトをダブルクリックするのと同じです。したがって、この手順では、新しいターミナルウィンドウ内でスクリプト内のコマンドを実行します。
少しねじれていますが、動作するようです。これにはもっと直接的なルートが必要だと思います(実際に何をしようとしているのですか?)が、それは今私から逃れています。
#!/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
ruby 1.9が必要です。または、require 'rubygems'
他の人が必要とする前に行を追加する必要がありますgem rb-appscript
。インストールを忘れないでください。
このスクリプトdt
(dup tab)に名前を付けたのでdt
、同じフォルダーでタブを開くかdt ls
、そこでls
コマンドを実行するだけで実行できます。
これは、少なくともMountain Lionでは機能します。毎回対話型シェルを初期化しますが、「macterm exec your-command」として呼び出すことにより、事後的に置き換えることができます。これをホームディレクトリのbin / mactermに保存し、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
AppleScriptでこれを行います。osascriptコマンドを使用して、効率化できます。スクリプトは次のようになります。
tell application "Terminal"
activate
tell application "System Events"
keystroke "t" using {command down}
end tell
end tell
ターミナルでのみアクセスする場合は、中間のtellステートメント以外はすべて省略できます。新しいタブではなく新しいウィンドウが必要な場合は、tキーストロークをnに置き換えます。
私はコマンドライン引数を取得して新しいウィンドウで再入力する方法を知っているのに十分なAppleScripterを経験していませんが、それは可能であり、それほど難しくはないと確信しています。
また、私はこれがうまくいくと思うし、今すぐテストすることはできませんが、#!/ usr / bin / osascript -eでいくつかのバリアントを使用してシェルスクリプトを開始し、それを実行可能ファイルとして保存できると確信していますしかし、あなたが望む。少なくとも私の頭の中では、これはあなたが$ runinnewterm ls / Applicationsのようなものをタイプすることを可能にするでしょう