How to launch apps on your iPhone with Alfred

Did you know you can launch an iPhone app or respring your iDevice using Alfred for Mac? It’s actually possible. You can invoke a hotkey, type Respring into your Mac, and your iPhone SpringBoard will restart automatically.

This tutorial is not for the feint of heart. It helps if you have a general understanding of OpenSSH, the command line, and scripting with AppleScript or Python. You will need to have a Mac with administrator rights and a jailbroken iDevice. Read on to get started…

For obvious reasons, you will need to download and install Alfred on your computer. Either install it from the Mac App Store or visit the Running With Crayons website.

First, you will need to install OpenSSH from Cydia. You will also need to install the latest version of APT 0.7 Strict. OpenSSH will allow your Mac to issue remote commands to your iPhone. APT Strict will increase the amount of commands your Mac can use.

Next, download enchanter from their Google Code page. Enchanter is a java library for scripting SSH sessions. This tutorial involves making a script for each command you want to execute on your iPhone. Each script file will include the credentials necessary to gain root access to your iPhone. I prefer to use Python to write my scripts, but enchanter also offers Beanshell and Ruby versions. For the sake of this article, I will use Python in my examples. Make sure you download enchanter-core-0.6.jar and enchanter-python-0.6.jar.

Copy these two files to /System/Library/Java/Extensions/ and type in your Mac password to authenticate. Create a folder to store your Python scripts. I created a folder in my user directory called “alfred-iphone” so that I can easily just put “~/alfred-iphone/script.py” into my Alfred Extension. If you want to be able to use Alfred for multiple iOS devices, you will need a different script for each one, so this folder naming convention is very practical.

Now you will need to create your first script. I will show you how to make a Respring script, to easily restart your iPhone’s SpringBoard. Make a new file with an app like BBEdit or TextWrangler and name it respring.py. Save it in the directory you created, i.e. “~/alfred-iphone” or whatever you decided to call it.

The contents of the file should look like this:


conn.connect("highPhone.local", 22, "root", "alpine");
conn.waitFor("root# ");
conn.sendLine("killall SpringBoard");
conn.waitFor("root# ");
conn.sendLine("exit");
conn.disconnect();

You will need to customize the first line with the details for your iPhone. The first value should be either the local domain of your iPhone or its IP address. If you know your iPhone’s name and it doesn’t have spaces, you can just put in iPhoneName.local, which in my case is highPhone.local. The second value is the SSH port number, which is always 22. The third value is the user name, in this case root, since we need the Mac to login as root to perform these actions. The fourth value is your root password. The default is alpine, but if you have changed yours, update the value with your password. Do note that your password is stored in plain text in this file, so if you’re worried about security, you may want to enable encryption on your Mac with FileVault.

The rest of the script is all about the Terminal experience. If you would like to see how it all works, open up Terminal, type in “ssh root@iPhoneName.local” and hit enter. Enter your root password when prompted, type “yes” if prompted, and then wait. If you receive a DNS Spoofing warning, simply delete the known_hosts file in ~/.ssh and try again. When you are prompted to enter a command, you will see “iPhoneName:~ root#” before the space to type. This is how enchanter knows when it’s okay to send a command. After a respring command is sent, i.e. killall SpringBoard (case sensitive), enchanter waits for the iPhone to finish, and knows its done when a new root# is present. For the script file, you only need to enter the end part of this string. “iPhoneName:~ root#” and “root#” will both work. Finally, we send the exit command to close the SSH session and the script disconnects.

Now to get this into Alfred, you need to create a new Alfred Extension. Open Alfred’s preferences, and click the Shell Script option to create a new extension. I called mine iPhone Respring. To change the icon, simply drag and drop an image file onto the existing icon. Every Mac has a collection of high quality iPhone icons located in “/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/” so I used one of those. Click the Create button. You can customize the Title and Description to your liking, but make sure the checkbox for Silent is checked and Action is left unchecked. The command should be “java -jar /System/Library/Java/Extensions/enchanter-python-0.6.jar ~/alfred-iphone/respring.py” all on one line. If you want to make sure this works, you can send that command into a new Terminal window (do not log in via SSH first). Save your extension and try it out.

