Create your own Mac robot assistant

Source: Internet
Author: User

Since reading the iron Xia series, I have never been tired of thinking about automation, monitoring, and warning. If you use an iPhone, you can use Siri on your mobile phone to perform operations (such as calling a contact) you want ). However, I have always been facing computers for a longer time than mobile phones, so I always like computers to be smarter. In fact, the "intelligence" I want here refers to a kind of "pseudo-Automation", which generally includes two features: automatic processing + timely reminder.

Mac OS X is derived from the open-source operating system Darwin (Unix-like operating system ). Thanks to the inherent hack culture, mac OS x is very programmable and many shell commands are open to the outside for system monitoring and operations. This makes my thoughts not out of reach!

When I had nothing to do over the past few days, I wrote a series of automatic services open-source code on github (services are not enough and are not fully built ). The list of projects that have been built or are being built is as follows:

There are many other services that can be compiled. For example, you can refer to the human's schedule. Let the robot automatically remind you of what you are advised to do during the current time period.

Currently, these services have run well on my BNS (most of them are self-starting deamon programs ).


First, let's analyze the possible services. In fact, services are divided by their lifecycles and can be simply scored as: Short services that run in real time (such as reporting time); when the system is started, deamon service of resident systems (memory, CPU, battery power monitoring/alarm light ).

Most of the service modes are based on the classic "Request/response" mode, which is no exception here. Because mac OS x system programming is not involved here, it cannot enjoy system-level benefits (such as signal mechanisms and system events ), this also makes it difficult for you to implement a set of custom protocols (because you need to use message and event mechanisms as necessary ). While writing a while true done is very unreliable, especially when deamon runs in the background (this greatly occupies the CPU, causing the CPU to keep idling), the CPU temperature soared.

Based on the above reasons, we finally chose the existing standard protocol (http) and its service processing program. Node. js is undoubtedly the best choice for building an http server, and its features are also very suitable for such requirements (single-threaded, event-based ). Therefore, node. js is used to run an http server on the local machine as a service container.

How to start the service

Because these services are hosted inside the local-started http server, to start it, you can only send requests to it locally. For example, if you want to open the goAgent, you may first think of opening the browser, and then enter:


Then, press enter to request a service. Yes, this is one of the ways (this also provides a way for many people who hate command lines to stay away from it ).

However, if you can only send requests to the http server in this way, the limitations are too great. This affects the services we want to directly start running in deamon mode when starting the system (because the above request method must be supported by a browser and manual operation is required ). Fortunately, the curl Command provided by shell supports directly sending requests to any accessible http server in the form of command lines. In this way, we can write a script to initiate a request in the following way, trigger the deamon service, and run it automatically upon startup.

curl http://localhost:9876/sayHellosleep 10curl http://localhost:9876/proxy_onsleep 10curl http://localhost:9876/weathersleep 10curl http://localhost:9876/memoryMonitor?opt=d          #opt:d run as deamon

We can see that the following parameters can be carried. The first opt (option) value is d (deamon), which indicates running in deamon mode. Therefore, if you want a service to run in both instant request and deamon mode, you can make a judgment within your service:

