-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathOpen directory in Terminal.plist
More file actions
130 lines (123 loc) · 3.55 KB
/
Open directory in Terminal.plist
File metadata and controls
130 lines (123 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?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>#!/usr/bin/env ruby18 -wKU
require ENV['TM_SUPPORT_PATH'] + '/lib/escape.rb'
def find_directory
files = [ ENV['TM_SELECTED_FILE'], ENV['TM_FILEPATH'], ENV['TM_DIRECTORY'], ENV['TM_PROJECT_DIRECTORY'], ENV['HOME'] ]
res = files.find { |file| file && File.exists?(file) }
res && File.file?(res) ? File.split(res).first : res
end
def is_running(process)
all = `ps -U "$USER" -o ucomm`
Array(all).find { |cmd| process == cmd.strip }
end
def terminal_script_new_window(dir)
return <<-APPLESCRIPT
tell application "Terminal"
activate
do script "cd #{e_as(e_sh(dir))}; clear; pwd"
end tell
APPLESCRIPT
end
def terminal_script_new_tab(dir)
return <<-APPLESCRIPT
tell application "Terminal"
activate
set numberOfTabs to count tabs in window 1
tell application "System Events"
repeat while "Terminal" is not name of (process 1 where frontmost is true)
delay 0.1
end repeat
tell process "Terminal" to keystroke "t" using command down
end tell
set startedAt to current date
repeat while (count tabs in window 1) is numberOfTabs
delay 0.1
if (current date) - startedAt > 2 then
error "Could not open new tab"
end if
end repeat
do script "cd #{e_as(e_sh(dir))}; clear; pwd" in the last tab of window 1
end tell
APPLESCRIPT
end
def iterm_legacy_script(dir)
return <<-APPLESCRIPT
tell application "iTerm"
activate
if exists the first terminal then
set myterm to the first terminal
else
set myterm to (make new terminal)
end if
tell myterm
activate current session
launch session "Default Session"
tell the last session
write text "cd #{e_as(e_sh(dir))}; clear; pwd"
end tell
end tell
end tell
APPLESCRIPT
end
def iterm_script(dir)
return <<-APPLESCRIPT
tell application "iTerm2"
activate
if exists the first window then
set aWindow to the first window
else
set aWindow to (create window with default profile)
end if
tell aWindow
set newTab to (create tab with default profile)
tell newTab
repeat with aSession in sessions
tell aSession
write text "cd #{e_as(e_sh(dir))}; clear; pwd"
end tell
end repeat
end tell
end tell
end tell
APPLESCRIPT
end
if dir = find_directory
script = nil
if ENV['TM_TERMINAL'] =~ /^iterm$/i || is_running('iTerm2')
script = iterm_script(dir)
elsif is_running('iTerm')
script = iterm_legacy_script(dir)
else
script = ENV['TM_TERMINAL_USE_TABS'] ? terminal_script_new_tab(dir) : terminal_script_new_window(dir)
end
open("|osascript", "w") { |io| io << script }
end
</string>
<key>input</key>
<string>none</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>^O</string>
<key>name</key>
<string>Open Terminal</string>
<key>outputCaret</key>
<string>afterOutput</string>
<key>outputFormat</key>
<string>text</string>
<key>outputLocation</key>
<string>discard</string>
<key>semanticClass</key>
<string>callback.file-browser.action-menu</string>
<key>uuid</key>
<string>54CDB56E-85EA-11D9-B369-000A95E13C98</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>