##Make “Open File Prompt” part of the API
Importance: minor
Motivation: Some plugins want to process a file, maybe present it after the processing it in some way. There is currently no canonical way to ask for a file from the user on the API side. There is
sublime.run_command('prompt_open_file')
and this opens an “open file” dialog window; but there is no way to retrieve the selected file. See also this thread.
Proposed solution #1 (old):
New function in module sublime
:
sublime.prompt_open_file(on_done, <folder>)
where:
-
on_done
is a function (filepath, window) -> None
that takes two argument and returns None
:
-
- the
filepath
that the user selected. If the user pressed cancel, then filepath
is None
.
-
- the
window
where the open file dialog was started.
-
folder
is an optional argument which, if given, starts the open file dialog in the given folder
. If it is not given, then the starting folder is whatever the starting folder is when you run sublime.run_command('prompt_open_file')
Proposed solution #2 (better):
New function in module sublime
:
sublime.file_selector_dialog(caption, <folder>)
where:
-
caption
is a string for the caption
-
folder
is an optional argument which, if given, starts the open file dialog in the given folder
. If it is not given, then the starting folder is whatever the starting folder is when you run sublime.run_command('prompt_open_file')
The function blocks until the user has selected to open a file or cancelled the operation. If the user selected a file, the filepath
is returned. If the user canceled the operation, None
is returned.
Example usage:
class MyFileProcessorCommand(sublime_plugin.ApplicationCommand):
def run(self):
filepath = sublime.file_selector_dialog('gimme a file!')
if not filepath:
# User pressed "cancel"
return
view = sublime.active_window().new_file()
contents = None
with open(filepath) as f:
contents = f.read()
# process "contents" ...
view.run_command('insert', {'characters': contents})