Sublime Forum

Request: Start ext. application with special parameters

#1

Hi,

can anyone write a simple plugin that does the following:

Start an external application with two parameters depending on the current file type?

I’ve written a small .au3 (AutoIt) script, that uses an .ini file which contains different sections for each known help file entries for the resolution to what the .chm file will be resized, position, the general path for a topic inside the file (like: /idh_scripting_comref.htm#idh_sc_), etc.

It takes (currently) 2 parameters:
1.) Topic that should be displayed
2.) File extension
The rest is already inside the .ini file

So the plugin should execute a line like this:
“” “” “”

Everything else is handled inside the wrapper.

It should take the word that is currently under the cursor or if no [a-zA-Z] is found at the current position it should find & take the first letter left from it and use that word instead.

An example ("|" = current cursor position):

$file = getpathcomponent|

The cursor is not on a keyword (getpathcomponent) so search for the first letter on the left side of the cursor position (“t”) and get the word under it (getpathcomponent again). Why this? I normally type the command and want to see the help file right after typing it.

Just use the extension of the current document without the leading dot.

It would be nice if I can define the keyboard shortcut for the plugin (to execute it) afterwards.

This would allow me / us to hit the same key for displaying a .chm help file with the correct topic, regardless if I’m currently working on a .au3, .ps1, .xys, etc. file.

If anyone is interested, I would post the .au3 file for the wrapper afterwards. If anybody else could make use of such a plugin.

Regards,
Highend

0 Likes

#2

Something like this perhaps?

Menu item Tools->New Plugin, and paste this code:

[code]import sublime, sublime_plugin
import os

class HighendCommand(sublime_plugin.TextCommand):
def run(self, edit, command):
word = self.view.substr(self.view.word(self.view.sel()[0].a))
cmd = “”%s" “%s” “%s”" % (command, word, self.view.file_name()[self.view.file_name().rfind(".")+1:])
print “Running command %s” % cmd
os.system(cmd)
[/code]

Then in your user keybindings:

    { "keys": "/"], "command": "highend", "args": {"command": "/path/to/wrapper.exe"}},
0 Likes

#3

Thank you quarnster!

I’ve added this line to my user keymap:
{ “keys”: “alt+p”], “command”: “highend”, “args”: {“command”: “D:/NavigateCHM.exe”} },

The NavigateCHM.exe is the compiled AutoIt script file. The Navigate.chm contains a MsgBox line at the beginning to show me when it’s executed.

The interesting thing is: The .exe is only executed (after pressing alt+p) under “weird circumstances”:

This is the line I use (it’s in a file named Test.xys)
foreach($task, $processTasks, “”) { sub “_” . $task; }

Three different cursor positions on this line lead to different results when using alt+p:
“|” is the current position

foreach($|task, $processTasks, “”) { sub “_” . $task; }
Console: Running command “D:/NavigateCHM.exe” “task” “xys”
I don’t get the message box from NavigateCHM.exe

foreach|($task, $processTasks, “”) { sub “_” . $task; }
Console: Running command “D:/NavigateCHM.exe” “foreach” “xys”
I don’t get the message box from NavigateCHM.exe

foreach(|$task, $processTasks, “”) { sub “_” . $task; }
Console: Running command “D:/NavigateCHM.exe” “($” “xys”
NavigateCHM.exe is invoked…

This really doesn’t make sense, does it?

P.s.: My last pm hasn’t passed the outbox for over 1,5 hours, so I’ll continue the discussion in this thread.

Regards,
Highend

0 Likes

#4

I don’t know what’s wrong, but try this code instead which will print out the program’s stdout and stderr which should make it easier to debug if it doesn’t work:

[code]import sublime, sublime_plugin
import subprocess

class HighendCommand(sublime_plugin.TextCommand):
def run(self, edit, command):
word = self.view.substr(self.view.word(self.view.sel()[0].a))
args = (command, word, self.view.file_name()[self.view.file_name().rfind(".")+1:])
cmd = “”%s" “%s” “%s”" % args
print “Running command %s” % cmd
p = subprocess.Popen(args, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
stdout,stderr = p.communicate()
print stdout
print stderr
[/code]

0 Likes

#5

print stdout / stderr only create blank lines so it seems there aren’t any errors.

With the new code, NavigateCHM.exe is always started correctly and displays the correct keyword from the associated .chm file.

Thanks a lot for coding this for me, an invaluable help for looking up keywords :smiley:

Regards,
Highend

0 Likes