AddThis Social Bookmark Button

Print

Control Your Mac from Afar

by Harold Martin
09/19/2003

There are many different ways to control your Mac -- even when you're not sitting at it. You might think that this level of flexibility would require special software. But no! If you're running Mac OS X, you'll be able to accomplish everything in this article without buying a single piece of software. As a bonus, you can also perform most of these tricks on the Mac you're sitting at right now. (Even though that wouldn't actually be remote controlling, now would it?)

The Tools We'll Use

ssh

ssh is the Secure SHell. It and its accompanying programs allow you to securely log in and copy files to other computers running an ssh server. The target Mac must have OS X's built-in ssh server enabled. You can do this by checking "Remote Login" in the "Services" tab in the "Sharing" System Preference pane. ssh is the "remote" part in "remote control:" we use ssh to log in to the Mac that we want to control, and then use the other technologies we'll talk about below to do the actual controlling. The computer you'll use to control the target Mac should have an ssh client. Most types of UNIX (including OS X) come with an ssh client, and clients are available on just about every other platform, including Windows and OS 9. If you're interested in learning more about ssh, I recommend SSH, The Secure Shell: The Definitive Guide.

Built-In Commands

On top of the normal UNIX commands we'll use, Mac OS X has a couple of extra ones that will be particularly helpful to us:
  • open is a command that can open a file, directory, application, or URL from the command line, just as if it was double-clicked in the Finder.
  • screencapture is a program that (surprise, surprise) takes a screen shot of what is on the computer's screen.

AppleScript

Mac OSX Conference

Session by Gordon Meyer:
Living in the Digital Hub: Your House and Mac OS X

Upgrade your digital life to include your house and living environment--controlling lights, temperature, music, and more--all from your Mac. We'll start with the basics of controlling lighting and other physical objects, then graduate to true automation which turns your home into a living entity that responds to, and anticipates, your needs.

O'Reilly Mac OS X Conference
October 27-30, 2003
Santa Clara, CA


AppleScript, as you probably know, can be used to control almost everything on your Mac (now even more so, with GUI Scripting). You can run AppleScripts from the command line with osascript scriptname. You can also write scripts on the fly with osascript -e 'line 1' -e 'line 2'. This is an amazing tool that we'll use a lot.

cron

We'll use cron to run AppleScripts and commands automatically at set times.

Remember ...

In order for open, screencapture, or osascript to work over ssh, you must have either ssh'd in as the user currently running, or you must run them with sudo. In other words, if user Alice is logged in at the actual machine and you ssh in as Bob, you would need to run sudo open file if you wanted file to appear on Alice's screen. On the other hand, if you logged in as Alice, you would only have to run open file. Keep this in mind; it's tripped me up before.

Let's Do It!

Now that you've got your tools in order, I'll walk you through a few tricks to get you started with remote-controlling magic. Well, it's not really magic -- just some good old-fashioned UNIX and Apple technologies.

Logging In

In OS X, open up Terminal and type:

ssh username@target's IP

Replacing the target's IP and the username you use on the target, as appropriate.

If you're using a non-UNIX ssh client, check its documentation for instructions on logging in.

After a successful login, you'll get a command prompt from which you'll execute the tricks below!

Seeing What the Target Is Doing

To start, go to the prompt and enter:
top

You'll see what processes are running, sorted by CPU usage. The processes whose names end in .app are OS X applications. If you want to see just the applications, enter:

ps -aux | grep '.app'

ps will list all processes, and grep will display all of the lines that contain ".app".

Now, if you want to see exactly what the other computer is up to, you could enter:

screencapture -x ~/screen.pdf

This will take a screenshot (without the "camera click" sound) that will be saved to the file screen.pdf. But how do you view it? By using scp to copy the screenshot to your computer! If you're still in your ssh session, you should type:

scp ~/screen.pdf yourusername@yourIP:

substituting your username and IP. Be sure to have the ":" at the end, otherwise scp won't recognize that you want to copy to another computer.

An alternate way to get the screenshot is to exit your ssh session and, at your local prompt, type:

scp otherusername@target's IP:screen.pdf screen.pdf

Again, substitute the correct IP and username.

Either way, you'll end up with the screenshot file screen.pdf in your home directory, which you can look at and see exactly what the other computer was doing!

Communicating With the User of the Other Computer

Now, what if you want to say something to the person using the other computer? To do this, we'll use the dynamic duo of ssh and osascript. Logged in over ssh, you can enter:

sudo osascript -e 'tell app "Finder" to activate' -e 'tell app "Finder" 
    to display dialog "I see you!"'

All that this does is run a two-line (two -es) script that activates the Finder (which is necessary in order for the dialog to have some place to appear) and then displays the dialog. You can use any AppleScript you want, and you can also get the results of what the user on the target Mac did. For example you could use something like this to let the user talk back to you:

sudo osascript -e 'tell app "Finder" to activate' -e 'tell app "Finder" 
    to display dialog "What would you like to say?" default answer ""'


Asking the user a question

In the text printed out over the ssh session, you will see a line like "text returned:Hi!, button returned:OK". The text returned will be what the user typed back.

But what if you actually wanted to talk to the user, with a voice? You would enter this:

sudo osascript -e 'say "Now I can watch you and talk to you"'
A couple of notes, though: I've sometimes had problems using speech with sudo, so you might also want to try this without sudo. Also keep in mind that this will have no effect if the target Mac is muted. You might want to use say and display dialog for maximum effect!

Closing and Opening Applications

Think back to the discussion of seeing what the target Mac is doing. What if you see an application that you want to close? From the listing you get from ps -aux, you can get an application's process ID (PID). You can then take that PID and pass it to the kill command:

kill PID

I've found this to be particularly useful when an application won't quit (even with force quit) or when it's in fullscreen mode (like a game). I can just log in from another machine and quit the wayward app, then get back to work. If something freezes so bad that normal kill won't, well, kill it (a rarity) then you can use "super" kill:

kill -9 PID

The only problem with killing an app is that it will immediately close down, giving the user no chance to save any open documents first. What if you would like to close down the application in a kinder manner? AppleScript to the rescue! You can quit TextEdit and still allow the user to save his changes by entering:

osascript -e 'tell app "TextEdit" to quit'

Be careful, though; if TextEdit isn't already open, then this script will cause it to open and then quit. Use ps to make sure TextEdit (or whatever application) is open in the first place. Also keep in mind that the user can simply click the Cancel button to keep the app from quitting.

Shutting Down or Rebooting

Suppose you want to shut down or reboot the computer. As before, there's the immediate UNIX way and the kinder (but user-cancelable) AppleScript way. The command-line way to shut down is:

sudo shutdown -h now

This will close your ssh session and shut down the computer, regardless of any apps or documents that may be open. To reboot, simply enter:

sudo reboot

Again, this will close everything down without mercy. To give the user a chance to save what they're doing (and yes, to cancel the shutdown) you can type:

osascript -e 'tell app "Finder" to shut down'

or

osascript -e 'tell app "Finder" to reboot'

to shut down and reboot, respectively. The only way a user can cancel this is to keep an application open long enough that the shutdown or reboot times out. But at least it won't give the user the "Are you sure you want to shut down your computer now?" dialog!

Using AppleScript GUI Scripting to Control any Application

Apple has released a beta scripting engine, called GUI Scripting, that allows AppleScripts to take full control of any application by directly interacting with its GUI elements (such as menus and buttons). First, download it from Apple's GUI Scripting page. There are also some example GUI scripts there that you should look over. Here is a simple script that puts the computer to sleep, something that can't be accomplished via the Finder's AppleScript dictionary or with regular UNIX commands:


try
    tell application "Finder"
        activate
    end tell
    tell application "System Events"
        with timeout of 30 seconds
            tell process "Finder"
                tell menu bar 1
                    click menu item "Sleep" of menu "Apple"
                end tell
            end tell
        end timeout
    end tell
end try

You should save this script it as sleep.scpt, preferably in a hidden directory (such as ~/.applescripts). The next time you log in, you can just enter:

osascript .applescript/sleep.scpt

though putting the computer to sleep will freeze your ssh session.

GUI Scripting will probably end up being one of the most useful tools in your toolbox, as what you can accomplish with it is (nearly) infinite, at least as far as controlling your Mac goes.

Automating Your Remote Control

Now that we've covered how to use a variety of UNIX commands and AppleScripts to control your Mac, there's only one major obstacle left to complete control: you have to be at a computer to do the controlling. Wouldn't it be nice if you could tell your Mac to take control of certain things at certain times?

Enter cron, the age-old UNIX command-scheduling tool. If you don't know how to use cron, you should read this tutorial or Chris Stone's "Learning the Terminal in Jaguar." Don't worry, cron is actually quite simple.

Since you can't be sure which user (in any) will be logged on when the cron job runs, you have to edit root's crontab.

With that in mind, you can grab your favorite UNIX text editor, run it as sudo, and edit /etc/crontab; or, you can use a GUI tool like CronniX. The only difference between CronniX and /etc/crontab is that in CronniX you don't use the who field. If you choose to use CronniX, you open root's crontab by clicking Open and typing in root for the username.

*    *    *    *    *    root    sudo osascript -e 
    'tell app "Finder" to activate' -e 'tell app "Finder" to display 
    dialog current date as string'

Though of course, you would never put that in your crontab, since it would drive you as well as all of your users crazy

Full Remote GUI Control

Finally, if you want complete control over the GUI, there's VNC. VNC allows you to open a window that shows the exact the desktop of the other computer, so you can move the other user's mouse, type in their dialog boxes, and otherwise annoy them. OSXvnc and Chicken of the VNC are my favorite server and client, respectively. After launching your VNC server, you can use any computer that has VNC client to log in and do your remote controlling. The biggest downside to VNC (and the reason it is not covered more extensively here) is that it is totally interactive and is difficult (if not impossible) to automate.


Have fun running a VNC server and connecting to it locally with a VNC client!

exit

You're well on your way to automating and controlling your Mac. Using both old and new tools, you should be able to use your Mac whether you're sitting in front of it, on another computer, or letting it execute timed tasks itself. Read sites like Mac OS X Hints and O'Reilly Hacks (and their books Mac OS X Hints: Jaguar Edition and Mac OS X Hacks) to find more tricks you can use.

With all the possibilities, I'll be excited to see what you come up with. Post your experiences and ideas in the Talkback section below!

Harold Martin is a freelance software developer and author. Visit him at his blog.


Return to the Mac DevCenter