How to Manipulate Fonts and Colors Dialogs in TextEdit

I want to programmatically/automatically make some changes to TextEdit RTF files.

As an example, but no limited to, I want to be able to set the font color and the background color to something more relaxed than hard black and white.

The way I would do it manually is to open the Fonts editor with ⌘T and then click on the appropriate buttons, one for the font color and one for the page background, then doing the same again with the Color popup dialog for each of them, picking the style of color picker and then picking the color within those choices. To do this in KBM, I would have to do a series of at least six Click on Found Image actions. I would rather do mouse clicks relative to the bounds of these dialog windows instead of doing image matching.

image

image

image

But even this is problematical because I can't even find the Fonts window or the Colors window on the screen, I can't get it listed among the app windows. So instead of searching just a local region of the window menu for the button, I have to search the whole screen, because I can't control or predict exactly where the Fonts and Colors dialogs will appear. Yet I know at some level it's treated as a separate window because when I do a screenshot using ⌃⇧⌘4, I can pick the dialogs as windows, as I did above.

Also, these dialogs appear to act like utilities, not as menus within the app. They can be opened in various different apps and if I make changes to the dialog size in one app, when I close that app and open the dialog in a different app, the size change carries over to the new app. That means that I cannot reliably count on the image size of the whole dialog and match it as an image and click relative to that whole image.

I would really rather do this programmatically, but since there is no text on those buttons, they don't seem to be any more findable in KBM v11 than they did before. And there is no "background" attribute listed in the AppleScript dictionary for TextEdit.

Anyone have any ideas?

You can use the Apply Style to Clipboard action for this.

You can read the file to the system (or a named) clipboard, apply styling and then overwrite the file.

Thanks for the tip to this action and it looks like it will be really useful and simplify some things for me.

But unfortunately, there are TWO "background" colors, one that applies to just the text and one that applies to the entire page, and the Apply Style to Clipboard action only does the first one, background on the text itself, like a highlight color. The background color set in this action does not change the color of the margins.

What I'm after is the background color of the entire page.In the first dialog image above, it's the button right under the title "Fonts" with an icon of a page with a corner turned down. By default, it's white, but if you change it, the color is applied to the entire page, including the margins, and a separate background color can be applied to the text, or to elements of the text, with the Apply Style to Clipboard action.

At the moment, I think I have what looks like a viable solution for the specific issue, if not the general case of how to interact with these dialogs. I opened a blank RTF file and saved it. Then I set the text color and the page color and saved that as a different filename. Then I compared the RTF text and examined the settings by referring to an RTF spec (painful because I couldn't find a version where the text was fully searchable). Apparently the page color is done by creating a shape that fills the page, is behind everything, nothing wraps around it, etc.

Inserting the line:

{\*\background {\shp{\*\shpinst\shpleft0\shptop0\shpright0\shpbottom0\shpfhdr0\shpbxpage\shpbypage\shpwr0\shpfblwtxt1\shpz0\shplid1025{\sp{\sn shapeType}{\sv 1}}{\sp{\sn fillColor}{\sv 16711680}}{\sp{\sn fFilled}{\sv 1}}{\sp{\sn bWMode}{\sv 9}}{\sp{\sn fBackground}{\sv 1}}}}}

right before the final "}" seems to do it.

Other parameters get added when it's saved, but that line works. There's a fillColor value of 16711680 that is the "Blueberry" color from the Crayon colors in the color picker. I haven't figured out how to create colors other than by experimentation, which is part of why I would like to be able to just manipulate the color picker.

Using my tell block macro, I generated this:

Text Edit - Background Colour.kmmacros (22 KB)

Macro screenshot

1 Like

WOW! Yes, that looks like a generic answer to the question in the OP, without having to have RTF snippets with hardcoded colors in them. I have to try this out, but I'm already agonizing over when I will have time to truly experiment with it. (I'll mark it as "The Solution" when I get it working...)

Meanwhile, I can try out your "Text Edit - Background Colour" macro and adapt it to what I was asking about in the first place. Obviously I'm going to have to fix the Francophile English spelling of "Colour" to the simpler American version or it won't work on my system ... :wink:

I got:

2023-10-26 17:54:55 Action 15097004 failed: Execute an AppleScript failed with script error: text-script:323:383: execution error: System Events got an error: Can’t get color well 1 of group 2 of toolbar 1 of window "Fonts" of process "TextEdit". Invalid index. (-1719)
2023-10-26 17:54:55 Execute an AppleScript failed with script error: text-script:323:383: execution error: System Events got an error: Can’t get color well 1 of group 2 of toolbar 1 of window "Fonts" of process "TextEdit". Invalid index. (-1719). Macro “Text Edit - Background Colour” cancelled (while executing Execute AppleScript).

And unfortunately, I don't yet know enough about what's going on under the hood in this for that message to be helpful. I don't think it's even getting as far as the window named "Colours" or "Colors".

Since it seems to be failing on the Fonts window, here is the window that pops up:

If I'm reading it right, it's attempting to click on the fifth button in the toolbar, the page background button. So what is it missing?

Making progress at fixing it. In addition to "colour" vs. "color", I didn't notice at first that I had changed to DeskSpaceID from TextEdit in the outer tell application but not the inner tell process. It's all DeskSpaceID for my usage. Also, DSI is based on TextEdit v1.8 (because that's what source is available) and you're probably running 1.16 at least, so that's probably the source of the group numbers being different.

I used your "tell block macro" to get the color well... line for the background color and it's different:

click color well 1 of group 5 of toolbar 1 of window "Fonts"

group 5 instead of group 2. That got me past the Fonts window to the Colors window. That was working when I used the Prompt With List action but not when I locked in a single, "standard" background color. I had forgotten about how with the PWL format that you used, with the double underline, the variable gets set to just the number, not the whole prompt string. Once I fixed that, everything just worked.

Looking good. Thank you SO much!

BTW, I LOVE your "Generate UI Element Tell Block" macro. That's an awesome generic solution to the problem I described in the OP of this thread.

1 Like