-
Notifications
You must be signed in to change notification settings - Fork 11
/
Open_Terminal_Here.js
63 lines (51 loc) · 2.2 KB
/
Open_Terminal_Here.js
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
/**
* Adds an "Open Terminal Here" menu item to folder items in the Places widget
*
* @source https://github.com/Komodo/macros
* @author Nathan Rijksen
* @contributor Mathieu Strauch
* @version 0.3
*/
// Register namespace
if ((typeof extensions) == 'undefined') extensions = {};
extensions.OpenTerminalHere = {};
(function()
{
// Get places pane document object
var d = document.getElementById('placesViewbox').contentDocument;
// Remove existing menu entry if it exists
var mi = d.getElementById('contextOpenTerminalHere');
if (mi) mi.parentNode.removeChild(mi);
// Get the sibling element which we want to insert our menu item after
var sibling = d.getElementById('placesContextMenu_rename');
if ( ! sibling) return;
var platform = navigator.platform.toLowerCase();
// Create our menu item
mi = document.createElement("menuitem");
mi.setAttribute("id", "contextOpenTerminalHere");
mi.setAttribute("hideIf", "file project");
mi.setAttribute("label", "Open Terminal Here");
// Add event listener for when the menu item is used
mi.addEventListener('command', function(e)
{
uris = ko.places.viewMgr.getSelectedURIs()
if ( ! uris.length) return;
path = uris[0].replace(/^[a-zA-Z]+:\/\//,'');
// Prepare command for each platform
var command;
if (platform.indexOf("mac") != -1) // Mac / OSX -- terminal
command = 'osascript -e \'tell application "terminal"\' -e \'do script \
"cd \\"' + path + '\\""\' -e \'activate\' -e \'end tell\'';
else if (platform.indexOf("linux") != -1) // Linux -- gnome-terminal
command = 'gnome-terminal --working-directory="' + path + '"';
else if (platform.indexOf("win") != -1) // Windows -- command prompt
{
path = path.replace(/^(?:[a-z]*:\/\/|\/)/i,'');
command = 'start cmd /K "cd ' + path + '"';
}
// Run command, dont show output window
ko.run.runCommand(window, command, {env: null}, null, null, null, true, 'no-console');
});
// Append menu item to popupmenu
sibling.parentNode.insertBefore(mi, sibling);
}).apply(extensions.OpenTerminalHere);