Sublime Forum

Open Command Window Here

#1

Does anyone know if anything been added to the API that allows a plugin to grab the current path triggered from a sidebar folder right click context menu entry?

Iā€™d like to be able to right click a folder and ā€œOpen Command Window Hereā€ type thing.

0 Likes

#2

Windows only :smile:

Make a new folder in your plugin directory, create a new file called opencommand.py and put this code in it:

[code]import sublime, sublime_plugin
import subprocess

class OpenPromptCommand(sublime_plugin.TextCommand):
def run(self, edit):
dire = os.path.dirname(self.view.file_name())
retcode = subprocess.Popen(ā€œcmdā€, ā€œ/Kā€, ā€œcdā€, dire])

def is_enabled(self):
    return self.view.file_name() and len(self.view.file_name()) > 0[/code]

Now create another file called ā€œContent.sublime-menuā€ and put the following in it:

{ "caption": "-", "id": "file" }, { "command": "open_prompt", "caption": "Open Command Window Hereā€¦" } ]
Now when you right click and click ā€œOpen Command Window Hereā€¦ā€ youā€™ll get a new command prompt at the location of the file you were editing.

0 Likes

#3

Oh, sorry, I see you wanted this from the sidebarā€¦ Just change ā€œContextā€ to ā€œSidebarā€ in the filename.

0 Likes

#4

Saved me some time with this. Thanks!

0 Likes

#5

Hmm, I canā€™t seem to get the Popen command to work on my setup (XP64, dev2122). The item shows up in the context menu enabled, but doesnā€™t seem to act in any way.

Iā€™ve dickered with the Popen command, but I admit little facility with Python libraries. Any ideas what the call might be fragile on?

0 Likes

#6

Yup. Try adding the import os.

import sublime, sublime_plugin, subprocess, os

The problem with loading this from the sidebar is that the working directory is set using self.view.file_name().
What I wanted was a way to use the folder I right clicked on as the current working directory for the command prompt.

0 Likes

#7

[quote=ā€œatomiā€]The problem with loading this from the sidebar is that the working directory is set using self.view.file_name().
What I wanted was a way to use the folder I right clicked on as the current working directory for the command prompt.[/quote]

I seeā€¦

Well the sidebar can pass arguments through I believe, so Iā€™ll have a play :smile:

0 Likes

#8

Ah ha, yes, there it is. Thanks, works like a charm now!

0 Likes

#9

[quote=ā€œjbrooksukā€]

[quote=ā€œatomiā€]The problem with loading this from the sidebar is that the working directory is set using self.view.file_name().
What I wanted was a way to use the folder I right clicked on as the current working directory for the command prompt.[/quote]

I seeā€¦

Well the sidebar can pass arguments through I believe, so Iā€™ll have a play :smile:[/quote]

Yeah that was what was hanging me up.

I see

{ "caption": "Delete Folder", "command": "delete_folder", "args": {"dirs": ]} },

in the Default\Side Bar.sublime-menu but when I add ā€œargsā€ to my Side Bar entry I get this in the console:

Traceback (most recent call last):
  File ".\sublime_plugin.py", line 276, in run_
TypeError: run() got an unexpected keyword argument 'dirs'
0 Likes

#10

Sublime Terminal does what you are looking for, and works cross-platform too: wbond.net/sublime_packages/terminal

You can right click on any folder, and also use ctrl+shift+t to open a terminal/command prompt at the folder of the current file, or ctrl+shift+alt+t to open at the project folder the current file is in.

0 Likes

#11

this works for me, thanks everyone.

open_prompt.py

import sublime, sublime_plugin, subprocess, os

class OpenPromptCommand(sublime_plugin.TextCommand):
    def run(self, edit, paths=]):
        retcode = subprocess.Popen("cmd", "/K", "cd", paths[0]])

Side Bar.sublime-menu


    { "caption": "-", "id": "end" },
    { "caption": "Open Command Window Here", "command": "open_prompt", "args": {"paths": ]} }
]
0 Likes