Tech

How To: Mix OpenLaszlo, Air and Javascript/HTML to create a desktop application with local file access

 

Doing some development on the apolmb project I thought about a way to make the local filesystem accessible to an OpenLaszlo application. Since the Flash Player version 10 seems only to be able to load and save local files, but not get directory listings, I installed Adobe Air 1.5.3 SDK and Runtime on my Ubuntu machine, integrated it with the help of Aptana in Eclipse, started a new Air HTML/JavaScript project and embedded the OpenLaszlo-compiled SWF inside.

It resulted in Air application, which prints the content of local files. The Air package is available here. The readLocalFile function will return the content of any file on your local disk (e.g. "/var/log/auth.log" or "C:/autoexec.bat")

Bridging Air and OpenLaszlo with Javascript

The HTML container inside the Air project provides access to the to the local filesystem through the AIR API. Embedding the SWF with OpenLaszlos lz.embed.swf function provided in the embed-compressed.js makes it possible to call the JS-functions in the HTML container with lz.Browser.callJS() and receive the return values. Heres the code:


Adobe Air HTML container code (airpolmb.html):


<html>
    <head>
        <title>OpenLaszlo App in Air Runtime / JavaScript Bridge</title>
        <script type="text/javascript" src="lib/air/AIRAliases.js"></script>
        <script type="text/javascript" src="lps/includes/embed-compressed.js"/>
        <script type="text/javascript">

            // reads the content of a local text file
            function readLocalFile(fpath){
                var f = air.File.applicationDirectory.resolvePath(fpath);
               
                var fs = new air.FileStream();
                fs.open(f, air.FileMode.READ);
                var content = fs.readUTFBytes(fs.bytesAvailable);
                fs.close();
               
                return content;
            }

        </script>
    </head>
    <body>
        <script type="text/javascript">
          lz.embed.swf({url: 'airtest.lzx.swf10.swf', allowfullscreen: 'false', bgcolor: '#ffffff', 
                        width: '100%', height: '100%', id: 'lzapp', accessible: 'false', 
                        cancelmousewheel: false});

          lz.embed.lzapp.onloadstatus = function loadstatus(p) {
            // called with a percentage (0-100) indicating load progress
          }

          lz.embed.lzapp.onload = function loaded() {
            // called when this application is done loading
          }
        </script>
    </body>
</html>

OpenLaszlo code (airtest.lzx):


<canvas height="100%" width="100%" title="Airtest" proxied="false">
    
    <method name="receivedFromJavaScript" args="data">
    <![CDATA[
        Debug.write("Response data: ");
        Debug.write(data);
        this.response.setAttribute("text", data);
    ]]>
    </method>

    <simplelayout axis="y" spacing="10" />
    
    <view height="10"></view>
    <view x="10">
        <simplelayout axis="x" spacing="10" />    
        <text>JS function: </text>
        <edittext name="func" width="220">readLocalFile</edittext>
        <text> argument: </text>
        <edittext name="arg" width="220">LocalFile.txt</edittext>        
        <button text="call"
                onclick="lz.Browser.callJS(parent.func.getValue(), 
                canvas.receivedFromJavaScript, parent.arg.getValue())"/>
    
    </view>
    <text x="10" text="Response data:"/>
    <text x="10" name="response" multiline="true" resize="true"/>
    
</canvas>

 

Alternative way without lz.embed

A different way leading to nearly the same results is to totally leave the lz.embed stuff out, directly import the class flash.external.ExternalInterface in the LZX code and go further using call and callback methods directly. In this case the response value is delivered by the callback function instead of being included in the return value.

airpolmb-alt.html:


<html>
	<head>
        <title>OpenLaszlo App in Air Runtime / JavaScript Bridge Alternative</title>
        <script type="text/javascript" src="lib/air/AIRAliases.js"></script>
        <script type="text/javascript">

		    // returns the SWF object node
			function thisMovie(movieName) {
		         if (navigator.appName.indexOf("Microsoft") != -1) {
		             return window[movieName];
		         } else {
		             return document[movieName];
		         }
		     }

            // reads the content of a local text file
            function readLocalFile(fpath){
                var f = air.File.applicationDirectory.resolvePath(fpath);
				
                var fs = new air.FileStream();
                fs.open(f, air.FileMode.READ);
                var content = fs.readUTFBytes(fs.bytesAvailable);
                fs.close();
                
				thisMovie("lzapp").getJSData(content);
				
                return content;
            }
			
        </script>
	</head>

    <body>
	<object id="lzapp" type="application/x-shockwave-flash" width="100%" height="100%"> 
   			<param name="movie" value="app:/airtest-alt.lzx.swf10.swf"/>
    		<param name="wmode" value="opaque"/>
		</object>
    </body>
