Skip to content

Commit

Permalink
Add command to run script in Terminal
Browse files Browse the repository at this point in the history
Executes the current script. Defaults to running in Terminal.app but will use iTerm instead if it is already running. Can also be customized using the `TM_TERMINAL` variable set to `iTerm`. Also respects the `TM_RUBY` variable.
  • Loading branch information
Timothy Xu authored and infininight committed Sep 14, 2015
1 parent 4551f55 commit 9187908
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
82 changes: 82 additions & 0 deletions Commands/Run Script in Terminal.tmCommand
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>#!/bin/bash
[[ -z "$TM_FILEPATH" ]] &amp;&amp; TM_TMPFILE=$(mktemp -t rubyInTerm)
: "${TM_FILEPATH:=$TM_TMPFILE}"; cat &gt;"$TM_FILEPATH"
# run script using either Terminal.app or iTerm.app
# if iTerm is open or if TM_TERMINAL is set to iTerm then use iTerm
# otherwise default to Terminal.app since that is standard.
TP=${TM_TERMINAL:=Terminal}
TPY=${TM_RUBY:=ruby}
esc () {
STR="$1" ruby18 &lt;&lt;"RUBY"
str = ENV['STR']
str = str.gsub(/'/, "'\\\\''")
str = str.gsub(/[\\"]/, '\\\\\\0')
print "'#{str}'"
RUBY
}
iTerm_running () {
ruby &lt;&lt;"RUBY"
all = `ps -U "$USER" -o ucomm`
split = all.split("\n")
if split.find { |cmd| 'iTerm' == cmd.strip }
STDOUT.write 0
else
STDOUT.write 1
end
RUBY
}
if [ "$TP" == iTerm ] || [ $(iTerm_running) == 0 ]; then
osascript &lt;&lt;END
tell application "iTerm"
activate
tell the current terminal
tell (launch session "TextMate")
write text "clear; cd $(esc "${TM_DIRECTORY}"); $(esc "${TPY}") $(esc "${TM_FILEPATH}"); rm -f $(esc "${TM_TMPFILE}")"
end tell
end tell
end tell
END
else
osascript &lt;&lt;- APPLESCRIPT
tell app "Terminal"
launch
activate
do script "clear; cd $(esc "${TM_DIRECTORY}"); $(esc "${TPY}") $(esc "${TM_FILEPATH}"); rm -f $(esc "${TM_TMPFILE}")"
set position of first window to { 100, 100 }
end tell
APPLESCRIPT
fi
</string>
<key>input</key>
<string>document</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>^@R</string>
<key>name</key>
<string>Run in Terminal</string>
<key>outputCaret</key>
<string>afterOutput</string>
<key>outputFormat</key>
<string>text</string>
<key>outputLocation</key>
<string>discard</string>
<key>scope</key>
<string>source.ruby</string>
<key>uuid</key>
<string>4444CB9A-8345-4116-8349-C359ADDA390C</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
1 change: 1 addition & 0 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<key>items</key>
<array>
<string>35222962-C50D-4D58-A6AE-71E7AD980BE4</string>
<string>4444CB9A-8345-4116-8349-C359ADDA390C</string>
<string>5289EE40-86B8-11D9-A8D4-000A95E13C98</string>
<string>------------------------------------</string>
<string>D5660BB2-C554-4694-9E0F-F20CDB6B9EA0</string>
Expand Down

0 comments on commit 9187908

Please sign in to comment.