Sublime Forum

Color Schemes as JSON

#1

Just a simple question.

Is it possible to format Sublime ColorSchemes as JSON?
If so, could you tell me how is the structure?

Thanks!

0 Likes

#2

You could easily create a good plugin to do this. Here is a thrown together sloppy plugin you could build off of.

[code]import sublime
import sublime_plugin
import json
import StringIO
from plistlib import readPlist, writePlistToString

class PlistToJsonCommand(sublime_plugin.TextCommand):
def run(self, edit):
f = StringIO.StringIO(
self.view.substr(
sublime.Region(0, self.view.size())
).encode(‘utf8’)
)
self.view.replace(
edit,
sublime.Region(0, self.view.size()),
json.dumps(
readPlist(f), indent=4, separators=(’,’, ': ')
)
)

class JsonToPlistCommand(sublime_plugin.TextCommand):
def run(self, edit):
j = json.loads(
self.view.substr(
sublime.Region(0, self.view.size())
)
)
self.view.replace(
edit,
sublime.Region(0, self.view.size()),
writePlistToString(j)
)
[/code]

Load the color scheme and then execute the PlistToJson command, work with the data as a json. When you get done, run the JsonToPlist command and then save. I think AAAPackageDev has something much better than this, but I have never used it; I usually work with them as plists. Anyways, the code I posted is not meant to be a completed thing, though it works.

I would try out AAAPackageDev, or if that doesn’t do what you want, you can build on this concept. You could have it do something crazy like detect if a file is a plist on file open and then convert it to json, and when you save it, convert it back to plist. I don’t know, I am just throwing out stuff, but what you are asking is easily doable.

Edit: Updated code to work completely from the content in the view; no need to open the file. Assumes plist is always UTF8.

0 Likes

#3

Wow facelesuser your awesome <3

I’m just a designer - coder converted to Sublime but I’ll see what I can do.
Thanks a lot!

0 Likes

#4

One more thing. Added change file syntax accordingly on conversion. If you are using a non-standard syntax, you could make it look where you want via a sublime-settings file.

[code]import sublime
import sublime_plugin
import json
import StringIO
from plistlib import readPlist, writePlistToString

JSON_SYNTAX = “Packages/Javascript/JSON.tmLanguage”
PLIST_SYNTAX = “Packages/XML/XML.tmLanguage”

class PlistToJsonCommand(sublime_plugin.TextCommand):
def run(self, edit):
f = StringIO.StringIO(
self.view.substr(
sublime.Region(0, self.view.size())
).encode(‘utf8’)
)
self.view.replace(
edit,
sublime.Region(0, self.view.size()),
json.dumps(
readPlist(f), indent=4, separators=(’,’, ': ')
)
)
self.view.set_syntax_file(JSON_SYNTAX)

class JsonToPlistCommand(sublime_plugin.TextCommand):
def run(self, edit):
j = json.loads(
self.view.substr(
sublime.Region(0, self.view.size())
)
)
self.view.replace(
edit,
sublime.Region(0, self.view.size()),
writePlistToString(j)
)
self.view.set_syntax_file(PLIST_SYNTAX)
[/code]

Okay, that is it.

0 Likes