Reprinted: Working with command lines completely-4: script-based on the Apple graphic interface

Source: Internet
Author: User

The following content is reproduced. The original author is Xu Yu, a computer Major Ph. D. Student. The original address for the http://blog.youxu.info/2008/10/19/macos-and-command-line-script/ reproduced here for exchange and sharing.

Fully working with command lines-4: script-based on the Apple graphic interface

I have been using Apple Mac OS x for two years. The more I use it, the more I feel that other operating systems need to learn a lot from Apple Mac OS X. in particular, the unified scripting interface of the graphic interface, I think this is a good inspiration for the future of the operating system, but also an indispensable part of the era of working philosophy with command line in the graphic interface.

The reason is that I recently wrote a series of Chinese articles, because there are almost no mathematical formulas, and I like the work of plain text, so I chose restructuredtext in the layout. generally, I work in VIM directly in English. however, Vim's support for Chinese is not as good as imagined. At least Chinese input methods are often garbled. textmate, the name of the world, is good, but the display of Chinese is only half a word that is so wide, it looks uncomfortable. other editors are either charged, too large, or not easy to use, so they finally use Apple's textedit.

However, people who write technical articles know that writing articles is not just as simple as typing. Especially for technical articles, they often need to leave the editing environment to run scripts, check Google, and read wiki.
(I don't know if this is the case for others. At least I have to do a lot of homework to write good articles.) especially because I am using restructuredtext,
One of the most basic requirements is to process the HTML/latex generated by the current text and then Preview It. These requirements are supported by native in vim and textmate,
It's easy to do. But in a software such as textedit, there's no way at first glance. Fortunately, the UNIX operating systems, especially apple, have been
There must be a simple solution for simple things, and a complicated things should be possible. Such a design idea provides more than one solution to the problem.
Because Apple designed something called a "scripted application", these tasks can be easily completed. After several days of research, I learned something and shared it with you.

1. The most often ignored menu bar, service menu.


It may be easier to use a specific example. Anyone who uses Gmail knows that if the email contains a number string such as 90081243455 or
Beijing is an address like YY, XX Road, Xicheng district. Gmail displays the address "using Google tracking express parcels" or "using maps to view addresses"
In other words, Gmail will recognize these specific styles and give you a context-related (that is, services related to your content.
This kind of service is considerate and comfortable. I believe many people like it very much. Generally, we call this context-related service.

Besides, Firefox. recently, Firefox launched a very considerate plug-in called ubiquity. I can hardly leave it in Firefox: To select any line of address, you only need to press a few keys to check Google map. select any term and check wikipedia immediately. generally, we call this a selected service.

The service menu on apple is such a context-based and selected-based service. For example, if your machine is installed with Skype,
Then you select a phone number. In this case, you can choose Skype> call this number. Skype from the service menu.
This call is automatically called. Or if you select a word, the service menu will automatically display: Look up in the dictionary.
After you select it, the dictionary on your machine will jump out and present the meaning of the word to you. All of this is based on an apple System Service Framework.
Simply put, each application tells the system what to do when you give me the information. In this way, when the selected context meets the conditions,
You can select this service from the menu. this is very convenient. for example, the boss often writes in the email: What did you do this week. then, I can select the entire sentence and press ⇧ ⌘ y.
In this way, the entire sentence is recorded in stickies, which is equivalent to writing a note at any time.

The existing menu bar is good, but it is more fun to write by yourself. fortunately, you do not need to write menus using Apple's raw obj-C. there is a powerful tool called thisservice,
It can package Ruby, Python, applescript, and shell scripts into services.
For example, I spent one minute writing a very simple script called translate yesterday, and submitted the text to Google Translate through python,
Returns Chinese. When reading news articles with many unknown words, you only need to select a paragraph and select this translate. A window for translating words into Chinese appears.
With the help of this software, any script can be clicked to become a service: I will not detail it in detail.

In general, we can note that when using computers on a daily basis, some content (text or images) in an application is often used as input to another program,
To another program for processing, such as the selected word dictionary, the selected address to view the map, the selected phone number to call, the selected Sentence Translation, and so on, these requirements exist.
The Windows family solves the problem through program switching and clipboard, while Apple does it through a unified interface. The idea is very similar to the Unix pipeline.
(Of course, the Windows family has never had the idea of a Unix family pipeline, so I cannot think of a uniform interface design ).

In Apple, the unified script interface can be used to handle communication problems between programs such as services. This is something the Windows family never imagined. At the same time,
According to my personal experience, this idea has not been fully implemented in the Linux/X11 family. In the Linux Command Line, we all know that different gadgets are orthogonal,
The output of a program is the input of a program, and the Unix philosophy has been flourishing. Until today, normal Linux/Unix users will also use pipelines to communicate between programs every day.
However, after the appearance of the graphic interface, such a good tradition does not seem to be inherited in the X11 system. For example, you cannot select a text and send it directly to the WC computing words,
It is because the graphical interface has no obvious concept of input and output. Strings and images have become the basic unit for transmission between programs. In this case, it is necessary not only to take files as the basic unit,
The communication is based on the selected strings, sentences, links, pictures, and so on. At this level, Apple does take the lead. (Of course, emacs Vim
It can also communicate with and call external programs, but not the whole system ).

2. The simplest language in history, applescript.


The old saying goes: when the great god knuth visited Apple, the president Steve Jobs played the glorious tradition of bragging and said: Professor Gao, you are here. I especially worship you,
I have read all of your books. You all know that if jobs really read all of Professor Gao's books, 99% of professors in the computer science field at universities around the world will basically commit suicide to jobs. So,
Master knows that he is a leader. The following story is divided into two versions. The first version is the version of Silicon Valley. Master immediately said, "Steve, you have been fooling around again.
The second version is my version: said Gao ye, I am not angry when I hear it. I thought, you and I work together as the two most familiar with font design in Silicon Valley, I am not good at offending you either,
I also asked you a simple question, which books have you read. as a result, Gao ye turned his eyes and asked questions: Mr Jobs, what is the simplest programming language in the world?
Steve Jobs was a tech-savvy master. He remembered that the things he had done when he was working with Woz were called Basic. After hearing the name, he thought it was simple and said: it was basic.
Grandpa Gao said, Is there anything simpler than this? (say Grandpa Gao thought, I hold Tex, metafont, mix, literate in my hand.
Programming, cweb, and other trump cards, I don't believe you can't say that you can't do anything about it). How can Steve Jobs realize Grandpa Gao's painstaking efforts and he won't program,
As a result, I wrote that our company is developing the simplest language in the world recently: Apple Script. Grandpa Gao knew it was just a blow,
So I immediately replied, "Steve, you have been confused again. Which kind of software did Steve take, so I asked the Development Department to develop a language that even he knew how to use?" applescript
It was shaken out.
(The above story is purely nonsense)

How simple is applescript? It's just as simple as speaking. I have also written an introduction. Here is another example:


tell application "TextEdit"
set filename to name of document 1
print filename
end tell

Look, it's English! Such a tiled, straightforward, almost non-syntactic programming language is really the simplest programming language in the world.

Although this language is simple, it is not very powerful. almost all applications on Mac can be controlled using this language. In particular, iTunes even has a dedicated website dedicated to allowing you to download the control script.

Applescript has two benefits: 1. It is supported by apple native and can be directly compiled into apple applications for use. Therefore,
Simple task processing can all be implemented in this language. For example, the service we mentioned above can also be implemented using applescript. 2.
He can interact with other applications. In fact, in the Apple system, after script bridging,
In theory, C/JavaScript/Python/Ruby can interact with applications, but applescript supports better and more directly.
For example, to obtain the name of the currently played iTunes music, you can use applescript:

tell application "iTunes" to get the name of the current track

You can also use Python

from Foundation import *
from ScriptingBridge import *
iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")
print iTunes.currentTrack().name()

Python is obviously a little bulky.

At the same time, applescript is also used by Apple's small and medium-sized robots Otto.
The underlying technology plays a fundamental role in the entire system. Although learning applescript is very inefficient,
However, it is sometimes useful to do some small tasks on apple. For example, I used textedit to support the automatic compilation of zookeeper R, that is, I first wrote a series of scripts,
Obtain the current file name, and then use the do shell script command to call make. In this way, the script can be called to complete automatic compilation.
There are two ways to use the shortcut key to call the script. One is to copy the script to the script directory of the application and add a menu item. The other is to use quicksilver for help.
The first method should be the simplest, but I have not tried it successfully. Interested readers can read this article.

3. quicksilver


Me
I have mentioned quicksilver before. At the beginning, I couldn't realize his magic. I didn't realize it until recently when I was playing script control. Simply put,
Quicksilver is a program that allows you to control programs with a keyboard. Or, Quicksilver is the Keyboard Command hub. For example, to play music, you only need to activate it with one click.
Quicksilver, play again (sometimes do not play all), Press enter. iTunes to play the music. Any control system script we mentioned above,
They can all be controlled by Quicksilver. I also mentioned in the article that "if you are an Apple user, you don't need quicksilver or command line,
Then you use Apple for windows ".

Quicksilver, as a feature for quick app startup, must be familiar to everyone. You can easily control iTunes. I will introduce a feature that is ignored by everyone:
Set context-related shortcut keys. Traditionally, the shortcut keys are disabled by the application. Even if you write a script to expand the application, you have to call them with a few clicks,
Instead, you cannot set keyboard shortcuts to call. There are three solutions. The first one is what I talked about in the first part. It is encapsulated into a service. The advantage is that a shortcut key can be encapsulated,
The disadvantage is that this service is useful globally, no matter which application you are using, this service can be used.
The second is what I mentioned in the second part.
Method. The third method is to use the trigger function of quicksilver. The Qs user can call up the trigger using the trigger, and then add
Custom trigger: point the target to an applescript you have written, assign a shortcut key to the trigger, and set the range to use.
Scope is limited in some applications. In this way, you can write a simple script without any effort, you can put your textedit on your computer
It is also transformed into a powerful editor.

Conclusion: If you are unplugging your mouse
As mentioned in this article, the use of the keyboard can force people to use efficient, concise, and correct tools. With the appearance of graphic interfaces and multimedia, the mouse has become the most common action.
In fact, by carefully analyzing and clicking the mouse, we can find that there are only two types: one is to select the content with the mouse and prepare some tools and programs to process the content, another option is mouse selection.
The former selects content, and the mouse is efficient. the latter's selection of operations is inefficient (because keyboard shortcuts and command lines are more efficient ). therefore, if we want to work efficiently,
We need to avoid the latter operation and further free the former.

The avoidance of the latter is simple: unplugging the mouse and forcing the liberation of the former with a variety of services, such as ubiquity
As well as the rise of context-sensitive programs, we do not need to copy between different programs, but directly communicate through standard interfaces between programs, so that we do not even need to switch programs.
All of these should be done based on command lines and the background, rather than by distracting the switching program. These new tools are more in line with the needs of a highly efficient modern people,
It can improve your efficiency. Of course, it is not just tools that decide the efficiency of your work, so don't be infatuated with these tools. In order to achieve 1% efficiency improvement,
However, over-emphasizing the various tricks of tools reduces efficiency. (warning: these tools are very attractive and easy to fall in love with them when they are used. remember, efficiency is the best criterion for judgment :).

I wrote a total of five articles in the "working with command line" series. I wrote a lot about some tools, some of which I spent a few words. basically, what I mentioned above,
I use things on my own every day and I think they are very useful. I also use the philosophy of "smart and get things done" when writing,
We will only introduce the well-designed clean software. Readers who want to practice GTD can choose their own tools. There are more GTD software that works entirely on the keyboard/command line,
You may also recommend it to me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.