ASE Scripts

Search This thread
I wanted to start a thread to post ASE scripts. They support three different languages and while gscript is amazing, this seems to have a lot of availability for saving the time of making entire programs for one or two functions that don't need an entire UI. Plus they can run after you close ASE, so its not a once and done environment. I would like to compile scripts here to make them easy to find.

Mods: This is not a build related topic, it spans all builds. It is not a duplicate, as the only searchable post was a link to the program. It is development because while the scripts can replace programs (in which case id post in app forum), I want to use this to do nodding and on-the-fly enhancements (development forum)
 

ale6

New member
Feb 14, 2009
2
0
Am I the only person who thinks that ASE is completely useless ?

Actually, the method used is a HUGE hack (using JSON embedded in a TCP socket is not the cleaner thing I saw) -- ok I'm not so deeply into ASE source code, but still.

... and, the API available is very very lightweight compared to Android's standard library.

Why not having an app which just exports the current Context object into a Beanshell context ? (and maybe having access to the interpreter via adb). Here, we could have access to the whole Java APIs and do nearly exactly what we can do know using plain Java code. It would be clearly awesome !

A better thing would be using Groovy, but it doesn't seem to like dx.

I've already tried to launch the Beanshell interpreter via ASE, but it fails (when the same script works on my computer).
 

corp769

Senior Member
Feb 8, 2009
211
1
I honestly like the idea of the ASE scripting because it gives people the ability to write scripts and learn about it.
 

mod1

Member
Dec 2, 2008
22
0
Great!

This is great. Just one thing I'd change: It would be nice to split the daemon package and the language support packages.

I, for one, have debian installed and already running a python environment with many extensions installed. I'd prefer to install just the "service" part of ASE, copy android.py to my site-packages directory and integrate it into existing scripts. I'd keep ASE running on a fixed port and start modifying existing scripts to utilize the ASE service.
 

mod1

Member
Dec 2, 2008
22
0
Mini Howto: Using ASE from debian

I wanted to use ASE from the debian installed on my android phone and did some minor patching to accommodate the fact that debian still uses python 2.5 whereas ASE requires python 2.6.

Here's how you do it:

Install the required packages:

Code:
# if you don't have python already
apt-get install python
# python 2.5 doesn't have json but this package will do.
apt-get install python-simplejson

Copy android.py from ASE to debian:

Code:
cp /data/data/com.google.ase/python/lib/python2.6/android.py /usr/lib/python2.5/site-packages/android.py

Apply this patch to android.py, to remove python 2.6 dependencies:

Code:
cd /
patch -p0 <<EOF
--- /data/data/com.google.ase/python/lib/python2.6/android.py   2009-06-14 22:48:06.000000000 +0000
+++ /usr/lib/python2.5/site-packages/android.py 2009-06-15 00:58:31.000000000 +0000
@@ -14,7 +14,7 @@
 
 __author__ = 'Damon Kohler <damonkohler@gmail.com>'
 
-import json
+import simplejson as json
 import os
 import socket
 import sys
@@ -25,7 +25,8 @@
 class Android(object):
 
   def __init__(self):
-    self.conn = socket.create_connection(('localhost', PORT))
+    self.conn = socket.socket()
+    self.conn.connect(('localhost', int(PORT)))
     self.client = self.conn.makefile()
     self.id = 0
 
EOF

That's it.

To use it:

Activate ASE and start a terminal. (I hope future versions of ASE will run the service independent of the terminal and will no longer require this).

In your debian terminal:
Code:
export AP_PORT=$(netstat -napt|sed -n 's/^tcp.*127.0.0.1:\([0-9]*\).*LISTEN.*ase$/\1/gp')
python

The above export is required because ASE currently uses a random port. I hope future versions of ASE will allow selecting a fixed one.

Now you can use ASE from your debian's python, the same way you would in ASE's terminal:

Code:
Python 2.5.2 (r252:60911, Nov 15 2008, 00:34:24) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import android
>>> a=android.Android()
>>> a.makeToast('debian')
{u'result': None, u'id': 0, u'error': None}
>>>

Now you can start porting scripts to use ASE :)
 

jinx10000

Senior Member
May 2, 2009
336
21
I have been trying to figure out how to reboot my phone using any of the interpreters in ASE.

I have tried the following in Lua.

os.execute("reboot")

and I get permission denied.

I have tried
os.execute("su")
os.execute("reboot")
but then the terminal changes from $ to # and nothing happens.

Get the same results using os.system() commands in python.

Anyone have any ideas on how to make this work? Seems like it ought to be simple but I have no experience in these languages.
 

hammersend

Member
Aug 9, 2009
10
1
Script to query Amazon

This isn't much but I'm an Amazon book junkee and I've tried all the bar code scanners in the market and none of them will just take me directly to the Amazon page for an item I scan without having to click several different buttons first.

So, without further adieu:

Code:
import android

"""start barcode scanner and scan barcode"""
droid = android.Android()
code = droid.scanBarcode()
ISBN = (code['result']['SCAN_RESULT'])

"""open browser at the first google i'm feeling lucky link which typically will be the amazon page for the item"""
url = "http://www.google.com/search?hl=de&q=site%3Aamazon.com+" + ISBN + "&meta=lr%3Dlang_en&btnI=I'm+feeling+lucky"
droid.startActivity('android.intent.action.VIEW', url)

The only issue is, sometimes, the bar code scanner goes into some kind of endless loop and just keeps restarting every time I scan. I think it's a bug in the zebra crossing app, but any help would be appreciated.

