Last active
January 24, 2025 13:15
-
-
Save retrography/7738fb850d8388156fc2 to your computer and use it in GitHub Desktop.
Apply a color preset to the current iTerm session from command prompt
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/osascript | |
-- Copyright © 2016, Mahmood Shafeie Zargar all rights reserved | |
-- This program is released under the terms of MIT License | |
-- Sort routine from http://www.macosxautomation.com/applescript/sbrt/sbrt-05.html | |
on run argv | |
try | |
set argument to item 1 of argv | |
on error | |
set out to "\n Scheme allows you to apply the color preset of your choice to your active iTerm session on-the-fly." | |
set out to out & " Note: Currently 'scheme' can only see and apply your custom presets, and not iTerm's native color presets that come with it out-of-the-box (e.g. Tango).\n" | |
set out to out & "\n Syntax: scheme [-l|--list] [color preset]\n" | |
return out | |
end try | |
try | |
set iTermPList to "~/Library/Preferences/com.googlecode.iterm2.plist" | |
on error | |
return "\n iTerm plist file not found or can not be opened.\n" | |
end try | |
set cMax to 65535 | |
set names to {} | |
set reds to {} | |
set greens to {} | |
set blues to {} | |
set alphas to {} | |
tell application "System Events" | |
tell property list file iTermPList | |
tell contents | |
set names to name of every property list item of property list item "Custom Color Presets" | |
if argument is in {"--list", "-l"} then | |
set output to "\n List of available color presets:\n\n" | |
set names to my sort(the names) | |
repeat with s in names | |
set output to output & s & "\n" | |
end repeat | |
return output | |
else if argument is in names then | |
try | |
tell property list item argument of property list item "Custom Color Presets" | |
set names to name of every property list item | |
set reds to value of property list item "Red Component" of every property list item | |
set greens to value of property list item "Green Component" of every property list item | |
set blues to value of property list item "Blue Component" of every property list item | |
repeat with colName in names | |
try | |
set end of alphas to value of property list item "Alpha Component" of property list item colName | |
on error | |
set end of alphas to 1 | |
end try | |
end repeat | |
end tell | |
on error | |
return "\n Failed to load color preset " & argument & " from iTerm plist file.\n" | |
end try | |
else | |
return "\n " & argument & " is not one of the available color presets.\n" | |
end if | |
end tell | |
end tell | |
end tell | |
try | |
tell application "iTerm" | |
set currentSession to current session of current tab of current window | |
tell currentSession | |
repeat with i from 1 to count of names | |
set redVal to round(item i of reds * cMax) | |
set greenVal to round(item i of greens * cMax) | |
set blueVal to round(item i of blues * cMax) | |
set alphaVal to round(item i of alphas * cMax) | |
set colName to item i of names | |
if colName contains "Ansi 0 Color" then | |
set ANSI black color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 1 Color" then | |
set ANSI red color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 2 Color" then | |
set ANSI green color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 3 Color" then | |
set ANSI yellow color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 4 Color" then | |
set ANSI blue color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 5 Color" then | |
set ANSI magenta color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 6 Color" then | |
set ANSI cyan color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 7 Color" then | |
set ANSI white color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 8 Color" then | |
set ANSI bright black color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 9 Color" then | |
set ANSI bright red color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 10 Color" then | |
set ANSI bright green color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 11 Color" then | |
set ANSI bright yellow color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 12 Color" then | |
set ANSI bright blue color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 13 Color" then | |
set ANSI bright magenta color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 14 Color" then | |
set ANSI bright cyan color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Ansi 15 Color" then | |
set ANSI bright white color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Background Color" then | |
set background color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Bold Color" then | |
set bold color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Cursor Color" then | |
set cursor color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Cursor Text Color" then | |
set cursor text color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Foreground Color" then | |
set foreground color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Selected Text Color" then | |
set selected text color to {redVal, greenVal, blueVal, alphaVal} | |
else if colName contains "Selection Color" then | |
set selection color to {redVal, greenVal, blueVal, alphaVal} | |
end if | |
-- These are missing for now: {"Badge Color", "Cursor Guide Color", "Link Color", "Tab Color"} | |
end repeat | |
end tell | |
end tell | |
on error | |
return "\n Failed to apply color preset " & argument & ".\n" | |
end try | |
return "\n Applied color preset " & argument & " to the current session.\n" | |
end run | |
on sort(theList) | |
set the indices to {} | |
set the sortedList to {} | |
repeat (the number of items in theList) times | |
set the lowItem to "" | |
repeat with i from 1 to (number of items in theList) | |
if i is not in the indices then | |
set thisItem to item i of theList as text | |
if the lowItem is "" then | |
set the lowItem to thisItem | |
set the lowItemIndex to i | |
else if thisItem comes before the lowItem then | |
set the lowItem to thisItem | |
set the lowItemIndex to i | |
end if | |
end if | |
end repeat | |
set the end of sortedList to the lowItem | |
set the end of the indices to the lowItemIndex | |
end repeat | |
return the sortedList | |
end sort |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment