Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question
4  
Welcome to Ask Different! Some questions are more advanced than others, and that's okay; you don't need to apologize. If you have a question about Apple hardware, Apple software, other Apple products or services, third-party hardware and software for Apple products, and it is not about topics prohibited in the FAQ then you're in the right place to ask your question! So if your question is on-topic, it's welcome here, even if you are a 13-year-old (but no younger, per the terms of service). –  Daniel Lawson Feb 18 '12 at 12:42
3  
Great question - there are at least three non-trivial levels of answers to this question. I hope this gets a basic answer, a middle one, and a "John Siracusa dissects the inner workings of Mac OS X application binary structure" level answer so you can choose and learn from all. –  bmike Feb 18 '12 at 16:27
add comment

5 Answers

up vote 11 down vote accepted

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).

share|improve this answer
add comment

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

share|improve this answer
    
But sometimes the APIs end up calling commands you can access from Terminal. I've read reports (nothing I care to replicate on my own computer, thank you very much) that if /sbin/shutdown is damaged or deleted, the Shutdown command in the Apple menu ceases to work. –  Daniel Lawson Feb 18 '12 at 12:35
1  
Shell scripts do not have to run in a terminal window or any other soft of window. But if they produce output that is not redirected to a file then you may choose to run it in a window. Shell scripts can also run other programs that have windows. –  richard Feb 18 '12 at 23:16
    
@richard Absolutely right. Edited. –  Daniel Lawson Feb 18 '12 at 23:21
add comment

Applications can run CLI commands to perform a certain task, and some actually do; but due to efficiency issues, professional application developers avoid running the command and rather use the APIs that the command uses to perform the task required.

share|improve this answer
    
I am a professional software engineer. I call commands from my programs. I will even write some or all of them myself then write the gui in a different language. I would not however use the cp (copy) or mv (move/rename) commands as the api to copy or rename is simple. Some Free Software projects use this technique. Some expects have written a excellent but hard to use command line tools (e.g. to create an iso cd image, and to write an image to cd) the gui expert writes a gui that uses both these tools, basically allowing a user to configure the tools, to set them going, and monitor progress. –  richard Feb 18 '12 at 23:40
    
I used API just as simple word that the question used. Actually, I have re-wrote the answer several times each time making it for less familiar audience with under-laying code and naming conventions of API vs libs vs Frameworks etc. I meant no offense to you or your profession @richrad, nor to any other professional software engineer. If there is something you want to edit in my post that would clarify the matter further, please go ahead and do so. –  ismail Feb 19 '12 at 1:29
    
There is no offense, I am just clarifying. A good programmer will or wont call command line programs, the decision will be based on complexity and possibly (after measuring speed) on efficiency. For the small group of professionals that sell to the market, there may be a need to make your program complex; Calling a command line program, can seem just to simple. Therefore a rewrite is needed. If you do commissions or Free-Software then you do the simplest thing that works. –  richard Feb 19 '12 at 13:21
add comment

One of the great ideas behind Unix is (was) that if you got an idea for a program, then you would first write it as a shell script - essentially calling a series of commands.

Then, if the program proved to be useful, you could improve on its interface, have users test it, and finally, when you were convinced it was worth it, write a "real" program.

This, of course, was before graphical user interfaces took over, so a "program" here is a CLI command in itself.

You still see this approach in some modern programs, especially if they show system information. Want to build a simple app to list all files in a folder? Just run ls -al, parse the result and show it a table. Have fun with the various parameters, and you have your material for version 2.0

share|improve this answer
add comment

One class of programs that run terminal commands (even though it all starts with an API call) is the integrated program-development environments such as eclipse or Xcode. The collection of tools required for program development is prohibitively large to be contained in, and maintained for, an IDE. Instead, it builds a makefile (a kind of script) and runs it with unix 'make' (or an equivalent) to step through the compile, link, load, and debug process. Make, in turn, runs the compiler, linker, etc. using their command-line interfaces. This lets the IDE be relatively independent of the tool-set chosen by the programmer, and insensitive to updates to the tools.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.