</html>

airtest-alt.lzx:


<canvas height="100%" width="100%" title="Airtest-AS3" proxied="false">
	
	<method name="receivedFromJavaScript" args="data">
	<![CDATA[
		Debug.write("Response data: ");
		Debug.write(data);
		this.response.setAttribute("text", data);
	]]>
	</method>
	
	<class name="FlashUtil" extends="node" >
		
	    <passthrough>
	      import flash.external.ExternalInterface;
	    </passthrough>
	    
	    <method name="callJS" args="...rest" allocation="class" >
	      ExternalInterface.call.apply(ExternalInterface, rest);
	      Debug.write(ExternalInterface, rest);
	    </method>
	    
	    <method name="initJSCallback" args="...rest" allocation="class" >
	      ExternalInterface.addCallback.apply(ExternalInterface, rest);
	      Debug.write('Init callback');
	    </method>

	</class>
	
	<script>
		lz.FlashUtil.initJSCallback("getJSData", canvas.receivedFromJavaScript);
	</script>

	<simplelayout axis="y" spacing="10" />
	
	<view height="10"></view>
	<view x="10">
		
		<simplelayout axis="x" spacing="10" />	
		<text>JS function: </text>
		<edittext name="func" width="220">readLocalFile</edittext>	
		<text> argument: </text>	
		<edittext name="arg" width="220">LocalFile.txt</edittext>
		<button text="call" 
				onclick="lz.FlashUtil.callJS(parent.func.getValue(), parent.arg.getValue())"/>
	
	</view>
	<text  x="10" text="Response data:"/>
	<text  x="10" name="response" multiline="true" resize="true"/>
	
</canvas>

During my investigation this two posts in the OpenLaszlo forum helped me the most.

Thanks to everybody in the OpenLaszlo community and at Laszlo Systems for sharing this great framework and all the help and ideas with all of us.

General Tags: 
General category: 

How To: Tag your music for Sony Ericsson Music Player

The music player of my SE K800i has a very special idea of how the ID3 tags in my music files should look like. Tags in the format of the newest version ID3v2.4 seem to be completely ignored, those files show up in the unknown artist/album section. In addition it seems that track numbers starting with a zero (e.g. 01) will be put after track numbers starting with a one (e.g. 10). So here's what I recommend: Save tags in version ID3v2.3, with utf-16 encoding and do not force two-digit track numbers. Easytag is nice for that. Happy listening...

General Tags: 
General category: 

How to fix and clean up your Windows

A little bit of care...

It's a common story and it seems like everybody can complain about his Windows installation. The reason for that might be the fact that Windows probably is not the best operating system around but most likely it's also the case that todays operating systems are complex software products and no matter how hard developers try to make it comfortable-to-use it requires some user knowledge and a little bit of care to run well over a longer period of time. The common solution to Windows problems is to reinstall Windows or buy a new computer. Both break a butterfly on a wheel.

This How-To will try to give you a little understanding in why your Windows slows down or programs stop working correctly and describe a few steps, you should walk through now if you haven't done any maintanance to your Windows before, and you can walk through again the next time when you have the feeling somethings wrong with your Windows. This How-To will work with Windows XP as well as Windows Vista.

Step 1: Backup your data

The risk that something goes wrong and the data on your drive gets lost by applying the following steps is low. But the probability that in the next year something will happen, which makes at least some of your data go away ( crash of your hard disk, accidental deletes, thievery) can't be ignored, Imagine which data you would miss after a crash of your hard disk and put a copy of this data on an external drive. You should always have a copy of your important data on two different disks. If it's personal data you may want to encrypt it, e.g. with Truecrypt.

