Home Download Buy Blog Forum Support

Set user preferences from command line?

Set user preferences from command line?

Postby ril on Wed Dec 25, 2013 12:44 am

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

Code: Select all
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

Code: Select all
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:

set_setting
Set the value of a setting. This value is view-specific.

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

Code: Select all
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!
ril
 
Posts: 3
Joined: Thu Aug 22, 2013 12:32 am

Re: Set user preferences from command line?

Postby MattDMo on Fri Dec 27, 2013 9:17 pm

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.
MattDMo
 
Posts: 36
Joined: Thu Jan 17, 2013 3:15 am
Location: near Boston

Re: Set user preferences from command line?

Postby ril on Wed Jan 01, 2014 10:44 am

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)?
ril
 
Posts: 3
Joined: Thu Aug 22, 2013 12:32 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 22 guests