Customize single scripts with specific fonts, as well as bg/fg colours

I know how to customize the shell in the Terminal prefs, but I would like to know how to customize only the shell of a related script. I have some scripts that I want to have a certain font and certain background and foreground colors. Each script should have different colors and different characters. While the terminal preferences for new shells should remain the usual default. How to do?


Posted on May 8, 2019 3:37 AM

Reply
6 replies

May 8, 2019 3:22 PM in response to appletired

Are you talking about changing the environment of the shell/window your script is running in?

Or having your script dynamically display color during its execution?


If the former, the easiest way is to use Terminal.app's 'Profiles' setting to define your color settings, then apply the profile to the Terminal window:


tell application "Terminal"

set current settings of selected tab of window 1 to settings set "Ocean"

end tell


Failing that you can set the defaults directly:


tell application "Terminal"

tell selected tab of window 1

set font name to "Arial"

set font size to 14

set background color to {0, 12767, 2345}

set normal text color to {65535, 0, 0}

set cursor color to {0, 0, 65535}

end tell

end tell


Finally, if you want to control it within the script, you need to learn about the shell's escape codes for color coding:

https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux

May 15, 2019 11:06 PM in response to appletired

If you want to execute script files with different windows, your easiest path is to set Terminal.app's default settings to the set you want, then execute your script:


tell application "Terminal"

set default settings to settings set "Ocean"

do script "ls" -- opens a new window for this command

set default settings to settings set "Grass"

do script "sh /path/to/your/script" -- executes the script

end tell


This works because the 'default settings' applies to new windows opened from that point forwards, and may be more reliable than setting individual font parameters.

May 10, 2019 5:56 PM in response to Camelot

hum, thank you for the answer.


I didn't intend to do it with Applescript but with pure Bash directly.

Unfortunately I'm not good at Applescript. I'm a beginner and I just discovered the world of the command line, but while I managed to learn a few things about the unix commands, I still haven't learned Applescript. And that should be more friendly!
However if there are no alternatives I do not exclude learning Applescript and I start studying from your post. E.g. I don't know how to put together "tell application" Terminal "...".

with "tell application" Finder "..." to launch the script. The script is saved with Terminal chmod + x ...

I already know the echo command to color but I can't find documentation to do echo fonts ... why?


May 14, 2019 2:28 AM in response to Camelot

Camelot, I'm trying, your script actually works well sometimes !!! :-D
Sometimes they don't..... :-C
It seems that the terminal has problems resetting itself with its preferences.

Again, sometimes your script works fine. The relationship between Applescript and Terminal does not seem to be solid.
Too bad because it looks like we can do it. But unfortunately I lack the necessary preparation to solve the problem


May 16, 2019 4:49 AM in response to Camelot

OK! This work fine. I


tell application "Terminal"

set default settings to settings set "PROFILE"

do script "sh '/PATH/FILE'"


end tell


I only use this part, because when I launch these scripts I don't need to open a window even to work.


Now there is only one small problem. When the program opens, it also opens an empty window but I would like it not to open it. it's possible?


thank you very much for all the help




May 13, 2019 1:02 PM in response to appletired

I already know the echo command to color but I can't find documentation to do echo fonts ... why?


There is no font control in standard shell commands. The shell harks back to the early days of computing where fonts weren't even a pipe dream.

Modern terminal-based applications (such as Terminal.app) add a degree of control, but it's just a cosmetic layer on top of the underlying shell.

In short, the shell has support for limited controls (bold, italic, some color), but multi-font, styled text was never a consideration. If that's your primary goal then you need to use something other than Terminal.app as your interface.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Customize single scripts with specific fonts, as well as bg/fg colours

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.