Step 2: Clean up your disk(s) and the Windows registry

The following steps will fail if you are low on disk space. Be sure to free around 20 percent of your disk(s) space before you continuing with this How-To. And please don't worry, those 20 percent of disk space won't be filled at the end of the How-To. Delete personal files you don't need anymore. Be sure you have a backup of them in an external location.

In Windows Explorer, right-click your hard disk(s) and go to the properties option. Click the disk cleanup button if its available. Also use CCleaner to free some more space and repair the Windows registry.  The registry holds information that can, if invalid, noticeably slow down or make programs stop working in your Windows.

If you're generally low on disk space, here are some more tips for advanced users:

Step 3: Check your disk (optional)

We expect your disk to be working properly, but if you want to be completely sure right-click your hard disk(s) and go to the properties option, choose "Tools" and run a disk check. Choose the option which corrects errors. If Windows says it's necessary to reboot do this. The disk check might take some time, probaly even an hour or longer.

Step 4: Defragment your hard disk

My experience with slow Windows systems is that files are massively fragmented. If you use the whole capacity of your disk big files will be saved in thousands of fragments on Windows systems. Access to those files is much slower then. On a standard Windows system theres a special effect: One of the biggest files on your system is the so-called page file, where Windows by default swaps some of the data from the machines working memory. This page file will fragment in a horrible way and reduce your systems performance by a multiple. Just freeing some disk space won't help a lot.

So right-click your hard disk(s) and go to the properties option to start a complete defragmentation. This might take some time, probably one to three hours and it's wise to leave your computer alone during defragmentation because saving files might interrupt it. So time for something else now, we'll continue when defragmentation is finished...

Step 5: Install Microsoft Updates and Service Packs

Some problems and security leaks in Windows and other Microsoft software are solved by the monthly updates Microsoft delivers. To update all of your Microsoft products use Microsoft Update ( open this page in Internet Explorer ) instead of the default Windows Update, which only updates Windows. Confirm the installation of the latest updates, software and Service Packs and restart when everything is done.

Advanced: To free some disk space on Vista after installing Service Packs you can optionally use vsp1cln.exe ( since Vista SP2 the file is called "compclean.exe" ). Be aware that you can't uninstall the Service Pack anymore after that.

Step 6: Check for viruses and spyware

For a first check, use the Microsoft Windows Malicious Software Removal Tool. Next, I recommend installing Microsoft Serurity Essentials to free your computer from viruses and spyware and keep up protection in the future. If you're happy with a similar software already installed on your computer just go for a full scan.

Step 7: Check your firewall status

 The Windows Firewall will keep you safe from attacks through the wire  and is enabled by default. Please check if this is still the case.

Step 8: Update drivers and software (optional, advanced)

This last step may be better handled by a Windows user with advanced knowledge. So if you're not one yourself ask somebody who knows a bit more about computers. He will be very glad to hear that you already did the first seven steps on your own, because they are most pain.

Using the latest drivers and software will give you more security and better performance and sometimes even more functionality. Your computer manufacturer probably will provide the latest drivers for your system. The following software will help you to identify outdated software and drivers:

Everything fine now?

Now your Windows should perform better, at least for the next few months. If problems reoccur just walk through this How-To again.

If your Windows now still has problems something is really wrong.  If it's incredibly slow it might be that the mode of your drive fell back to PIO-mode. If Windows crashes from time to time your RAM or your hard disk may be seriously damaged. Check both for errors then.

And even if all this doesn't help, just create a new user profile and check if problems still show up when logging in with this new user. Sometimes you just need to throw away a screwed-up user profile to make Windows work again.

I hope this How-To helps you to be less annoyed by your Windows, please post comments if you feel like something could be improved here. Thanks!

Windows still annoys you?

Just install this wallpaper. Seriously,try Ubuntu.

General Tags: 
General category: 

Synchronize contacts between a Sony Ericsson K800i mobile phone and Google Mail with Google Sync

The K800i does not seem to be officially supported by Google Sync yet , but in my case the setup guide for the W800i perfectly did it. Adding, updating and deleting contacts works well in both directions. Contacts, which have been collected automatically by Google Mail and just store an e-mail address do not show up in the contacts list on the K800i. Not to mention, that by using Google Contacts for Thunderbird even the address book of your thunderbird is wired to this synchronization party.

