Terminal Commands

Joined
Jul 7, 2007
Messages
137
Reaction score
4
Points
18
Location
Baltimore
Your Mac's Specs
iPod Video| 15" MacBook Pro | iPod Shuffle
Is there anyway to create a file with terminal commands inside them, and when you double click the file, the terminal command will execute in terminal?
 

rman


Retired Staff
Joined
Dec 24, 2002
Messages
12,637
Reaction score
168
Points
63
Location
Los Angeles, California
Your Mac's Specs
14in MacBook Pro M1 Max 32GB 2TB
I believe this has been discussed before. You will need to spawn a terminal window, which you will execute the shell script that you plan to create. So basically your script will have a line that start a terminal process tha execute you script.

If you search the board you may find the example that I believe was given.
 
Joined
Feb 16, 2008
Messages
17
Reaction score
0
Points
1
Location
Illinois
Your Mac's Specs
Black Macbook, 1gb RAM
All I do is create a shell script with the extension .sh, save it and run "chmod 755 name-of-script.sh", then change the default app it uses to the Terminal.

Is that what you want?
 
Joined
Apr 29, 2006
Messages
4,576
Reaction score
378
Points
83
Location
St. Somewhere
Your Mac's Specs
Mac Studio, M1 Max, 32 GB RAM, 2 TB SSD
All I do is create a shell script with the extension .sh, save it and run "chmod 755 name-of-script.sh", then change the default app it uses to the Terminal.

Is that what you want?

This is what I do too - works like a charm.
 
Joined
Apr 29, 2006
Messages
4,576
Reaction score
378
Points
83
Location
St. Somewhere
Your Mac's Specs
Mac Studio, M1 Max, 32 GB RAM, 2 TB SSD
Whoops, I take that back. I have been using X11.app as the program my executable shell scripts launch with, as most of them are X11 oriented. I quickly tried to use Terminal.app, but Tiger will not allow it as part of the Open With selection set - it is grayed out. I will hunt around and see if I can find a way to make this work, and will post back.
 
Joined
Feb 16, 2008
Messages
17
Reaction score
0
Points
1
Location
Illinois
Your Mac's Specs
Black Macbook, 1gb RAM
Whoops, I take that back. I have been using X11.app as the program my executable shell scripts launch with, as most of them are X11 oriented. I quickly tried to use Terminal.app, but Tiger will not allow it as part of the Open With selection set - it is grayed out. I will hunt around and see if I can find a way to make this work, and will post back.

If the Terminal.app icon and text is grayed out then just uncheck "Recommended Applications" or something like that.
 
Joined
Apr 29, 2006
Messages
4,576
Reaction score
378
Points
83
Location
St. Somewhere
Your Mac's Specs
Mac Studio, M1 Max, 32 GB RAM, 2 TB SSD
Yup, that is it. However, I find what happens when I double click on the script's icon is that Terminal.app launches, but it doesn't run the script.

After some researching on the web, it seemed that you needed to title your script with the suffix ".term". This seems to work too, but the same result occurs. Terminal.app launches, but it doesn't run the script.

I am guessing that what is needed here is a small AppleScript script that launches Terminal.app and then feeds it the script name as a command. However, I am abysmally ignorant of AppleScript and don't know how to do this.

Would any kind volunteers like to demonstrate their mastery of AppleScript by showing us the code for a script that will launch Terminal.app, have it run a bash script and then exit terminal.app and close the terminal windows? Thanks!
 
Joined
Feb 16, 2008
Messages
17
Reaction score
0
Points
1
Location
Illinois
Your Mac's Specs
Black Macbook, 1gb RAM
Yup, that is it. However, I find what happens when I double click on the script's icon is that Terminal.app launches, but it doesn't run the script.

After some researching on the web, it seemed that you needed to title your script with the suffix ".term". This seems to work too, but the same result occurs. Terminal.app launches, but it doesn't run the script.

Hmmmm......It works for me. The only problem I have is that when I choose Terminal.app to open the script (with a .sh extension), Finder says "It is not known if this application can open shell script files", but it still works.

Does the Terminal actually open, or does it start to open and then quit? If it opens and says "[Process Complete]", then you should be in business. Try it with a script that echoes a string so you can physically see the results.

My commands (in order) and results:

cd Desktop
vi test.sh
(typed " echo 'test' ")
(exited vi)
chmod 755 test.sh
(changed preferred app to Terminal)
double clicked and got...
---------
Last login: Mon Feb 18 16:57:01 on ttys000
macbook:~ nick$ /Users/nick/Desktop/test.sh ; exit;
test
logout

[Process completed]
---------
 
Joined
Apr 29, 2006
Messages
4,576
Reaction score
378
Points
83
Location
St. Somewhere
Your Mac's Specs
Mac Studio, M1 Max, 32 GB RAM, 2 TB SSD
Agreed nMiller, I did achieve that result at one point along the way, but did not consider it to be "success" - the Terminal.app window stays open, leaving quite a mess. I have been looking for a way to in essence launch a bash script via a clickable icon that simply goes away and does some work and then disappears when done.
 
Joined
Feb 16, 2008
Messages
17
Reaction score
0
Points
1
Location
Illinois
Your Mac's Specs
Black Macbook, 1gb RAM
Agreed nMiller, I did achieve that result at one point along the way, but did not consider it to be "success" - the Terminal.app window stays open, leaving quite a mess. I have been looking for a way to in essence launch a bash script via a clickable icon that simply goes away and does some work and then disappears when done.

Ok. Yeah I suppose AppleScript would work for that.
 
Joined
Apr 29, 2006
Messages
4,576
Reaction score
378
Points
83
Location
St. Somewhere
Your Mac's Specs
Mac Studio, M1 Max, 32 GB RAM, 2 TB SSD
OK, AppleScript experts, please weigh in! Could someone post here a brief and simple script to start Terminal.app, have it execute a bash script, and then close down and quite Terminal.app, clearing the Terminal.app window in the process. Thanks!
 
Joined
Apr 29, 2006
Messages
4,576
Reaction score
378
Points
83
Location
St. Somewhere
Your Mac's Specs
Mac Studio, M1 Max, 32 GB RAM, 2 TB SSD
OK folks, I have it... an AppleScript script that will do the above. It turns out to be VERY easy. Here it is:

Code:
do shell script "/Users/mac57/bin/any-bash-command"
That is the whole thing! Where I show "any-bash-command", you can substitute any command that bash can execute from Terminal.app, including bash scripts. So, this is pretty general purpose for the nature of the question at hand.

To run this, open the AppleScript editor (Applications/AppleScript/Script Editor.app) and type that line in. Save the result out to your desktop, or somewhere convenient. While still in the script editor, use the Run button to test your script to be sure it is doing what you want.

Now to make life simple, so that you don't have to open the Applescript Editor every time you want to run your script, open the AppleScript utility itself (same place as editor is) and check the check box for "Show Script Menu in menu bar". A little scroll-like symbol will appear on your menu bar. I call this the "Scroll icon" from now on. Click it and select "Open Scripts Folder". This opens a Finder window on your scripts folder. Place your new AppleScript script in there and it magically now appears as part of the drop down menu you get when you click the Scroll icon. You can now run your script by simply selecting its name from the Scroll icon on your Menu bar.

Done!
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top