Script :: Toggling Bike screen and text colors

Here, FWIW, is an AppleScript which I use to toggle Bike between my usual working colors (amber text against a dark chocolate background), and a temporary black ink on white background.

You should be able to replace my color values with your own.

An RGB color is specified as an AppleScript list of 3 integers, each in the range {0…65535}, or to put it another way {1…(2 ^ 16) - 1}.

(Click the copy icon at top right of the code)

tell application "Bike"
    
    -- In AppleScript the return values from
    -- {background color, foreground color}
    -- are integers in the range [0..65535]
    
    -- but the same properties in JS seems to get floats (in the range 0..1)
    -- also, using the properties JS always seems to yield black background 
    -- and black foreground, regardless of input values.
    
    set white to ((255 / 255) * 65535) as integer
    set black to ((23 / 255) * 65535) as integer
    
    
    -- Black ink on white paper
    set light to {{white, white, white}, {black, black, black}}
    
    -- Chocolate and orange.
    set dark to {{8736, 8736, 8736}, {65535, 51656, 37008}}
    
    -- The return values of these two properties seem to get
    -- accidentally swapped in the API, just after a colors 
    -- reset in Preferences, when foregroundColor gives us 
    -- the background, and vice versa.
    set {nBack, nFore} to {foreground color, background color}
    
    if (item 1 of nBack) < (item 1 of item 1 of dark) then
        set {background color, foreground color} to dark
    else
        set {background color, foreground color} to light
    end if
end tell

Two footnotes on Bike colors scripting in case of confusion:

  1. In the current preview build (1.7: 90) something about Preferences > Editor > Reset Colors seems to temporarily confuse the scripting interface, if the Appearance mode is Light, and it appears to initially (just once) return a foreground value in place of the background value, and vice versa. (See test script below)
  2. Some kind of type conversion glitch seems to be confounding the setting of these properties if we use JavaScript for Automation, so AppleScript only, at the moment, I think.

To test an apparent scripting interface glitch immediately after Preferences > Editor > Reset Colors :

  1. Choose Preferences > General > Light
  2. Click Preferences > Editor > Reset Colors
  3. Try running the script below. On this system, the screen colors are unexpectedly inverted on first run, but the inversion doesn’t repeat thereafter.
tell application "Bike"
    set {backColor, foreColor} to {background color, foreground color}
    
    set {background color, foreground color} to {backColor, foreColor}
end tell
1 Like

This has had me stumped for a while :slight_smile:.

What is happening is:

  1. When you reset colors Bike resets to system colors which are “Dynamic” meaning they return different actual colors depending on light/dark mode

  2. When AppleScript reads colors Bike is returning that dynamic color, but AppleScript only deals with non dynamic colors … so it picks one of the light/dark variants. Seems like generally it picks right, but it gets confused if the apps appearance changes at runtime.

  3. I’ve fixed for next release by making sure that Bike only returns non-dynamic colors to AppleScript

2 Likes

Subtle :slight_smile: Thanks for :mag_right: that down !

This should be fixed in Bike 1.7 Preview (91)

1 Like