General Tags: 
General category: 

Ubuntu desktop magic

There are several things I really like about my Ubuntu installation, but there is some fantastic usability thing I did set up after a while and I don't want to keep it from you:

The common way
Most probably you will start your machine once or twice a day, coming up with a fresh desktop. You will start Firefox, Pidgin, Thunderbird, probably Songbird for the music, Nautilus to do some file actions and maybe open a terminal window. You'll show and hide them, close and reopen them. Some application windows will remember their position and sizes, some won't. During your session you'll feel the need to resize windows, move them, hide them or even put them to another workspace. Usually your application window won't stay on in the same place with the same size for a long time. We're used to those procedures because we did that way since we use window managers. But they take time and clicks. If we search an application we have to look in the window list or use the window switcher to identify and show it. If we bring all that to mind we realize that this is a bit of pain.

What if...
So what if we could fill the desktop with the basic applications automatically. And what if we put them on a default position with a default size on a default workspace, so we don't feel the need to move, resize, show or hide anything at all. All which is left is switching the workspace. And we'll know exactly where to find a application window because it stays always in the same place.

How to do it
On my Gnome desktop I set up six workspaces. On the first one I put Firefox, Skype and Pidgin, the second one has Thunderbird on it, the third one is clean ( to run free), the fourth shows two Nautilus windows of the same size side by side ( yeah, i like orthodox file managers), the fifth runs two terminals and on workspace six Songbird plays the music. All those applications are startet and put into position by a script, which I linked on my Desktop. All it needs is the package wmctrl. Here's the scripts source. Have fun!


#! /bin/sh

# start on workspace 0
wmctrl -s 0

# start firefox with a default page and go on
firefox "http://de.indymedia.org/index.shtml" &
{
    # wait for firefox to start
    sleep 20s

    # resizing and positioning
    wmctrl -r Firefox -e 0,10,40,1090,700

    # set a custom window title
    wmctrl -r Firefox -T "Ich trinke ein Getränk und esse ein Geäst und sing..."

    pidgin &
    skype &
    {
        sleep 20s
        wmctrl -r Buddy List -e 0,1120,40,140,700
        wmctrl -r Skype -e 0,875,40,225,400
        wmctrl -s 1

        thunderbird &
        {
            sleep 10s
            wmctrl -r Thunderbird -e 0,10,40,1250,700

            wmctrl -s 3
            nautilus &
            {
                sleep 3s
                wmctrl -r File Browser -e 0,10,40,620,700
                wmctrl -r File Browser -T "Soll"
                
                nautilus &
                {
                    sleep 3s
                    wmctrl -r File Browser -e 0,645,40,620,700
                    wmctrl -r File Browser -T "Haben"

                    wmctrl -s 4
                    gnome-terminal &
                    gnome-terminal -e "sudo -i" &
                    {
                        sleep 4s
                        wmctrl -r matze -e 0,10,40,620,700
                        wmctrl -r root -e 0,645,40,620,700
                        
                        wmctrl -s 5
                        Apps/Songbird/songbird &
                        
                        {
                                sleep 15s
                                wmctrl -r Songbird -e 0,10,40,1250,700
                        }
                    }
                }
            }
        }
    }
}

 

General Tags: 
General category: 

How to keep your Ubuntu clean.

  1. Update you package sources an install all upgrades.
  2. Remove old linux-headers and linux-modules packages. You only need the ones for the linux-image(s) currently installed.
  3. Purge old configs. Usually you won't need configurations from removed packages, but Ubuntu keeps them by default. Run aptitude in the command line and press the "_" key on the "Not installed packages" item. Then continue with "g", assure that you really won't need the configs of the packages to purge anymore and confirm with "g" again.
  4. Check your caches. The Sun JVM has an oversized cache by default. You are free to minimize it in the JVM control panel. More cache files can be probably found in the hidden ~/.local/share/ directory. Configure your Flash player the way you like it: www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager.html#117118
General Tags: 
General category: 

Pages

Subscribe to RSS - Tech

ad ubuntu linux

Greenpeace Energy

www.lobbycontrol.de