Sublime Forum

Using window.showQuickPanel to choose a command

#1

If, like me, you have plugins with a lot of commands, and far too many key bindings, consider this little snippet. It pops up the quickpanel, and lets you choose a command to execute.

class ChooseOutputFormatCommand(sublimeplugin.WindowCommand):
  def run(self, window, args):
    items = 
      ("Article (pdf)", "compileToPdf"),
      ("Filofax Pages (pdf)","compileToFilofaxPagePdf"),
      ("Index Cards (pdf)","compileToIndexCardPdf"),
      ("Standard Manuscript Format (pdf)", "compileToSmfPdf"),
      ("Web Page (html)", "compileToHtml"),
      ("Rich Text Format (rtf)", "compileToRtf"),
      ("S5 slideshow (html)", "compileToS5"),
      ("Screenplay (pdf)","compileToScreenplayPdf"),
      ("Book (pdf)","compileToBookPdf"),
      ("Book with pagebreaks (pdf)","compileToBookPbPdf"),
      ("Booklet (pdf)","compileToBookletPdf"),
    ]
    commands = [x for x,y in items]
    names =    [y for x,y in items]
    window.showQuickPanel("", "onOutputChosen", names, commands)
  
class OnOutputChosenCommand(sublimeplugin.WindowCommand):
  def run(self, window, args):
    if len(args) != 1:
      print "%s items selected; expected 1" % len(args)
      return
    command = args[0]
    print "selected %s" % command
    window.activeView().runCommand(command)
0 Likes

#2

Anyone know if there is a way to test if a command is enabled? I’d like to improve that snippet so that it didn’t show disabled commands.

0 Likes

#3

There isn’t; I’ll expose an API function for it in the next beta.

0 Likes

#4

You, sir, are a god.

0 Likes

#5

The canRunCommand method works perfectly. I now have the ability to select a build command based on the current extension. Neat!

0 Likes