if [[ $# -eq 0 ]]; then    speak "battery checking!"    monitor    exit 0fiwhile getopts ":d" optnamedo    case "$optname" in    "d")        echo "monitor_deamon"        monitor_deamon    ;;    *)        echo "others"    ;;    esacdone

The so-called deamon mode is actually a while-true program, which can be repeatedly executed in the run-sleep-run-sleep mode:

function monitor_deamon (){    while [[ true ]]; do        monitor        if [[ $CONNECTED != $CONNECTED_TMP ]]; then            if [[ $CONNECTED = 'No' ]]; then                speak "AC Power disconnected!"            else                speak "AC Power connected! Battery is Charging now!"            fi            CONNECTED_TMP=$CONNECTED        fi        sleep 60    done}

However, no matter what the request is, a sub-process is enabled to execute in the http server, so it will not affect any other services or block any other requests:

var exec         = require("child_process").exec;var shellCmdList = require("./shellCmd").getShellCmdList();var url          = require('url');var queryString  = require("querystring");

function battery (request, response){    var paramObj=queryString.parse(url.parse(request.url).query);    var cmd=shellCmdList["battery"];    if (typeof paramObj !="undefined"         &&         typeof paramObj.opt != "undefined"         &&         paramObj.opt=="d") {        cmd+=" -d";    };    exec(cmd, function(error, stdout, stderr){        logExecInfo("battery", error, stdout, stderr);    })    response.writeHead(200, {"Content-Type": "text/plain"});    response.end();}

At the same time, because most services do not require too much "real-time", the above run will run for at least 60 seconds, and it takes about one second to run itself once. Therefore, the cpu usage is almost zero and negligible (dozens of such deamon services can be ignored if you have at least i5 or above processor capabilities ).

The "pseudo-Automation" mentioned above requires at least two features. Among them, automatic processing is handed over to these services, and instant reminders obviously need to rely on "voice reminders ". This is also a major feature of mac systems. It does not require any third-party program dependencies. It can be "spoken" in itself, and supports basic languages and automatically selects voice libraries. You only need to open terminal and enter the following command:

say -v alex 'Hello, Kobe bryant'

You can have it speak (the-v indicates that you can select a specific language library. Here is the alex pronunciation, and you can select Chinese )!

Therefore, before using this service, you should first enable this function of the system. For details, see my other article: "Python script for automatic voice broadcast weather at Mac startup". In addition, you should also know how to make a script program run automatically when it is started, which is also described in this article.

As long as you can get it, and you are willing to implement it (first you have to use shell or any other script language supported by mac), you can try the automation service you want to implement.

Mobile Terminal Control

If your computer and mobile phone are connected to the same Wi-Fi, then you can also use the mobile browser to complete the httpserver on your computer (although your httpserver listens to your local machine [localhost/], but in the LAN, your computer still has an IP address in the LAN to uniquely identify your computer, so the effect is the same ). You just need to open system settings-network settings to see what IP is displayed in the Wi-Fi column, and then modify the above localhost to the IP address in the mobile browser, but the port is required.

Of course, if you develop an iPhone and can write an exclusive client for it, that would be even better!

Remote Control Conjecture

Run httpserver and client on a machine. Is it possible that a remote computer can be controlled like a normal Website access? I have written an article about XXX's remote control over computers through the mail protocol (of course, it also relies on the http protocol, but it only serves as a transmission medium ). Now we have httpserver, and everything is simple. You only need to expose your current host ip address (of course, you must authorize access to these services ).

In addition, if you think it is unrealistic to expose your local computer to the Internet. You can use a proxy or intermediary mode. This is easy. You can apply for a proxy server on many app engine platforms. In this way, you only need to directly interact with the proxy server of the server you want to manipulate. The proxy server can temporarily cache your service commands. the http server polls the proxy server to obtain the commands you receive at intervals.

Mac makes life better

In fact, you will find that all repeated actions should be replaced by programs and then handed over to the computer for execution. At least unix operating systems support this, but mac OS x has always advertised itself with a high level of user experience because only a few programmers are supported, therefore, it does provide automated tools for many common users who are not programming and want to reduce repetitive work.

Apple script

A script language applicable only to mac operating systems.

Automatic or tool

In the left box, you can concatenate repeated process operations, let the system act as a "robot" to perform boring and repetitive tasks for you (for example, rename all the file names in a folder, go to a website and follow some rules to capture specific images ...).

Voice dictation provided by mac

I tried it, but the recognition rate is not very good unless you are very standard in English.

About vino

Vino is the new nickname of Kobe Bryant. It indicates that the old wine is getting better and better!

Enjoy & Have fun!

Related Article

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