Skip to content

Instantly share code, notes, and snippets.

@nugget
Created January 5, 2015 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nugget/d5ef21864b45e24374d2 to your computer and use it in GitHub Desktop.
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
#!/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