Android and Linux

Saturday, April 2, 2011

shell launcher for sl4a scripts

Scripting Layer for Android, or SL4A is an app which allows you to run scripts such as perl and python on your phone. The cool thing about them is that they have full access to Android API calls, unlike the Android command line. But sl4a scripts can be invoked from the command line and here's a script to do it.

Everything from "am" to the end needs to be on one line, but it's too long to post it that way here and I've always found the typical way of splitting lines on the web with a backslash confuses people, so just make sure "am...}" goes on a single line or copy it from the QR code below.

To run it, just execute the script with the name of an sl4a script as an argument. e.g., if you name the script slsh you run "slsh test.py"

#! /system/bin/sh
[ -z "$1" ] && exec echo "please specify the name of a script to run"
am start -a com.googlecode.android_scripting.action.LAUNCH_BACKGROUND_SCRIPT
-n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher
-e com.googlecode.android_scripting.extra.SCRIPT_PATH /sdcard/sl4a/scripts/${1}

This could give you the ability to do virtually anything on the phone through the command line by invoking sl4a scripts. sl4a would do the hard work, of course, but this gives you a lower level access to it. You could, for example, probably use it to easily ssh into the phone from the computer and send a text message or get the GPS location.

Copy "slsh" to your phone's clipboard with this QR code:

Followers