Sublime Forum

Change background color based on open file path

#1

Hello,

I wrote a plugin to automate some task but I want to add the feature of changing the background colour of sublime text if the opened file is in a specified folder.

Can anyone help me to find out how to do this ?

Thank you

0 Likes

#2

Background color is defined in color scheme so your request is to create and change color scheme on-the-fly.

0 Likes

#3

Is it possible ?

0 Likes

#4

yes, it’s dirty though imho. but it’s the only way.

0 Likes

#5

Can you give me some clue of how to do it please ?

Also, it’ll affect only the opened tab or all the window ?

0 Likes

#6
  1. Read current color scheme file.

    1. Read color_scheme from user settings. (Note there if the user uses auto, use sublime.ui_info() to find whether it’s light or dark now.)
    2. Use sublime.find_resources() to find the file path of the color scheme.
    3. Use sublime.load_resource() to get the file content of the color scheme.
    4. Use sublime.decode_value() to parse the color scheme content into a Python dict, say color_scheme.
  2. Modify the color scheme.

    1. Change color_scheme['globals']['background'] value to your preference.
    2. Save the result of sublime.decode_value(color_scheme) into another file.
    3. Set user’s settings or view settings use the new color scheme file.
0 Likes

#7

An example of this:

0 Likes

#8

Thank you, I know how to do it now :slight_smile:

0 Likes