How To Create a Security Cam with FaceTime for Mac and AppleScript

Posted: October 15, 2011 by AppleWorld in Labels: , , ,
0



We bet that by now you’ve had the opportunity to try out Apple’s newly released beta of FaceTime on the Mac. Here at Mac|Life, we’ve definitely had a lot of fun testing it, but we recently thought it would be a cool idea to make it even more useful. What if you could use FaceTime on the iPhone 4 and new iPod touch to call your Mac to check in on your house or office, without being home to answer the call. Imagine your new MacBook Air doubling as a security camera.

While you could train your pets to automatically answer your FaceTime calls, we wanted our Mac to automatically answer the FaceTime call, giving us complete freedom when calling into our home or office Mac. So, we fired up our AppleScript editor to check on the possibility of automating FaceTime, but were completely disappointed to find that FaceTime is not scriptable.
But, there is a work around, and that’s what we will show you how to do in this how-to. And if you don’t want to get your hands dirty, we’ll provide you with the AppleScript project files below.

What You’ll Need:
- AppleScript Editor (located in /Applications/Utilities/)
- FaceTime beta (already set up)
- iPhone 4, iPod touch, or another Mac
- A little patience

Ease of Tutorial: Easy to Medium


1. Open AppleScript Editor


Click image to embiggen

The first step in creating our AppleScript application is to open the AppleScript Editor (located in the /Applications/Utilities directory). Once that Application is opened, paste the following code into the editor:

set TimeBeforeAutoDisconnect to 900 --Set the time here

--WARNING: DO NOT TOUCH ANY OF THE CODE
--BELOW UNLESS YOU KNOW WHAT YOU ARE DOING!
set FirstRun to 0
set now to current date
repeat
    if appIsRunning("FaceTime") then
        tell application "System Events"
            tell process "FaceTime"
                tell application "FaceTime" to activate
                delay 10
                if FirstRun is equal to 0 then
                    key code 36
                    delay 15
                    keystroke "F" using {command down, shift down}
                    set FirstRun to 1
                    set now to current date
                end if
            end tell
        end tell
    end if
   
    if (current date) is greater than now + TimeBeforeAutoDisconnect then
        tell application "FaceTime" to quit
        delay 3
        set FirstRun to 0
    end if
   
end repeat

on appIsRunning(appName)
    tell application "System Events" to (name of processes) contains appName
end appIsRunning

2. How the Code Works

Let’s take a walk through how this code works. If you’re new to AppleScript or have never encountered it before, you may be a bit scared just by looking at the code above, but with a little explanation, you’ll know just how it works. Note: if you don’t want to know how the code works, feel free to skip this section and move onto either tweaking the code or running the application.

Currently, the AppleScript code will just wait, constantly checking to see whether or not the FaceTime application is running. If it’s not running, then it won’t do anything except keep checking. As soon as the app launches, meaning that you are receiving a call, the AppleScript will spring into action, setting FaceTime as the foremost application. After 10 seconds of ringing, the script will press the enter key, accepting the call. We then wait 15 seconds before switching the FaceTime view to full screen. This 15 second delay allows for your two devices to achieve a video connection.

If you notice on the first line of the script we have a variable called “TImeBeforeAutoDisconnect” this variable allows us to set the seconds before the AppleScript will terminate the FaceTime program and return the script to it’s original state, allowing for a new FaceTime call. This is a limitation that we needed to implement because of FaceTime’s inability to be directly scriptable with AppleScript. Because there are no Apple Event listeners available for FaceTime, we’re not able to detect a new call, so we must rely on the FaceTime application launching as a sign of an incoming call. If the application is opened, then the script believes there is a call currently in progress. However, this time limit is something that you can change in the “Tweaking the Code” section below.


3. Tweaking the Code


Depending on your wants or needs, you may wish to tweak the code to provide more flexibility. With this AppleScript, there are two things that you can tweak to change the functionality of the Auto Answer script.

First is the auto disconnect time. Notice the variable “TimeBeforeAutoDisconnect” in the first line. This variable is set by default to 900 seconds (or 15 minutes). However, you can change the 900 seconds to whatever amount of time you wish. We recommend a shorter amount of time because this will be the amount of time it takes for the script to reset itself and the FaceTime application. Based on the default, this means that you can only call once in a 15 minute time period. Experiment around with the amount of time that suits you best.

The other functionality that you may wish to tweak is the ability to go full screen. In the program you will notice the following line of code:

keystroke "F" using {command down, shift down}

This code will press the Command + Shift + F key, allowing the FaceTime application to go full screen after a short delay. By default we include this functionality in the script, but if you don’t want the FaceTime application to go full screen, feel free to remove this line from the program before continuing.

Next, we will show you how you can save this script as a run-able application.

4. Saving AppleScript as an Application


Because this script can be used to automatically accept FaceTime calls and use your Mac as a security camera, we wanted to have the ability to keep the script running on our Mac while we were away from our house or workplace. So, we found that it was more useful as an application.



To save your completed script as an application, click File > Save As from the AppleScript Editor. In the drop-down save dialog, type in a name for the application, and specify a save location. In the File Format drop-down menu, select Application, and ensure that the options remain unchecked. When you’re finished, click Save. You have now created your AppleScript application that will automatically accept incoming FaceTime calls. Give yourself a pat on the back, and continue to the next section to learn about running your app.

5. Running the Application


To run the AppleScript application, navigate to the location where you saved it, and double click on it. It should begin running and you will see its icon in the Dock. If you want to keep it running, but don’t want the icon in the Dock, follow the instructions over at Mac OS X Tips to remove the application from the Dock.

When you receive a call through FaceTime, the script will kick in after a few seconds and take over, answering the call automatically. From your iOS device or other Mac, you can hang up whenever you want, or wait 15 minutes (the script default) for it to automatically hangup the call for you. After the script hangs up the call, you will be able to dial right back in.


6. Some Caveats


Because FaceTime isn't scriptable, there are some downfalls to our AppleScript that automatically answers the video call.First of all, we do have to specify an automatic hangup time. This time can be set by you, or the default is 15 minutes (900 seconds). You are able to hangup the call at any time on the initiating device, but you will have to wait until this timer expires before you can call your Mac back.

If you’re in a call and the timer automatically hangs up the call, you will be able to instantly call back in and have the script auto answer your call. If you go over the section on “Tweaking the Code,” you will be able to learn more about the functionality of the script and how you can set a longer time limit.
Another downfall is that it will accept any FaceTime call that you recieve, so if you plan on using this as a security camera, you may want to consider using a new email address with FaceTime on your Mac that no one else would know to call.

Also, when you wish to quit the application, you will need to do a Force Quit by navigating to the Apple Menu > Force Quit, selecting the application and clicking the Force Quit button. Due to the script constantly repeating itself this is the only way to quit it.

If you use a screensaver on your Mac, you will also need to disable it so that the AppleScript can take over and accept the call. You can disable your screensaver by going to Apple menu > System Preferences and clicking on the Desktop and Screen Saver icon. Once there, select Screen Saver and move the Start screen saver slider to the Never position.
Hopefully, the release version of FaceTime for Mac will be scriptable. Fingers crossed.


Download Our Code and Application


If you want to download our code instead of saving your own application, you can do so by clicking here. The .DMG file contains both the AppleScript code and the actual application that you can run on your Mac with the default settings explained in this article. Before downloading, please review the disclaimer below.


Disclaimer

This bit of code is relatively harmless, however, Mac|Life and the author assumes absolutely no responsibility if you were to break your computer. So, consider yourself warned if your Mac suddenly starts displaying dancing unicorns or cute kittens.

0 comments: