-
Notifications
You must be signed in to change notification settings - Fork 0
/
instant_color.py
127 lines (110 loc) · 3.84 KB
/
instant_color.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import sys
import re
import xml.etree.ElementTree as ET
import subprocess
from subprocess import call
import applescript
import string
##################FOR YOU TO CONFIGURE#######################
COLOR_NAME = "Solarized Dark" #Default Value
PATH_TO_ROOT_FOLDER = "/PATH/TO/YOUR/OWN/FOLDER"
#############################################################
PATH_TO_PLIST = PATH_TO_ROOT_FOLDER + "com.googlecode.iterm2.plist"
PROFILE_NUMBER = -1
apple_script_to_run = ''''''
#convert iTerm prefs into an xml so we can operate on it
cmd = "plutil -convert xml1 -o - " + PATH_TO_PLIST + " > "+ PATH_TO_ROOT_FOLDER + "readable_prefs.xml"
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
process.communicate()[0], "-actual"
tree = ET.parse(PATH_TO_ROOT_FOLDER + 'readable_prefs.xml')
root = tree.getroot()
root = root[0]
def check_children(passed_root, text):
for child in passed_root:
if(child.text == text):
return True
return False
def get_colors(preset_name):
global tree
color_root = tree.getroot()[0]
i=0
for child in color_root:
if(child.text=='Custom Color Presets'):
color_root = color_root[i+1]
i+=1
j=0
for child in color_root:
if(child.text== preset_name):
color_root = color_root[j+1]
return color_root
j+=1
print "ERROR: Didn't find that preset"
return None
def print_color(Color_Name, component_name, value):
global PATH_TO_PLIST
global PROFILE_NUMBER
cmd = "/usr/libexec/PlistBuddy -c \"Print 'New Bookmarks':" + str(PROFILE_NUMBER) + ":'" + Color_Name + "':'" + component_name + "' " "\" \"" + PATH_TO_PLIST + "\""
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
print process.communicate()[0], "-actual"
def preamble():
global apple_script_to_run
apple_script_to_run += ('''
tell application "iTerm_Nightly"
tell current window
tell current session''')
def closer():
global apple_script_to_run
apple_script_to_run += ('''
end tell
end tell
end tell''')
def change_color(Color_Name, value):
global apple_script_to_run
apple_script_to_run += ('''
set ''' + Color_Name + ' to ' '{' + str(value[0] * 65535) + ',' + str(value[1] * 65535) + ', ' + str(value[2] * 65535) + ''', 0 }
''')
def apple_dis_script():
global apple_script_to_run
# print apple_script_to_run
a_script = applescript.AppleScript(apple_script_to_run)
a_script.run()
def set_shell_var():
global COLOR_NAME
cmd = "export VIM_CS=\"" + COLOR_NAME + "\""
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
process.communicate()[0], "-actual"
def main():
global COLOR_NAME
if len(sys.argv) > 1:
COLOR_NAME = sys.argv[1]
color_presets = get_colors(COLOR_NAME)
rgb_colors = [0,0,0]
j=0
#from iterm plist
ANSI_array = ["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "bright black", "bright red", "bright green", "bright yellow", "bright blue", "bright magenta", "bright cyan", "bright white"]
for colors in color_presets:
if colors.text != None:
if colors.text.find("Color") != -1:
color_str = color_presets[j].text #Ex: "Ansi Color"
m = re.search('[0-1][0-7]|[0-9]', color_str)
if m != None:
index = int(m.group(0))
color_str = string.replace(color_str, m.group(0), ANSI_array[index])
color_type = color_presets[j+1] #Ex: Ansi Color Dict
for component_str in color_type:
if component_str.text.find("Component") != -1:
component_name = component_str.text
else:
value = component_str.text
rgb_colors[2] = rgb_colors[1]
rgb_colors[1] = rgb_colors[0]
rgb_colors[0] = float(component_str.text)
preamble()
change_color(color_str, rgb_colors)
closer()
apple_dis_script()
j+=1
set_shell_var()
print "Done"
if __name__ == '__main__':
main()