Executing Unix-commands from GUI?


Is it possible from AppleScript to call a file/command, that will execute
a command in Unix?

Is it possible to make a file (.pl or .sh), which you can dobbleclick in
order to execute it in the terminal window?

with Kind regards

Søren Rehné
This isn't a total solution, but it might set you on the right track. It seems that Terminal.app will take any arguments that you pass it, and execute them as UNIX commands. For instance:
open -a /Applications/Utilities/Terminal.app /usr/bin/top
will tell Terminal.app to open the program /usr/bin/top. A new little window will pop open, and top will be running inside. Quit top, and it will say Process Completed. You can also get the same results by dragging the program onto the Terminal's icon in the Dock. The only thing I haven't figured out is how to specify the window size at launch. Knowing that this works, you should only need to find a way to script it.

I was actually trying to figure this out myself, once, but since I'm no AppleScript expert, I was having trouble telling Terminal.app to open a particular file. Let me know if you come up with anything.
Someone's written some AppleScript extensions (two different sets, I think) that let you call Unix commands from an AppleScript and get the results back. Look on www.versiontracker.com for it.

If you make an executable script you can run it from the Finder if you make sure you do these things:

o Make #!/bin/sh or #!/bin/perl (or whatever) the first line
o Make the file executable (chmod +x myfile)
o Change the filename so that it ends in .command.

You might need to have no spaces in the filename too.

Hey, the .command trick is pretty cool! Now if I could just figure out how to resize the new window...
To resize and save the window size of this script, do the following;

1) Launch the xxx.command executable.
2) Resize the window to the desired size.
3) Choose "Save As" or "Save" from the Terminal Menu with the "Main Window" option chosen and save the file.

This new executable file will open with the saved window size settings.

Credit given to endian for the Terminal "Save As" tip.
Thanks rharder for the .command tip.

continue scripting...
Cool. That only leaves one more thing. How do we open a file with a certain program from an AppleScript? I tried fumbling my way through it once, but I didn't get anywhere.
It's been quite a while since I've scripted. I tried for a little while to open a file with a particular application and quit with frustration. It Seems some AppleScipt events are handled a bit differently in OS X. I might give this a second try once 10.1 comes. I heard more apps are scriptable like Terminal.

I'll correct myself in the previous post. The saved Terminal file is not an executable but just a plain file. It just happens to have the file extension .term which when double clicked will open Terminal and then the executable it was saved from.

If you open this .term file in a text editor you will notice it appears to be in xml format listing the key and strings for Terminal's property list. If you scroll down a bit, notice the string for the key "Shell" is the path for the shell script in which this key references. So if you change location of your shell script you would need to change this string to reflect the change. Basically this file will open your shell script with the properties of the Terminal window you saved.

I experimented with changing text color by altering the string for the "Color" key. Can change the window title with the string from the "CustomTitile" key. I guess you can go to town with customizing this if you so please.

I also tried to accomplish what you would like to do with AppleScript but with shell commands (open a file with a particular app). I guess the object is to get a double clickable file to do what you want it to do. ex. open somefile.txt with TextEdit.
In this example I wrote out a shell script (which I'm fumbling over myself) like this;

/usr/bin/open -a /Applications/TextEdit.app somefile.txt

Saved it with the .command extension, and made it an executable. This would be your double clickable file.

Another double clickable file example that would open top in a shell with saved window parameters would be this;
The shell script:


Saved it with the .command extension,and made it an executable. Ran it. Adjusted the desired window size and saved it from Terminal with the "Main Window" option. Buff, your double clickable file.

OK, I had my fun.
How to open a file with a particular application that supports AppleScript.
TextEdit and a file in a users document folder used as an example.

In Script Editor code:

tell application "TextEdit"
close every document saving no
open alias "volumename:Users:username:Documents:file to be opened"
end tell

Use colons :)) to target specific locations on the computer.

More AppleScript Mac OS X specific info at...
Hmm, that might work. Is there a way to tell the Finder to open a file with a particular application, and achieve the same results as drag and drop?
If I understand what your asking, you want to be able to open a file with any kind of (or no) extension with a particular app?

If the file format can be handled by the application, I see no reason why that wouldn't work. You would use a similar tell block as my example in the above post.

Is this what you meant?