Good day, dear habravchane-makovody!
Today I will tell how to increase the efficiency of work in Mac OS X through the use of the console.

Lyrical digression
I think it is no secret that Mac OS is a Unix-based system, but it has been reworked almost beyond recognition. Even the console stuck to hell - not immediately find. And the whole system is aimed at using the GUI, with the default settings, even switching between the buttons on the Tab does not work - nowhere without the mouse. And nevertheless, the Poppy is not only windows, not only dock and the launcher. Mac is also all the power of shell scripts and console!
')
If you come from the world of M $, then for a start it would be nice to teach the general commands of the shell, for example, through this
tutorial . At a minimum, you need to learn the commands for navigating through directories and how to launch programs and scripts.
If you came to the world of Mac OS from the world of Linux and FreeBSD, you most likely know at least the basics of shell scripting. But for you, the article may be something interesting, because in the Mac there are unique console commands that are also useful to know.
Here are some of the features of the Makovsky console further and will be discussed.
Let's start the work

For starters, let's get rid of the standard terminal. Well, more precisely, we will put another - better. I personally prefer
iTerm2 , which is suitable for working much better than the system one. Although, he has some interesting moments (see the picture above).
Next, we may need additional tools, so install
MacPorts (although, of course, you can also use another package manager). Now we can install the necessary console utilities using simple commands. For example, we put Midnight Commander (in any case it can be useful), we type in iTerm2:
sudo port install mc
After entering the password and installing the dependencies and mc itself, we can start it, as you already guessed, with the
mc
command.

Oh God! We need the function keys, and they are pressed only through
Fn+F15
, what to do? Well, what - to climb into the system settings and turn it off. Otherwise it will be difficult to work with mc.
Another important note:
smoke manuals if something is not clear, type the
man
in the
man
console and get a detailed description of the command. (By the way, to exit from viewing the manual, you just need to press Q).
Continuing familiarity with the console. open
command
You can easily see that mc shows a lot more files and folders than the Finder. Of course, you can put a hack for him to display such files, but not everyone loves it. So it remains only to use
Cmd+Shift+G
and manually enter the folder name. But this method is not very convenient: there is no autocomplete, there is no possibility to see hidden files. Now back to the terminal. On Mac OS X, there is a great
open
command that acts as if the user had made a double-click on a file or folder in the Finder — opens a file, a bundle or link in a related program, opens a folder in Finder. Here is a simple example of working with this command: go to a hidden folder using mc (for example, / etc /) and type:
open .
Wuala! Finder has shown us our system folder! But that's not all, because hidden files are still not visible. For example, open our
.profile
:
open .profile
And - oops! - hidden file opened in TextEdit! Everything is very simple.
A couple of words about bandla
Everyone knows this, but just in case, I repeat: the usual programs in Mac OS X are actually folders with the .app extension and specially arranged content. It's easy to make sure of this: open the / Applications / folder in the Finder and do a
Ctrl+Click
(or
Right-Click
, if you want) on any program and select “Show package contents”.

From the console, this is also very clearly visible: mc displays these programs exactly as folders. And how to launch a GUI program from the console, you ask? Very simply, there are even two ways.
Method one, the simplest:
open /Applications/Safari.app
The program will be launched and the console will be free for further action.
Method two, interesting:
/Applications/Safari.app/Contents/MacOS/Safari
The program will be launched, but the console will not be released - it will wait for the program to finish and output everything that the program wants to bring into it. That is, in this way you can see the working log of some programs.
Another very important difference between these two methods: the second allows you to run two instances of the program, while the first activates an already running one, if there is one. So through the console, you can solve this problem (although, rather, a feature) makosi: you cannot run two copies of the program through the Finder, the dock and the launcher, but from the console, please, at least twenty-two.
Script?
I'll tell you briefly what the shell script is. In essence, this is a plain text file containing a series of commands to execute. The simplest script just runs all the commands in sequence. Scripts can be more complicated conditions, cycles and functions, that is, the script can be a very large and complex program. The main advantage of such scripts is that in their service all the power of console utilities from the world of unix, which more than compensates for the poverty of language constructs. I will give a simple example of a script that converts all .png files in the current directory, reducing their size to 200 pixels in width or height.
The example is the simplest, but it demonstrates the main thing: great power is hidden in the scripts.
AppleScript scripts
Mac OS X has several built-in mechanisms to automate actions. One of the very common is AppleScript, for which there is also a graphic editor-launcher. But you can also use such scripts from the console, for this there is a special utility -
osascript
. You can use it like this:
osascript myscript
or
osascript -e " "
I will not delve into AppleScript, it deserves a separate article, and not even one. So I recommend reading the
help or
Google on it .
Important: You can combine shell scripts with AppleScript scripts to maximize flexibility in your work! For example, I use such mixed scripts to automatically style a disk image: the disk itself is created using the shell (see below), and the background and location of the elements in the image is set using AppleScript.
There is also
Automator , but it is quite GUI-shny, so in this article it is pointless to consider it. He, of course, useful, but to the power of the console does not hold out.
Briefly about other useful commands Mac OS X
A complete (well, almost) list of unique commands for makosi can be found in one good
article (although the information there is a bit out of date), but I will briefly tell you about the most interesting ones.
To work with disk images there is a console utility
hdiutil
. A couple of examples of working with her:
More details about the possibilities can be found in
man hdiutil
.
If you are a makosi software developer, then you probably know about such a wonderful thing as Info.plist, and in general about plists. So, there is a very handy utility that allows you to convert .plist to .bplist, that is, into a faster and easier binary format of the sheet.
If you want to practice nonsense, you can speak any text from the console. For this there is a utility
say
.
You can also set a voice that the poppy will talk to us with:
say -v Bad habr habr habr habr habrahabr habr habrahabr
One problem - by default, this thing only speaks English.
Now take a screenshot of the command from the console.
You can also convert text between various formats from the console. For example, you can convert a .doc file to HTML:
Also textutil can convert encodings.
To change the system (and not only) settings, you can use the
defaults
command. We use it for the good! Finally turn on the display of hidden files and folders in the Finder!
defaults write com.apple.Finder AppleShowAllFiles 1 && killall Finder
The finder will automatically restart and will now display hidden files and folders! But again, not everyone likes it, so let's turn it off until the
parents get scared or delete the necessary files. To hide the already mentioned command, put 0 instead of 1. Well, for example, a
link to an article where many tweaks are described with the help of this command.
Something like a conclusion
Well, we have disassembled some interesting console features in Mac OS X. The article, of course, does not pretend to be complete and is, rather, a “lure” for involving makovodov in shell scripting and indeed in the console.
And may the power of Unix be with you!