I must say, I am very excited about being able to use python on my G1, as maybe I'm just an idiot, but I can't seem to get into the standard sdk with its multiple points of entry, and having to deal with eclipse. I also really can't wait for the ability to write and run full fledged apps that can take advantage of the full widget set.
 

hammersend

Member
Aug 9, 2009
10
1
@jinx10000
I figured out how to execute os commands as root.

I wrote a small python script called testing.swapon.py that could obviously not be successfully executed as a regular user containing:
Code:
import os
os.system("swapon /dev/block/mmcblk0p3")
and placed it in /system/sd.

Then at the terminal emulator, not the ase but the regular terminal I downloaded from the market, and as the regular user, i.e. not root, I executed
Code:
$ su -c "/data/data/com.google.ase/python/bin/python /system/sd/testing.swapon.py"

And surprise, after running free, I noted that my swap partition was indeed mounted. And just to be sure, I unmounted it as root and did the whole thing over again. So, it looks like we can do some interesting root stuff with the ase scripting languages. Now, people just need to get interested in this so we can get some momentum and some cool scripts written.
 
Last edited:

dsgjersr

New member
Aug 10, 2009
1
0
The previously mentioned "export AP_PORT" doesn't work for me with the new ASE (r16)
The following one does:

export AP_PORT=$(netstat -napt|sed -n 's/^tcp.*127.0.0.1:\([0-9]*\).*LISTEN.*com\.google\.ase\?:\?/\1/gp'|sed '/.*/q')
 
Last edited:
Mar 31, 2008
19
0
Is there an update to this post now that I can run python 2.6 in my debian chroot and ASE now has a server? I'm kinda confused....


I wanted to use ASE from the debian installed on my android phone and did some minor patching to accommodate the fact that debian still uses python 2.5 whereas ASE requires python 2.6.

Here's how you do it:

Install the required packages:

Code:
# if you don't have python already
apt-get install python
# python 2.5 doesn't have json but this package will do.
apt-get install python-simplejson

Copy android.py from ASE to debian:

Code:
cp /data/data/com.google.ase/python/lib/python2.6/android.py /usr/lib/python2.5/site-packages/android.py

Apply this patch to android.py, to remove python 2.6 dependencies:

Code:
cd /
patch -p0 <<EOF
--- /data/data/com.google.ase/python/lib/python2.6/android.py   2009-06-14 22:48:06.000000000 +0000
+++ /usr/lib/python2.5/site-packages/android.py 2009-06-15 00:58:31.000000000 +0000
@@ -14,7 +14,7 @@
 
 __author__ = 'Damon Kohler <damonkohler@gmail.com>'
 
-import json
+import simplejson as json
 import os
 import socket
 import sys
@@ -25,7 +25,8 @@
 class Android(object):
 
   def __init__(self):
-    self.conn = socket.create_connection(('localhost', PORT))
+    self.conn = socket.socket()
+    self.conn.connect(('localhost', int(PORT)))
     self.client = self.conn.makefile()
     self.id = 0
 
EOF

That's it.

To use it:

Activate ASE and start a terminal. (I hope future versions of ASE will run the service independent of the terminal and will no longer require this).

In your debian terminal:
Code:
export AP_PORT=$(netstat -napt|sed -n 's/^tcp.*127.0.0.1:\([0-9]*\).*LISTEN.*ase$/\1/gp')
python

The above export is required because ASE currently uses a random port. I hope future versions of ASE will allow selecting a fixed one.

Now you can use ASE from your debian's python, the same way you would in ASE's terminal:

Code:
Python 2.5.2 (r252:60911, Nov 15 2008, 00:34:24) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import android
>>> a=android.Android()
>>> a.makeToast('debian')
{u'result': None, u'id': 0, u'error': None}
>>>

Now you can start porting scripts to use ASE :)
 

Firerat

Senior Member
Feb 24, 2009
3,848
185
Anyone make (or find) any cool scripts?

I did a toggleswap.sh script ages ago

it gets around the not having su thing

not the most secure thing in the world, as once you have su remember ase getting root for
sh /sqlite_stmt_journals/ToggleSwap.sh
ase will always be able to execute that script, without prompting


but you can add a shortcut to the script ( longpress home and select ase ;) )


Code:
#!/system/bin/sh
tmpscript=/sqlite_stmt_journals/ToggleSwap.sh 
ToggleOn ()
{
echo "#!/system/bin/sh
for i in \`ls /dev/block/mmcblk0p*\`;do
	swapon \$i 2>/dev/null
	if [ \"\$?\" = \"0\" ];
	then
		break
	fi	
done" > $tmpscript
return
}
ToggleOff ()
{
echo "#!/system/bin/sh
for i in $swapon;do
	swapoff \$i
done" > $tmpscript
return
}
swapon=`awk '$1 !~ /^File/ { print $1 }' /proc/swaps`
if [ "$swapon" = "" ];
then
	ToggleOn
else
	ToggleOff
fi
su -c "sh $tmpscript"
free
rm $tmpscript
 

jUbib

Senior Member
Apr 21, 2010
55
3
Two very simple python scripts for adding a new contact and composing a new text message:

New contact:
Code:
import android
droid = android.Android()
droid.startActivity('android.intent.action.INSERT', 'content://contacts/people')
droid.makeToast('Add new contact')
droid.exit()

New message:
Code:
import android
droid = android.Android()
droid.startActivity('android.intent.action.SENDTO', 'sms:')
droid.makeToast('New message')
droid.exit()