To add Growl support, you need to create an AppleScript extension instead. Include this code at the top of the extension:

try
tell application "Growl"
set the allNotificationsList to ¬
{"Alfred iPhone"}
set the enabledNotificationsList to ¬
{"Alfred iPhone"}
register as application ¬
"Alfred iPhone" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application "Terminal"
end tell
end try

To launch the Python script, simply add:

do shell script "java -jar /System/Library/Java/Extensions/enchanter-python-0.6.jar ~/alfred-iphone/respring.py"
tell application "Growl"
notify with name ¬
"Alfred iPhone" title ¬
"Success!" description ¬
"You have successfully restarted your iPhone SpringBoard." application name "Alfred iPhone" image from location ¬
"file:////System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/com.apple.iphone-4-black.icns"
end tell

I recommend that you write, compile, and run this script in Apple’s AppleScript Editor app, included on every Mac.

You can create Python scripts for every action that can be performed from the command line. To update your Cydia sources, “apt-get update” or to upgrade all available packages in the background, “apt-get upgrade”. This can be extended by adding Cydia packages for the command line. Using Erica Sadun’s ericautilities, you can open a URL in Safari by using something like “openURL https://www.idownloadblog.com” or launch an application by its bundle identifier by using something like “launch com.Apple.Calculator”. Here’s how you would write launchcalculator.py to use with enchanter.


conn.connect("highPhone.local", 22, "root", "alpine");
conn.waitFor("root# ");
conn.sendLine("launch com.Apple.Calculator");
conn.waitFor("root# ");
conn.sendLine("exit");
conn.disconnect();

For advanced AppleScript gurus, you can add Alfred query options to your extension. For example, I named one extension “Respring” and then I have query options for “iPhone” and “iPad”. I have an extension named “iPhone” with query options including “respring”, “reboot”, and “update”. Here’s an example of an Alfred AppleScript Extension that uses queries:

on alfred_script(q)
try
tell application “Growl”
set the allNotificationsList to ¬
{“Alfred iPhone”}
set the enabledNotificationsList to ¬
{“Alfred iPhone”}

 

register as application ¬
“Alfred iPhone” all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application “Terminal”

 

end tell
end try
if q is “respring” then
do shell script “java -jar /System/Library/Java/Extensions/enchanter-python-0.6.jar ~/alfred-iphone/respring.py”
tell application “Growl”
notify with name ¬
“Alfred iPhone” title ¬
“Success!” description ¬
“You have successfully restarted your iPhone SpringBoard.” application name “Alfred iPhone” image from location ¬
“file:////System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/com.apple.iphone-4-black.icns”
end tell
else if q is “update” then
tell application “Growl”
notify with name ¬
“Alfred iPhone” title ¬
“Updating” description ¬
“Looking for new Cydia packages.” application name “Alfred iPhone”
end tell

 

do shell script “java -jar /System/Library/Java/Extensions/enchanter-python-0.6.jar ~/alfred-iphone/update.py”
tell application “Growl”
notify with name ¬
“Alfred iPhone” title ¬
“Success!” description ¬
“You have refreshed your Cydia updates.” application name “Alfred iPhone” image from location ¬
“file:////System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/com.apple.iphone-4-black.icns”
end tell
else if q is “upgrade” then
do shell script “java -jar /System/Library/Java/Extensions/enchanter-python-0.6.jar ~/alfred-iphone/upgrade.py”
tell application “Growl”
notify with name ¬
“Alfred iPhone” title ¬
“Success!” description ¬
“You have updated your Cydia packages.” application name “Alfred iPhone” image from location ¬
“file:////System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/com.apple.iphone-4-black.icns”
end tell
end if
end alfred_script

I’m sure many of you will create some awesome ideas. Be sure to share your experiences in the comments. If you are having trouble, post your errors or questions. Happy tinkering!