Sublime Forum

Running scripts wih sublimetext, advice needed

#1

hello,

I want to run a collection of scripts from ST via key binding, menu items. I see that commands must be implemented within “packages” folder, as a sublime plugin.

Also, some scripts require user input. What is the easiest way to input a text string, that serves as argument before executing a command?

I am new to sublime plugins, I think I’d prefer not to use API from ST and scripting stuff outside of ST. Plus, some scripts are part of a project and should stay there. I am not sure if I should do it this way, though.

It’s fairly easy to get started, I’d just like to ask for some comments, tips before diving deeper into the matter.

thanks

0 Likes

#2

It’s sort of unclear what you’re asking here. Python files that are in packages are plugin resource files, not arbitrary user scripts.

A way of thinking of this is that Sublime has a Python interpreter built in, but:

  • If you thought that installing Sublime was all of the tools that you needed in order to create plugin commands and packages for Sublime Text, this is indeed the case.

  • If you thought that because you installed Sublime, you can now run Python programs, you cannot; you need to install Python to do so (if it isn’t already installed).

With that in mind:

This is possible; Sublime has an internal exec command for doing this. It’s the command that is used to execute a build. So for example if you make sure that Tools > Build System is set to Automatic or Python, you can open a Python script, select Tools > Build (or the associated key, see the menu), and Sublime will run your program externally.

To execute a program in response to a key binding, you would need to use the exec command directly in the key binding. For example:

    {
        "keys": ["super+alt+t"],
        "command": "exec",
        "args": {
            "shell_cmd": "python /home/tmartin/test.py",
            "working_dir": "/home/tmartin",
            "encoding": "utf-8",
        },
    },

The same command could also be applied to a menu item as well, if desired.

Note that console programs run by the exec command (so either this way or by the build process) will display their output in the output panel in Sublime but you can’t interact with them. If you run a program that needs to be interacted with, when it reaches a point where it’s waiting for you to enter text it will be effectively hung.

To run such a program you need to make the command that you exec also open a new console window and then execute the program inside of that terminal.

This is true, but it doesn’t really relate to the previous sentence. In this context, a command is something that you would define to add new functionality to Sublime. In theory you could create a plugin command to run your program, but as just mentioned Sublime already provides an exec command for doing this, so this is not really needed.

I covered this in your other thread, so verbose though I may be, I won’t put you through having to read it again. :wink:

Sublime’s API is only available to the Python code that it loads from plugins in packages; Python scripts in general (wether you run them from Sublime or not) don’t have access to it because the interpreter in Sublime isn’t used to run external programs.

You shouldn’t put any non-plugin Python files in your package unless you intended them to be a part of your package. Generally that means that unless you’re including support code for a plugin that you’re writing, you don’t want to put those sorts of files there.

1 Like