2

Terminal menus example

Would there be a way to have a file/icon of some sort of my desktop - (or, not as ideal but in the dock) which essentially opens a terminal window via the menu Shell -> New Window -> .. one of the profiles.

(The profiles you see "discotest" etc are simply plain Terminal windows, with different color backgrounds.)

Use ... when I have a few shells open to different places, as I open each one, I simply change the color so they are all different. (I do this so often I have the keystroke to bring up the color selector in finger memory!)

Thus, I'm wanting to have say three icons on my desktop, named red/green/blue and simply double-clicking one, opens Terminal -> menu Shell -> New Window -> SomeProfile

An alternative, perhaps even better, solution to the underlying problem, would thus be, an automation that choose a new random/whatever color each time you open yet another new Terminal window.

TY

2
  • A profile (setting set) has more than the background color, such as the font, window size, shell, terminal type, etc. Several window properties are exposed to AppleScript - are you just wanting to change the window background color, or the profile? Commented Aug 16, 2024 at 15:42
  • just the color would be great! "tap something to cycle the color", simple. (to make differnt Terminal windows distinguisbablke) Commented Aug 16, 2024 at 17:28

3 Answers 3

2
+50

An AppleScript can be used, for example:

# on run {args, parameters} -- use this declaration for a Run AppleScript action in Automator on run args -- script run from the editor or Script Menu, or app double-clicked set useFinderSelection to false -- set to true to use the Finder selection if no arguments set processedItems to {} if args is not in {{}, missing value, current application, me} then -- command line or Automator arguments? repeat with anItem in args try set anItem to POSIX path of anItem if isDirectory(anItem) then doStuff("cd " & quoted form of anItem) set end of processedItems to anItem end if on error errmess -- oops log errmess end try end repeat else if useFinderSelection is true then set processedItems to doFinderSelection() end if if processedItems is {} then doStuff("") -- just the default working directory end run to doFinderSelection() -- handle folders in the current Finder selection tell application "Finder" to set theSelection to (get selection as alias list) set processedItems to {} repeat with anItem in theSelection if contents of anItem is not (path to me) then -- application will be selected if double-click set anItem to POSIX path of anItem if isDirectory(anItem) then doStuff("cd " & quoted form of anItem) set end of processedItems to anItem end if end if end repeat return processedItems end doFinderSelection to doStuff(theScript) -- open a new Terminal window with random background color, running theScript if application "Terminal" is running then set {r, g, b} to {16383 * (random number from 0 to 4), 16383 * (random number from 0 to 4), 16383 * (random number from 0 to 4)} -- 125 possible colors tell application "Terminal" activate do script theScript -- a script of "" just creates a new window set background color of window 1 to {r, g, b} if (r + g + b) / 3 > 21845 then -- try to make the text color contrast set normal text color of window 1 to {0, 0, 0} -- black else set normal text color of window 1 to {65535, 65535, 65535} -- white end if end tell else tell application "Terminal" launch -- avoid multiple windows do script theScript -- default window activate end tell end if end doStuff on isDirectory(posixPath) -- check if a file item is a directory or bundle set posixPath to quoted form of posixPath set determination to (do shell script "/usr/bin/file -b " & posixPath) if determination contains "directory" then return true return false end isDirectory on open fileItems -- items dropped onto the app (only needed for a droplet) repeat with anItem in fileItems set anItem to POSIX path of anItem if isDirectory(anItem) then doStuff("cd " & quoted form of anItem) end repeat end open 

Edit:

Updated the script to be able to be run from a script editor or the Script Menu*, from the command line via osascript, or saved as a script applet or droplet. With a slight change to the run handler parameters, it can also be run from a Run Script action in Automator. It looks for directories in any file arguments (command line or dropped onto the droplet, otherwise optionally the current Finder selection), and opens a new Terminal window with a random background color for each one found, running a script in the window that changes the working directory to the folder. If there are no arguments or Finder selection, a new window is created at the default working directory.

*The Script Menu (enabled according to the Script Editor preference) is a status item menu on the right side of the main menu bar that uses scripts (or aliases to them) that you put in your user's ~/Library/Scripts folder. If a script is placed in the main folder, it will always be in the menu, otherwise it can be placed in a folder named for the application in the Applications subfolder (create folders as needed), where it will only be in the menu when that application is active.

4
  • This is the way. +1 shortcuts can also call any Automator script or this and be added as a desktop widget or Notification Center widget for single click launching of this automation. Commented Aug 17, 2024 at 21:19
  • Amazing! I'm a bit confused as to how to make that run, when, I click an icon on desktop but I'll figure it out ! Commented Aug 17, 2024 at 21:28
  • 1
    @Fattie - From the Script Editor you can save it as an application, from Automator it can be placed in a Run AppleScript action and saved as an application. My favorite for scripts is to put them in the Script Menu, then they are always available from a menu in the menu bar. Commented Aug 17, 2024 at 22:15
  • @red_menace amazing ! Commented Aug 17, 2024 at 22:38
1

The shortcuts app lets you make all sorts of automations and save them to the desktop. Have you tried that to see if it gets you started on a solution?

1
  • shortcuts app is fascinating. i spent ten mins looking at it and, as i understand it it cant "click menu items" in an app it opens. a solution seems to be to use javascript in a Shortcuts harness, as for example outlined here youtu.be/ScYZBoUDdAk?t=146 I put this comment in as it may help future googlers! Commented Aug 18, 2024 at 17:04
0

After searching on SE sites I did find another solution, mashing up some answers

(1) https://superuser.com/a/1201518/401734

to set a randomish color from the shell itself:

] osascript -e "tell application \"Terminal\" to set current settings of front window to some settings set" 

(2) Lauching a Terminal '.command' file, without exiting session

tip by user @klanomath in the comments

using ".command" files to very easily launch a shell

Hence like:

(3) https://apple.stackexchange.com/a/474750/19991

In short, make a plain text file example.command, the contents of which is these four lines of text:

osascript -e "tell application \"Terminal\" to set current settings of front window to some settings set" cd /Users/blah/Desktop/blah ssh -i blah blah $SHELL 

Double click on the file.

All works perfectly and does literally exactly what asked in the question, if anyone else is looking for that:

enter image description here

5
  • Again, note that a settings set is an entire profile, which sets a lot more than colors. If you aren't changing the profile, osascript can run any script to do stuff like set individual Terminal properties such as window colors, run a shell script, and/or work with arguments. See the Terminal scripting dictionary for its commands and properties. Commented Aug 18, 2024 at 0:27
  • "See the Terminal scripting dictionary for its commands and properties" outstanding tip @red_menace, TY. you know, there's only "so much time in the day" to spend on IDEs, CLIs, APIs, command sets, libraries, etc that are not in one's core competency, so, it's likely I'll never get to it :/ It would be cute to modify that osascript command so that it cycles rather than random occasionally repeats, but, when I started these QA (really, some years ago!) I had utterly no clue of the existence of ".command" files on Mac, so, my life is changed. I've used my shiny new xyz.command desktop file 10 Commented Aug 18, 2024 at 14:08
  • 0s of times already! Tears of joy! 🥹🥹 Commented Aug 18, 2024 at 14:08
  • I went ahead and updated my answer so that the script can be run in different ways, with arguments that will set the current working directory. Commented Aug 18, 2024 at 14:21
  • 🥹🥹🥹🥹🥹🥹🥹🥹🥹🥹 Commented Aug 18, 2024 at 16:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.