Home Download Buy Blog Forum Support

Run external program

Run external program

Postby zdenekkostal on Wed Sep 07, 2011 12:28 pm

Hello,
I need to run external program on some shortcut.

For example instead of build I want to call this (Windows 7, 64 bit), when I press F7:

Code: Select all
c:/Web/tools/preditor/Preditor.exe /UPDATE mars/mars@P2G %1 - where %1 = actual file path

Is this possible?
Thank you for your help!
zdenekkostal
 
Posts: 2
Joined: Wed Sep 07, 2011 12:22 pm

Re: Run external program

Postby atomi on Wed Sep 07, 2011 5:34 pm

You can run that using a sublime-build file.
There are a few of them in the Data\Packages\ folders to see how they work.
Example Ant.sublime-build:
Code: Select all
{
   "cmd": ["ant"],
   "file_regex": "^ *\\[javac\\] (.+):([0-9]+):() (.*)$",
   "working_dir": "${project_path:${folder}}",
   "selector": "source.java",

   "windows":
   {
      "cmd": ["ant.bat"]
   }
}

replace cmd: ["ant.bat"] with the command you'd like to run and set the appropriate selector


Or you can create a plugin and call it using a key binding in (in this example command:"run_preditor")

Code: Select all
import sublime, sublime_plugin, subprocess, os

class RunPreditor(sublime_plugin.EventListener):
   def run(self, edit):
        if os.name == "nt":
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

        process = subprocess.Popen(('Preditor.exe','/UPDATE', 'mars/mars@P2G', '%1', '-', 'where', '%1=', view.file_name()),
        stdin=subprocess.PIPE, stdout=subprocess.PIPE, startupinfo=startupinfo)

(Note: quick cut and paste, I haven't tested this you will probably need to tweak the Popen arguments)


Code: Select all
   { "keys": ["f7"], "command": "run_preditor" }
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: Run external program

Postby zdenekkostal on Thu Sep 08, 2011 7:17 am

Thank you, with a little of adjustments this works for me:
Code: Select all
import sublime, sublime_plugin, subprocess, os

class RunPreditor(sublime_plugin.EventListener):
   def on_post_save(self, view):

     if not view.file_name().strip().startswith('C:\\temp\\'):
       return

     if os.name == "nt":
       startupinfo = subprocess.STARTUPINFO()
       startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

     process = subprocess.Popen(('c:/Web/tools/preditor/Preditor.exe', '/UPDATE', 'mars/mars@P2G', view.file_name()),
     stdin=subprocess.PIPE, stdout=subprocess.PIPE, startupinfo=startupinfo)
     print('PREDITOR UPDATED: ' + view.file_name().replace('C:\\temp\\', ''))
zdenekkostal
 
Posts: 2
Joined: Wed Sep 07, 2011 12:22 pm


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot] and 30 guests