Sublime Forum

Select File/Folder Dialog with QuickPanel

#1

There was an question in the forum for an user based folder select dialog.
Now I’ve made an PlugIn to select file or folder by using the quick panel.

EDIT:

  • Now it shows only the relative pathes.
  • The full path will shown in the status bar
  • Added: Folder creation (in mode folder-selection)

http://www.imgbox.de/users/BugFix/sublime_sel_folder12.png http://www.imgbox.de/users/BugFix/sublime_sel_file12.png

“SelectFileFolder.py”

'''

PlugIn: SelectFileFolder

- You can navigate, starting with given root directory.

- In mode "folder" you can create a new directory. After creation the PlugIn starts again with the new folder as root.

- as menu item:
    {"caption": "Select File/Folder", "command": "user_select_file_folder", "args": {"root": "C:\\YOUR\\START\\PATH", "select_type": "file"-OR-"folder"}}

- as call from another PlugIn:
    sublime.active_window().run_command("user_select_file_folder",
        {"root": "C:\\YOUR\\START\\PATH", "select_type": "file"-OR-"folder"})

- the selected value is available with:
    file_selected = sublime.load_settings(sel_file_folders.sublime-settings).get('file')
    folder_selected = sublime.load_settings(sel_file_folders.sublime-settings).get('folder')

'''


import sublime, sublime_plugin
import os, os.path

SETTINGS_FILE = "sel_file_folders.sublime-settings"
settings = None


class UserSelectFileFolderCommand(sublime_plugin.WindowCommand):
    global settings
    return_type = None
    list_selection = None
    path_current = None
    up = '__UP__'                             # can be modified here
    new_folder = '__CREATE_FOLDER__'          # can be modified here
    this_folder = '__SELECT_FOLDER: '         # can be modified here
    user_prompt_create = "New Folder Name = " # can be modified here

    def run(self, root, select_type):
        if not select_type in ('file', 'folder'):
            self.save_settings('file', '')
            self.save_settings('folder', '')
            return
        self.return_type = select_type
        self.list_selection = self.get_file_folder(root)
        sublime.active_window().show_quick_panel(
            self.list_selection, self.on_selection, sublime.MONOSPACE_FONT, -1,
            self.on_highlighted)

    def get_file_folder(self, path):
        self.path_current = path
        listing = os.listdir(path)
        list_folder, list_file, list_return = ], ], None
        for f in listing:
            pathname = os.path.join(path, f)
            if os.path.isdir(pathname):
                list_folder.append(os.path.split(pathname)[1])
            elif os.path.isfile(pathname):
                if self.return_type == 'file': list_file.append(os.path.split(pathname)[1])
        list_folder.sort()
        if self.return_type == 'file':
            list_file.sort()
            list_return = list_folder + list_file
        else:
            list_return = [self.new_folder, (self.this_folder + os.path.split(path)[1])] + list_folder
        return [self.up] + list_return

    def on_selection(self, selection):
        item_selected = self.item_path(self.list_selection[selection])
        sel_is_folder = (os.path.isdir(item_selected) == True)
        if selection > -1:
            if self.return_type == 'file':   # FILE selection
                if sel_is_folder:
                    sublime.set_timeout_async(lambda:
                        sublime.active_window().run_command("user_select_file_folder",
                        {"root": item_selected, "select_type": self.return_type}), 0)
                else:
                    if item_selected == self.up:
                        sublime.set_timeout_async(lambda:
                            sublime.active_window().run_command("user_select_file_folder",
                            {"root": os.path.split(self.path_current)[0], "select_type": self.return_type}), 0)
                    else:
                        self.save_settings('file', self.path_current)
                        print('SELECTED FILE:', item_selected)
            else:                            # FOLDER selection
                if item_selected:len(self.this_folder)] == self.this_folder:
                    self.save_settings('folder', self.path_current)
                    print('SELECTED FOLDER:', self.path_current)
                elif item_selected == self.up:
                    sublime.set_timeout_async(lambda:
                        sublime.active_window().run_command("user_select_file_folder",
                        {"root": os.path.split(self.path_current)[0], "select_type": self.return_type}), 0)
                elif item_selected == self.new_folder:
                    self.create_folder()
                else:
                    sublime.set_timeout_async(lambda:
                        sublime.active_window().run_command("user_select_file_folder",
                        {"root": item_selected, "select_type": self.return_type}), 0)
        else:
            self.save_settings('file', '')
            self.save_settings('folder', '')

    def on_highlighted(self, selection):
        if self.list_selection[selection] == self.up:
            sublime.status_message(os.path.split(self.path_current)[0])
        elif self.list_selection[selection]:len(self.this_folder)] == self.this_folder:
            sublime.status_message(self.path_current)
        elif self.list_selection[selection] != self.new_folder:
            sublime.status_message(os.path.join(self.path_current, self.list_selection[selection]))

    def item_path(self, item):
        if item == self.up or \
        item:len(self.this_folder)] == self.this_folder or \
        item == self.new_folder:
            return item
        else:
            return os.path.join(self.path_current, item)

    def create_folder(self):
        sublime.active_window().show_input_panel(self.user_prompt_create,
            "", self.on_new_folder, None, self.on_escape)

    def on_new_folder(self, user_input):
        re_call_path = self.path_current
        if user_input != '':
            folder = os.path.join(self.path_current, user_input)
            if not os.path.exists(folder):
                os.makedirs(folder)
            re_call_path = folder
        sublime.set_timeout_async(lambda:
            sublime.active_window().run_command("user_select_file_folder",
            {"root": re_call_path, "select_type": "folder"}), 0)

    def on_escape(self):
        sublime.set_timeout_async(lambda:
            sublime.active_window().run_command("user_select_file_folder",
            {"root": self.path_current, "select_type": "folder"}), 0)

    def save_settings(self, name, value):
        settings.set(name, value)
        sublime.save_settings(SETTINGS_FILE)



def plugin_loaded():
    global settings
    settings = sublime.load_settings(SETTINGS_FILE)
1 Like

Change how the show_quick_panel truncates strings or truncate strings by getting quick panel width
Displaying collapsible tree structure for a json data on a panel
Change how the show_quick_panel truncates strings or truncate strings by getting quick panel width