Home Download Buy Blog Forum Support

$file not recognized as a part of exec in shortcut

$file not recognized as a part of exec in shortcut

Postby nikan on Fri Apr 19, 2013 3:37 pm

Hello all,

I'm trying to add a shortcut for running the current file in the other tool and I add this to my Default (Windows).sublime-keymap:

{ "keys": ["ctrl+shift+alt+d"], "command": "exec", "args": { "cmd": ["C:\\Program Files (x86)\\Decoda\\Decoda.exe", "$file"], "shell": true } }

but when I press the shortcut the app opens without file and I see in console that $file was not substituted:

Running C:\Program Files (x86)\Decoda\Decoda.exe $file

Am I doing something wrong?

Thanks in advance,
Anton.
nikan
 
Posts: 2
Joined: Fri Apr 19, 2013 3:32 pm

Re: $file not recognized as a part of exec in shortcut

Postby skuroda on Fri Apr 19, 2013 7:43 pm

I believe the variable substitution occurs before it gets passed to the exec command. You will need to write a simple plugin to convert the variables before passing it to the exec command. Hope that helps.
skuroda
 
Posts: 677
Joined: Fri Oct 05, 2012 8:11 am

Re: $file not recognized as a part of exec in shortcut

Postby nikan on Mon Apr 22, 2013 12:23 pm

Yeah, had to come up with this:
Code: Select all
import sublime, sublime_plugin, subprocess

class RunDecodaCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window.active_view()
        if view and view.file_name():
            command = "C:\Program Files (x86)\Decoda\Decoda.exe"
            result = subprocess.call([command, view.file_name()], shell=True)
            if result != 0:
                view.set_status('run_decoda', "run_decoda: process %s launching error %d" % (command, result))
            else:
                view.set_status('run_decoda', "run_decoda: process launched...")

            sublime.set_timeout(self.clear,10000)
        else:
            sublime.error_message("No file to open!")

    def clear(self):
        view = self.window.active_view()
        view.erase_status('run_decoda')


and launching key binding:
Code: Select all
{ "keys": ["ctrl+shift+alt+d"], "command": "run_decoda" }
nikan
 
Posts: 2
Joined: Fri Apr 19, 2013 3:32 pm

Re: $file not recognized as a part of exec in shortcut

Postby dmikalova on Mon Jun 03, 2013 10:10 pm

The easiest solution I found for this is to use the build system as a proxy for exec, which then lets you use $file, $file_name, etc.

Go to Tools > Build System > New Build System, copy and paste your old build system into here, or just edit your old build system, and then add a variants section, give it a name, and all the other commands you want. Then add a new keyboard shortcut and add an args with the variant name.

All in all:
David-Go.sublime-build
Code: Select all
{
  "shell": ["true"],
  "cmd": ["sublime-go-build '$file' '$file_name' '$file_path'"],
  "selector": "source.go",
  "variants": [
    {"shell": ["true"],
      "cmd": ["sublime-go-test '$file' '$file_name' '$file_path'"],
      "name": "Run"}]
}

User/Default (Linux).sublime-keymap
Code: Select all
   { "keys": ["ctrl+b"], "command": "build" },
   { "keys": ["ctrl+shift+b"], "command": "build", "args": {"variant": "Run"} }

Now when I press ctrl+b with a .go file open it runs the build command which executes my bash script "sublime-go-build" which is in my system's $PATH with three arguments, $1=$file, etc. If I press ctrl+shift+b it does the same thing with "sublime-go-test" instead. You can add as many variants as you want and set them to whatever keyboard shortcuts that you want. One advantage is that now you can run this only on the language files you want, one disadvantage is that you have to add this to every languages sublime build file.
dmikalova
 
Posts: 4
Joined: Tue May 21, 2013 10:43 pm


Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 27 guests