Eseguire direttamente un bash script in MacOS con Automator 

To English version

Per aprire direttamente ed eseguire uno script in bash sotto MacOS è opportuno seguire queste indicazioni:

A) all’inizio lo script  deve contenere la riga #!/usr/bin/env bash per una migliore compatibilità con la shell zsh, qualora venga usata di default;

B) usare l’applicazione Automator del MacOS (menu Applicazioni), creando una nuova applicazione e poi scegliendo nel menu sinistra la preferenza ‘Esegui script shell  e mantenere la scelta di default della shell in ‘/bin/zsh

C) usare il comando ‘cmd_to_run’ abbinato a ‘osascript’ per scrivere nella finestra  di Automator la stringa contenente il comando per eseguire lo script  desiderato. Ecco un esempio:

cmd_to_run="cd <patht to file>  && sh  <file name.sh>”

osascript -e "tell application \"Terminal\" to do script \"$cmd_to_run\"activate"  

Basta sostituire all’esempio il percorso dove si trova lo bash script ed il nome dello stesso. Poi salvare il file creato come applicazione dandogli il nome desiderato. Avrete nel menu delle applicazioni un’icona che eseguirà direttamente il contenuto del vostro bash script .

E’ forse la soluzione più rapida ed efficace benché si trovino sulla rete altre soluzioni che però spesso non funzionano. Ovviamente l’esatta esecuzione dello script deriva anche dalla correttezza e compatibilità dello script in bash sotto ambiente MacOS.

--------------------------------------

English version:

How to run a bash script on MacOS with Automator

To directly open and run a bash script in MacOS using Automator, follow these instructions:

A) At the beginning of the script, include the line #!/usr/bin/env bash for better compatibility with the zsh shell, in case it is used by default.

B) Use the Automator application in MacOS (located in the Applications menu) to create a new application. Then, choose 'Run Shell Script' from the left menu and keep the default shell selection as '/bin/zsh'.

C) Use the 'cmd_to_run' command paired with 'osascript' to write the string containing the command to execute the desired script in the Automator window. Here is an example:

cmd_to_run="cd <patht to file>  && sh  <file name.sh>”

osascript -e "tell application \"Terminal\" to do script \"$cmd_to_run\"activate" 

Simply replace the placeholders in the example with the path where the bash script is located and its name.

Then save the created file as an application, giving it the desired name. You will then have an icon in the applications menu that will directly execute the contents of your bash script. This is perhaps the quickest and most effective solution, although other solutions may be found on the internet, which often do not work. Of course, the exact execution of the script also depends on the correctness and compatibility of the bash script under the MacOS environment.