Sublime Forum

Set user preferences from command line?

#1

I’m trying to write a bash script to automate switching between the day/night versions of my favorite text editing themes. For instance, in Apple’s Terminal.app, I can

osascript -e "tell application \"Terminal\"" -e "set theSettingsSet to settings set named \"Pro\"" -e "set current settings of (every tab whose current settings is default settings of application \"Terminal\") of (every window whose visible is true) to theSettingsSet" -e "set default settings to theSettingsSet" -e "end tell"

to (1) switch every default-themed window to a new theme, and (2) change the default for subsequently created tabs.

In Sublime 2, I can

subl --command "set_setting {\"setting\":\"color_scheme\",\"value\":\"Packages/Color Scheme - Default/Amy.tmTheme\"}"

which changes the theme of the frontmost view, but doesn’t touch other views or Packages/User/Preferences.sublime-settings. According to Commands — Sublime Text Unofficial Documentation, this is as intended:

[quote]set_setting
Set the value of a setting. This value is view-specific.[/quote]

However, the GUI command Preferences > Color Scheme does exactly what I want. So while I suppose I could call

osascript -e "tell application \"System Events\"" -e "tell menu item \"Amy\" of menu \"Color Scheme\" of menu item \"Color Scheme\" of menu \"Preferences\" of menu item \"Preferences\" of menu \"Sublime Text 2\" of menu bar item \"Sublime Text 2\" of menu bar 1 of process \"Sublime Text 2\" to click" -e "end tell"

, this is is totally brittle (breaks in different localizations or if UI labels change between versions, requires user opt-in to Accessibility controls) and completely useless on Windows and Linux. Is there a native way to access and alter this setting?

Thanks for reading!

0 Likes

#2

I haven’t tried it, but check out NightCycle from Package Control. It’s for ST2 only, but it looks like that’s what you’re using.

0 Likes

#3

Thanks! I checked out the source, and it was pretty helpful to see the API calls in context. I wrote a plug-in that I can invoke from bash with “subl --command”, using the sublime.load_settings(), sublime.save_settings(), settings.get(), and settings.set() methods. Spiffy!

Next question: I’d like to be able to test whether my plugin is installed, and offer to install it for the user if not. Unfortunately, subl --command “my_command” exits with status 0 whether or not “my_command” is present. Is there a way I can ask sublime what plugins or commands it has installed (without installing anything that isn’t in the basic distribution, i.e. not Package Control)?

0 Likes