In their simplest form, do programs just run terminal commands?

Say for example in a program it lets me select one folder and copy it to another location, on a very basic level is that application running commands that I could run in terminal or is it using some internal OS API to make that move?

Please spare me any vicious responses; I'm just curious and totally aware it can be perceived as a question a 13-year-old would ask.


Solution 1:

Conceptually, it always uses OS API - the question is only in what way. There are essentially three options.

  1. using low-level OS API (system calls) directly. In your example the task is quite involved: get the list of items in the folder, check the type (folder, file ...), for each of them create the corresponding item in the target folder, for files read the content from the source, write to the target file etc. Since the task is so complex, it is easy to get something wrong, so most applications will avoid this.

  2. use a library (API) that simplifies the task. For example the Apple Cocoa framework provides NSFileManager class with the copyItemAtPath:toPath:error method which does all the dirty work using low-level OS API, so the application doesn't need to use low-level API itself but can rely on something that requires less work and is always present in the system. Also Apple is likely to make sure that it works well.

  3. use an external process to do the task. In this case the external process will be using one of the two methods above to do the work. The application has to start such process, monitor it and wait until it is done. It is likely that such process can be run as a command line tool, so this would likely be a command you can run in Terminal. It is not guaranteed, but very possible.

Most applications will use option 2. because it is more simple than 1. and more safe and efficient than 3. In order to run an external process you need to set it up properly and you have no control over what it does. For example it is much harder to figure out what exactly went wrong in case of a failure and it is hard to know what it is doing (e.g. show progress). That is why in most cases developers will likely choose option 2, but there is no guarantee. A notable example are applications that use shell scripts for customization - such as installers.

Advanced user note: you can use dtrace facility of OS X to find out what a particular application is doing. For example, you can check any processes it is spawning so you would see the tools it is using (see execsnoop).

Solution 2:

I'm afraid the answer is "it depends, but usually the second." Actually, even if a GUI program is running terminal commands, it runs them by calling an API.

A program that is simply a list of terminal commands is called a shell script. Such programs can run in Mac OS X, but they have to either run in a Terminal window or launch a program that uses the GUI if you want to see their output. Other programs can call command line programs through internal APIs.

While most development questions are off-topic for this site, one example that is actually on-topic involves running Automator.

One option within the list of internal commands a program created in Automator can call is the ability to call a shell script, or list of Terminal commands. But that is just of of many options available within its internal API.

enter image description here