Sublime Forum

Run Multiple Commands.. Command

#21

I tried. The first command works, the save, but the next command is not executed…

I’m using ST3, I don’t know if can be a compatibility issue.

1 Like

#22

Can you please be more specific?

I’m sorry, this is a big thread. I though it was a orphan question.

1 Like

#23

With “context”: “window”, in both commands, it works, sorry. You should make a package of it, the Chain of Command package doesn’t work like this.

Thanks!

1 Like

#24

@jjforums, I had made some fixes to this plugin and it is available at github.com/skuroda/ImprovedMacros. I had originally intended to find some way to replace the built in macro functionality, but never figured out a good way to record all commands. Anyways, run_multiple_commands command (and a file variant) are bundled in the previously mentioned package. Not part of package control, since it’s incomplete to me, but you can add it as a repository, and it will auto update as if it were in package control.

1 Like

#25

This is a very useful tool and I appreciate the efforts…so a big thank you!
Quick question on this…I have it working for what I need thus far…with one minor issue - after running through my third command string (which is referencing the “Goto” overlay I have a manual entry to make so I set a delay; however i can’t get the commands to pick back up and execute. I’m assuming it’s because I’ve flipped into the “Goto” overlay and that’s the termination factor here.
Below is my code and what I’m trying to get it to do per the “Multiple Commands” Pluggin:
{ “keys”: “ctrl+shift+m”], “command”: “run_multiple_commands”,
“args”: {
“commands”:
{“command”: “instant_file_search”, “context”: “window”, “delay”: 0},
{“command”: “type_criteria”, “context”: “window”, “delay”: 0},
{“command”: “show_overlay”, “args”: {“overlay”: “goto”, “text”: “:”}, “context”: “window”, “delay”: 3500},
{“command”: “move_to”, “args”: {“to”: “eol”, “extend”: true}, “context”: “window” },
{“command”: “move”, “args”: {“by”: “pages”, “forward”: true, “extend”: true},“context”: “window” },
]
}}

Any help/suggestions on this would be wonderful! :smiley:

1 Like

#26

@woodring, Are you expecting type_criteria to run, then wait 3.5 seconds for entry? If the goto panel is where you are expecting to do the manual entry, try moving the delay to the next command. I tried a simplified version, using the 4th and 5th commands and the delay on the second. It seemed to work okay.

1 Like

#27

Very useful code!

Fine for quickly executing more than one command by a keyboard short-cut without the need to write a script for that.

Thank you, Nilium!

1 Like

#28

I was looking for something exactly like this and had given up.

Thanks much for taking the time to write this bit of code.

1 Like

#29

How can I make sure this plugin is loading & working?
I added it to the appropriate folder, but when I try to use it to execute a pre-recorded macro from a file just nothing happens - no error, nothing. I tried to use it with the example commands from github, again - nothing happens.

1 Like

#30

in the console:
sublime.log_commands(True)

1 Like

#31

I have a Portable version of ST3

where do you put run_multiple_commands.py? I tried in the root repo, in Packages, and in Data/Packages/User, but couldn’t make it work

1 Like

#32

A slightly optimized version given that this is my most often used command.

[code]import sublime, sublime_plugin

Takes an array of commands (same as those you’d provide to a key binding) with

an optional context (defaults to view commands) & runs each command in order.

Valid contexts are ‘text’, ‘window’, and ‘app’ for running a TextCommand,

WindowCommands, or ApplicationCommand respectively.

class RunMultipleCommandsCommand(sublime_plugin.TextCommand):

ctx_obj_map = {
‘window’: lambda x: x.window(),
‘app’: lambda x: sublime,
‘text’: lambda x: x,
None: lambda x: x,
}

def exec_command(self, command):

# default context is the view since it's easiest to get the other contexts
# from the view
context = RunMultipleCommandsCommand.ctx_obj_map[command.get('context')](self.view)

# skip args if not needed
if 'args' in command:
  self.view.window().run_command(command'command'], command'args'])
else:
  self.view.window().run_command(command'command'])

def run(self, edit, commands = None):
print (‘running commands:’, commands)
if commands is None:
return # not an error
for command in commands:
self.exec_command(command)
[/code]

1 Like

#33

