Sublime Forum

Is there any AppleScript (or literally any language) support for accessing the python console from outside Sublime?

#1

I read a post from maybe four years ago wondering if there is any AppleScript support for Sublime. I’ve been scouring the interwebs trying to figure out if this has been added yet, it seems like the answer is no. But it doesn’t even have to be applescript. Can we do it in ruby, python, bash, perl…anything. It feel like I’m walking around an impenetrable clear glass dome and I can see all these python commands being run perfectly in a shiny new python console just begging to be scripted from the outside but there’s no way for me to get in. Is there any workaround that exists for accessing the many working python commands that exist happily inside sublime from outside using any language?

1 Like

#2

you can execute subl with the --command parameter, followed by the name of the command you want to execute. As for returning results, probably you would need to write a command that would write to a file or something.

0 Likes

#3

thanks kingkeith! This seems like it’s promising. How do I chain multiple commands together, like for example:

open up sublime -> create a new file -> save that file

I’ve tried:

~ sublime test --command 'new_file save'
~ sublime test --command 'new_file && save'
~ sublime test --command 'new_file,save' 

Also how do I pass arguments properly when using the command line? For example:
From the console this will work:

window.run_command("advanced_new_file_new",{"initial_path": "/Users/max/test/test2/hello"})

But from the terminal:

 sublime test --command "advanced_new_file_new,{'initial_path': '/Users/max/test/test2/hello'})"

gives the following error:

Unable to parse command: advanced_new_file_new,{'initial_path': '/Users/max/test/test2/hello'})
0 Likes

#4

you can execute multiple commands by specifying the --command parameter multiple times. i.e.

subl --command new_file --command save
0 Likes

#5

I think that it is unfortunately not possible to pass arguments to commands via the command line. A workaround would be to create your own command that will read from a predetermined/hard-coded filename, and then execute the commands stored in that file.

1 Like

#6

Oh man @kingkeith! This feels like progress :smile:

import sublime, sublime_plugin
    
class SpeakingToSublime(sublime_plugin.WindowCommand):
    def run(self):
        self.window.run_command("advanced_new_file_new",{"initial_path": "/Users/max/test/test2/"})

then from the terminal:

sublime test --command "speaking_to_sublime"

takes us right there:

So the next step is to try to figure out how to edit speaking_to_sublime.py which I’m working on but any help would be greatly appreciated :slight_smile:

0 Likes

#7

@kingkeith @mbigras

You can pass parameters to commands via CLI, it’s just not so trivial to discover (even though I knew it was possible I had to search for quite a while until I found it again). To be fair, I for the longest time I too thought it wasn’t possible.

5 Likes

#8

ah nice! It would be nice if subl --help mentioned that - I read it as only allowing a command name:

>subl --help
Sublime Text build 3113

Usage: subl [arguments] [files]         edit the given files
   or: subl [arguments] [directories]   open the given directories

Arguments:
  --project <project>: Load the given project
  --command <command>: Run the given command
  -n or --new-window:  Open a new window
  -a or --add:         Add folders to the current window
  -w or --wait:        Wait for the files to be closed before returning
  -b or --background:  Don't activate the application
  -s or --stay:        Keep the application activated after closing the file
  -h or --help:        Show help (this message) and exit
  -v or --version:     Show version and exit

Filenames may be given a :line or :line:column suffix to open at a specific
location.
0 Likes

#9

so this works:

subl --command "insert {\"characters\": \"hello world\"}"

but this doesn’t:

subl --command "insert {'characters': 'hello world'}"

the reason why is because although apostrophe characters can be used for strings in Python, this argument needs to be valid JSON.

4 Likes

#10

Man, this is pretty exciting stuff because I’m pretty sure that writing scripts that can interface with alfred isn’t too far off! Check it out, creating files from the outside using FuzzyFileNav, @FichteFoll’s proper syntax recommendations and @kingkeith’s insert command example:

sublime test --command "advanced_new_file_new {\"initial_path\": \"/Users/max/test/test2/\"}" --command "insert {\"characters\": \"hello_world.md\"}"

Now we just need the infamous @deanishe to get stoked on it and a new Alfred workflow could be coming out soon :slight_smile:

0 Likes

#11

I’ve been trying to run the sublime text commands from the cli…from ruby (using backticks) but haven’t had any luck so far. I think it might be because I’m not escaping my quotes properly but I’m not sure. @FichteFoll and @kingkeith what do you guys think?

# puts `ls`

# puts "Enter Filename:"
# file_name = gets.chomp

puts "Opening in sublime"
# passing arguments and escaping quotes like on command line
# `sublime /Users/max/repos/git-note --command "advanced_new_file_new {\"initial_path\": \"/Users/max/repos/git-note/#{file_name}\"}"`

# passing arguments and not escaping quotes
# `sublime /Users/max/repos/git-note --command "advanced_new_file_new {"initial_path": "/Users/max/repos/git-note/#{file_name}"}"`

# no arguments and escaping quotes like on command line
# `sublime /Users/max/repos/git-note --command "advanced_new_file_new {\"initial_path\": \"/Users/max/repos/git-note/hello_world.md\"}"`

# all quotes escaped 
# `sublime /Users/max/repos/git-note --command \"advanced_new_file_new {\"initial_path\": \"/Users/max/repos/git-note/hello_world.md\"}\"`

so far none of these have worked

0 Likes

#12

I couldn’t find docs on how escaping inside backticks works in Ruby, but I assume you need to double-escape the backslashes within the JSON data segment, i.e. as follows:

`sublime /Users/max/repos/git-note --command \"advanced_new_file_new {\\"initial_path\\": \\"/Users/max/repos/git-note/hello_world.md\\"}"`

Other than that, try system:

system("sublime", "/Users/max/repos/git-note", "--command", 'advanced_new_file_new {"initial_path": "/Users/max/repos/git-note/hello_world.md"}')
0 Likes