1

I'm just migrated to macbook from ubuntu, for development.

I had a bash script in ubuntu which opened lots of terminal tabs, and entered various commands in each one, like going to a particular folder, opening some software, ssh-ing onto the live box, etc. It's my "getting ready to start work" script, that i run when the computer starts up. It's listed below. It works in quite a primitive way, using xdotool to simulate keyboard and mouse input - everything that I would do if I was doing it manually.

I'd like to run an equivalent on my mac - is there such a thing? If there are alternatives, which one is easiest to 'translate'? thanks

#!/usr/bin/env bash
window="$(xdotool search --class mate-terminal | head -1)"
xdotool windowfocus $window

#first cd to our work folder: this will be the base window
xdotool type "cd $elearn"
xdotool key Return

#open tab for elearn with mongrel_rails start
xdotool key ctrl+shift+t
sleep 2
xdotool type "cd ~"
xdotool key Return
xdotool type "cd $elearn"
xdotool key Return
xdotool type "source renametab mongrel"
xdotool key Return
xdotool type "mongrel_cluster_start"
xdotool key Return

#open tab for elearn with console start
xdotool key ctrl+shift+t
sleep 2
xdotool type "cd ~"
xdotool key Return
xdotool type "cd $elearn"
xdotool key Return
xdotool type "source renametab console"
xdotool key Return
xdotool type "script/console"
xdotool key Return

#open tab for elearn, don't do anything
xdotool key ctrl+shift+t
sleep 2
xdotool type "cd ~"
xdotool key Return
xdotool type "cd $elearn"
xdotool key Return
xdotool type "source renametab main"
xdotool key Return

#open tab for elearn, with mysql
xdotool key ctrl+shift+t
sleep 2
xdotool type "cd $elearn"
xdotool key Return
xdotool type "source renametab mysql"
xdotool key Return
xdotool type "mysql -u root e_learning_resource_development"
xdotool key Return

#open three tabs sshd onto live box
#we need to do this now on the live box, to switch to the "nobody" user
#sudo -u nobody bash
xdotool key ctrl+shift+t
sleep 2
xdotool type "cd $elearn"
xdotool key Return
xdotool type "ssh-live1"
xdotool key Return
xdotool type 'export PS1="$PS1""\[\e]0;live-console \a\]"'
xdotool key Return
xdotool type "cd /path/to/server/folder"
xdotool key Return
xdotool type "sudo -u nobody bash"
xdotool key Return
xdotool type "script/console production"
xdotool key Return

#same again stay on ubuntu user and don't start console
xdotool key ctrl+shift+t
sleep 2
xdotool type "cd $elearn"
xdotool key Return
xdotool type "ssh-live1"
xdotool key Return
xdotool type 'export PS1="$PS1""\[\e]0;live1 ubuntu\a\]"'
xdotool key Return
xdotool type "cd /path/to/server/folder"
xdotool key Return

#same again but don't start console
xdotool key ctrl+shift+t
sleep 2
xdotool type "cd $elearn"
xdotool key Return
xdotool type "ssh-live1"
xdotool key Return
xdotool type 'export PS1="$PS1""\[\e]0;live1 nobody\a\]"'
xdotool key Return
xdotool type "cd /path/to/server/folder"
xdotool key Return
xdotool type "sudo -u nobody bash"
xdotool key Return

#open tab and tail the dev log
xdotool key ctrl+shift+t
sleep 2
xdotool type "cd $elearn"
xdotool key Return
xdotool type "source renametab log"
xdotool key Return
xdotool type "tail -f log/development.log"
xdotool key Return

#go back to tab 1, then exit
xdotool key alt+1
sleep 2
xdotool type "exit"
xdotool key Return


1
  • What kind of research have you already done, including searching for similar questions on this site?
    – nohillside
    Commented Nov 12, 2021 at 10:42

1 Answer 1

1

You could use iTerm2 a free and brilliant terminal and use its Python API

https://iterm2.com/python-api/

You could use AppleScript or JavaScript based script with the terminal app

List of all AppleScript commands for Terminal application

I would have to check, but there might be something in the new shortcuts app for MacOS . I will check

Update : shortcuts doesn’t have the actions for terminal other than run script.

Don’t forget you can do a lot from command line such as tmux to open multiple consoles or ssh connections, gnu parallel and many tools to to manage tmux sessions.

8
  • Funnily enough one of my colleagues just recommended switching to iterm2, i will check it out, thanks. Commented Nov 12, 2021 at 11:42
  • No problem I have just updated my answer. I.e don’t forget solutions which are command line based I.e tmux
    – AndrewC
    Commented Nov 12, 2021 at 11:54
  • Have a look at smug a tmux session manager github.com/ivaaaan/smug
    – AndrewC
    Commented Nov 12, 2021 at 12:04
  • I've set up an example python script but am struggling with a lack of python knowledge and a lack of simple examples :). ALl i need it to do is to a) open new tabs) b) rename the tabs c) run a list of commands in each tab. Can you see some documentation for that? I'm on iterm2.com/documentation-scripting-fundamentals.html but can't find those examples... Commented Nov 12, 2021 at 12:27
  • iterm2.com/python-api/examples/launch_and_run.html
    – AndrewC
    Commented Nov 12, 2021 at 12:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .