Sublime Forum

Run external program

#1

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:

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

Is this possible?
Thank you for your help!

0 Likes

#2

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:

{
	"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”)

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)

	{ "keys": "f7"], "command": "run_preditor" }
0 Likes

#3

Thank you, with a little of adjustments this works for me:

[code]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\\', ''))[/code]
0 Likes

#4

If this still is a problem and you’re using Sublime Text 3 than you can use the plugin ExternalTools (which I’ve created) and which gives you the ability to open any any external application with an key binding or via the command palette.
If you encounter any problem I would be pleased if you can give me an feedback here.

0 Likes

#5

Hi, Does this file regex is using for identify file extension ?

0 Likes