简体   繁体   中英

Open a file with Open File Dialog via plugin

I have a plugin that I want the ability for the user to choose a file for something. I was thinking it would be cool if my plugin could cause the system open file dialog to appear, the user selects the file and I then get the filename back.

However, I can't seem to find a way to do that in a sublime plugin.

I tried calling open_file with no parameters but if throws an error expecting the filename passed in.

Do you know how I can create the desired behaviour?

Alternatively if I show a footer panel where the user can type the filename, is there a way that gives some sort of tab complete feature on the filenames available?

Thanks

The Sublime Text plugin API does not contain a way to call a file selector dialog box.

open_file(file_name, <flags>) just opens the file_name which you specify in the Sublime Text window.

The iOpener ST3 package - see Package Control and GitHub - does what you are looking for as an alternative. You type a file path (in the input panel at the bottom) and it does a Bash style auto-complete when tab is pressed, if there are more than one complete alternatives it displays the alternatives in an overlay list for you to select which one you want. iOpener is released using the GPL V2 license, so you can use/adapt the (well commented) code in your plugin under the terms of that license. Hope this helps.

这对我有用:

window.run_command('prompt_open_file')

This was implemented in Sublime Text 4075, see the API reference :

open_dialog(callback, <file_types>, <directory>, <multi_select>, <allow_folders>)

Presents the user with a file dialog for the purpose of opening a file, and passes the resulting file path to callback.

Return value

None

Arguments

  • callback : A callback to accept the result of the user's choice. If the user cancels the dialog, None will be passed. If a file is selected, a str containing the path will be passed. If the parameter multi_select is True, a list of str file paths will be passed.

  • file_types : A specification of allowable file types. This parameter should be a list containing 2-element tuples:

    • A str containing a description
    • A list of str with valid file extensions

    Example

    [ ('Python source files', ['py', 'py3', 'pyw']), ('C source files', ['c', 'h']) ]
  • directory : A str of the directory to open the file dialog to. If not specified, will use the directory of the current view.

  • multi_select : A bool to indicate that the user should be allowed to select multiple files

  • allow_folders : A bool to indicate that the user should be allowed to select folders

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM