MacOS – How to use osascript to open the Terminal App in a new window and make sure it’s on the top of all other windows

applescriptmacosterminal

I found a way to open Terminal.app very usefully as:

osascript -e \'tell application "Terminal" to do script "cd myfolder && ./something.sh param1"\'

If it were executed from a terminal window then everything is fine – at least for every case that I test. Howeer I run this command from a Node.js server because only that I can trigger a script in a terminal window. But here is the trouble: when it's executed, the Terminal window is behind my Chrome Browser.

How can this window be surely set on the top? I noticed that the window can be surely on top of other Terminal windows but how to set it on top of all other windows?

For example, the window is below Chrome and very hard to be seen:

enter image description here

Best Answer

Activate

Use the AppleScript command activate to bring the Terminal.app application to the front:

tell application "Terminal"
    activate
end tell

Using osascript this becomes:

osascript -e 'tell application "Terminal" to activate'