2

I have a some python script called spc.py somewhere on the disk, which is for processing text file in some way (and it uses more external libraries). Now I call it from console with passing a filename as argument.

Because I'm running this script often, during the editing the document itself, I would like to be able to call the script straight from the Sublime Text, passing active file as argument, so I can call it by one click without leaving the window.

But I haven't found a way how to call external python script from Sublime Text plugin. Can you give me a clue? Is that even possible?

atomic44
  • 179
  • 1
  • 9
  • I just posted an answer with 2 related solutions I've written. I'm not sure exactly what you're attempting to do, as the question is slightly vague. If you update your question with a [**MCVE**](http://stackoverflow.com/help/mcve), I'll tailor the answer to your specific case. – Enteleform May 11 '16 at 02:32
  • @Enteleform You give me the right answers, because the real problem was than I didn't know how to get out from the internal Sublime Text python interpreter to my interpreter that I'm using. I've post the exact solution I have found according to your second recommendation. – atomic44 May 15 '16 at 10:07

3 Answers3

2

One of way to doing this is by calling python with arguments through cmd.exe using subprocess module, with sending the file name of active file as argument for python program.

import sublime, sublime_plugin
import subprocess

class ExecuteExternalProgramCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        exec_file = "C:\Path\To\My\Program\spc.py"
        command  = ["cmd.exe", "/c", "python", exec_file, self.view.file_name()]
        subprocess.Popen(command)

Save it to your Sublime Text User plugins folder and then put keyboard shortcut to your User Keymap, for example

{"keys": ["ctrl+shift+e"], "command": "execute_external_program"},
atomic44
  • 179
  • 1
  • 9
1

You can:

  • Create a custom build system which accepts the current file as an argument.
  • Create a plugin which executes text from the current file.
Community
  • 1
  • 1
Enteleform
  • 3,753
  • 1
  • 15
  • 30
0

You can use the answer provided here to get the file name of the current sublime window. Now beyond that you just need a method that accepts a path as an input argument and from there do what you need to do.

import sublime, sublime_plugin
import re, os, os.path

class OpenrelCommand(sublime_plugin.WindowCommand):
    def run(self):
    print self.active_view()
Community
  • 1
  • 1
kpie
  • 9,588
  • 5
  • 28
  • 50