I don't know how but I'm writing!

Rant: In Sublime, Window Commands != Application Commands

Consider this diff:

--- Unsaved view (172)
+++ /Users/drew/.local/share/chezmoi/dot_config/sublime-text/Packages/User/dwm_tab.py
@@ -182,5 +182,5 @@
         variables = collect_env_variables()
         variables.update(collect_sublime_variables(self.window))
         dirs = [sublime.expand_variables(dn, variables) for dn in dirs]
-        sublime.run_command("open_dir", {"dir": dirs[0]}, )
+        self.window.run_command("open_dir", {"dir": dirs[0]}, )

The red text doesn't work, but the green text does.

proceeds to throw computer out of window

The Sublime Text API Reference only ever refers to 'commands', but in the actual API there are classes such as ApplicationCommand, WindowCommand, TextCommand, etc. that all subclass a common Command. The names of these commands are (bizarrely) derived from mutating the class name, so MyCoolApplicationCommand gets turned into my_cool_application when referencing from other places. There are a few theories here:

  1. open_dir is not a valid ApplicationCommand, and sublime.run_command only runs application commands. So calling sublime.run_command ("open_dir", ...) is invalid because open_dir isn't actually a 'real' command, it can't find the OpenDirCommand class you're referring to. Essentially a namespace error.

  2. There are extra special arguments that get passed to the command when running it from a window that you don't see. This causes the first invocation to fail while the second succeeds.

Either way, the original code fails silently, not even giving you console output or any return value indicating that something is wrong. Also, enabling command logging with sublime.log_commands() shows that the invocation for both lines is exactly the same!

I had to discover this by randomly trying things I saw in example files. That sucks!

EDIT: After re-reading the API reference, it does actually say that sublime.run_command only runs ApplicationCommands. So (1) was correct. However, I can't find anywhere a list of valid application commands, only this out-of-date page that doesn't list open_dir. Cool!