anyone tried the ImprovedMacros package on github? I installed it manually, but it has no effect, using sublime 3 v3083, in console I saw

reloading plugin ImprovedMacros,ImprovedMacros

and there is no error. but it’s not showing in Preferences -> package settings menu. and my added hotkey does not work. I added the hotkey example in readme.

1 Like

#34

Assuming you meant this, it does not add anything to the UI, so that would be expected.

2 Likes

#35

Hey guys, I can’t get this to work!
After installing (“New Plugin” > paste > save as “run_multiple_commands.py”) I get this in the console:
`> reloading plugin User.run_multiple_commands

Traceback (most recent call last):
File “/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py”, line 78, in reload_plugin
m = importlib.import_module(modulename)
File “./importlib/init.py”, line 90, in import_module
File “”, line 1584, in _gcd_import
File “”, line 1565, in _find_and_load
File “”, line 1532, in _find_and_load_unlocked
File “”, line 584, in _check_name_wrapper
File “”, line 1022, in load_module
File “”, line 1003, in load_module
File “”, line 560, in module_for_loader_wrapper
File “”, line 853, in _load_module
File “”, line 980, in get_code
File “”, line 313, in _call_with_frames_removed
File “/Users/MYNAME/Library/Application Support/Sublime Text 3/Packages/User/run_multiple_commands.py”, line 14
args = command’args’]
^
SyntaxError: invalid syntax
/Users/MYNAME/Library/Application Support/Sublime Text 3
LOADING BOOKMARKS`

Any ideas?
Thanks!
Louis

1 Like

#36

It seems during the migration from phpbb to Discourse, the code got messed up. There are a couple instances where opening [ braces are missing.


Either way, imo a command like this should be built-in. We have macros, but those only allow text commands and can iirc only be executed by referencing them in a different file. If this was to be added in one form or another, I’d also like to request the following:

  • a “command” parameter for providing a command name; window, text or application command
  • a “args” parameter for providing arguments to the command call
  • a “repeat” parameter to repeat a command several times
  • a “context” parameter, just like for key bindings
4 Likes

#37

Thank you Nilium for the plugin! I just found this and was having the same problems as Louis. In addition to some leading [ characters being removed in the migration, spacing got ruined as well. (And that’s why making indentation significant in a programming language is a bad idea.) I played around with it and got it working. Here’s a cleaned up version:

# run_multiple_commands.py
import sublime, sublime_plugin
# Takes an array of commands (same as those you'd provide to a key binding) with
# an optional context (defaults to view commands) & runs each command in order.
# Valid contexts are 'text', 'window', and 'app' for running a TextCommand,
# WindowCommands, or ApplicationCommand respectively.
class RunMultipleCommandsCommand(sublime_plugin.TextCommand):
    def exec_command(self, command):
        if not 'command' in command:
            raise Exception('No command name provided.')
        args = None
        if 'args' in command:
            args = command['args']

        # default context is the view since it's easiest to get the other contexts
        # from the view
        context = self.view
        if 'context' in command:
            context_name = command['context']
            if context_name == 'window':
                context = context.window()
            elif context_name == 'app':
                context = sublime
            elif context_name == 'text':
                pass
            else:
                raise Exception('Invalid command context "'+context_name+'".')

        # skip args if not needed
        if args is None:
            context.run_command(command['command'])
        else:
            context.run_command(command['command'], args)

    def run(self, edit, commands = None):
        if commands is None:
            return # not an error
        for command in commands:
            self.exec_command(command)

And if that gets messed up or doesn’t work, I also posted it at https://gist.github.com/bgmort/7ae52ea4270f1c404321c20d1b97733c.

2 Likes

#39

try this:

{
“keys”: [“f7”],
“caption”: “SublimeREPL: Python - RUN current file”,
“command”: “run_multiple_commands”,
“args”:
{ “commands”:
[
{“command”: “save”},
{“command”: “run_existing_window_command”, “args”: {“id”: “repl_python_run”, “file”: “config/Python/Main.sublime-menu”}, “context”: “window”}
]
}
}

0 Likes

#41

a lot of people use https://packagecontrol.io/packages/Chain%20of%20Command these days btw, which has similar simplicity and is installable from Package Control.

1 Like