Created
January 5, 2015 19:39
-
-
Save nugget/d5ef21864b45e24374d2 to your computer and use it in GitHub Desktop.
Tcl script to pull RGB color values out of OS X .terminal settings files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env tclsh | |
# | |
# This will definitely require modifications before it will be useful | |
# to you, but it might help guide you in the right direction. | |
# | |
package require tdom | |
package require base64 | |
proc float_to_hex {val} { | |
set dec [expr int(255 * $val)] | |
set hex [format %02X $dec] | |
return $hex | |
} | |
proc decode_color {buf} { | |
set buf [regsub -all {\s+} $buf ""] | |
set buf [::base64::decode $buf] | |
if {[regexp {([\d\.]+) ([\d\.]+) ([\d\.]+)} $buf _ r g b]} { | |
set rr [float_to_hex $r] | |
set gg [float_to_hex $g] | |
set bb [float_to_hex $b] | |
return "${rr}${gg}${bb}" | |
} else { | |
return | |
} | |
} | |
proc main {} { | |
foreach tf [glob -nocomplain "*.terminal"] { | |
unset -nocomplain host | |
set fh [open $tf] | |
set doc [dom parse [read $fh]] | |
close $fh | |
set root [$doc documentElement] | |
foreach node [$root selectNodes "//key"] { | |
set key [$node asText] | |
set vnode [$node nextSibling] | |
set value [$vnode asText] | |
set host($key) $value | |
} | |
$doc delete | |
unset -nocomplain out | |
lappend out [decode_color $host(BackgroundColor)] | |
lappend out [decode_color $host(TextColor)] | |
lappend out [decode_color $host(TextBoldColor)] | |
lappend out [decode_color $host(SelectionColor)] | |
if {[regexp {ssh (\S+)} $host(CommandString) _ hostname]} { | |
set fh [open "/tmp/itermcolor" "w"] | |
puts $fh [join $out "\t"] | |
close $fh | |
if {[catch {exec scp /tmp/itermcolor $hostname:.itermcolor} err]} { | |
puts "Error connecting to $tf" | |
# puts " $host(CommandString)" | |
# puts " [join $out " "]" | |
} else { | |
puts "Copying '[join $out " "]' to $hostname" | |
} | |
} else { | |
puts "Weird command for $tf:" | |
puts " $host(CommandString)" | |
puts " [join $out " "]" | |
} | |
} | |
} | |
if !$tcl_interactive main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment