My Words

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: Upgrade ext3 to ext4 if your Ubuntu installation is inside of an encrypted LVM2 partition

In general you can use this guide for the basics, make sure that you are using grub2 as the boot manager (if you boot from the partion you're going to convert) and you have a backup of all important data. You need to boot from a Live-CD or a USB-installation of Ubuntu to do the changes to your boot partion from there. Theres one little trick about how to get access to an encrypted LUKS LVM2-container. If you setup such a thing (e.g. by using Ubuntus alternate installer) also take a look here.

General Tags: 
General category: 

How To: Fix your screen configuration when using ATI Catalyst™ Proprietary Linux Display Driver

The team play of Ubuntu and ATI graphic cards is something I wouldn't bet my shirt on. At some point it's still a mystery for me to configure it correctly, the effects of changes in xorg.conf, driver updates, use of aticonfig or Ubuntu's display configuration tool give a surprise every time and if suddenly things are working correctly I call it a miracle. Unfortunatly after having benefited from a miracle for quite some months, things stopped working again for no apparent reason.

At some point the driver seems to not listen to xorg.conf any way. It writes its own configuration into /etc/ati/amdpcsdb and this file gets messed up after some time. So at any start of trying a new configuration its seems wise to replace it with the /etc/ati/amdpcsdb.default file.

But still the xorg.conf file seems to have some influence on graphic settings. So for a fresh start also completely remove /etc/X11/xorg.conf and do a aticonfig --initial --force which will give you a completely new one based on the configuration this ATI tool detected.

After doing both of this still strange things were happening in my Ubuntu installation. Logging into GDM with my user profile gave unexpected and unwanted results in display configuration, logging in with another user profile worked perfectly well. The reason for that: user-dependent GNOME Display settings in the home directory in ~/.config/monitors.xml. Delete this file to prevent Display chaos.

So we can finally start adapting the new configuration now. This should be done in the CCC (Catalyst Control Centre). I hope things go well from here for you too. Good luck.

General Tags: 
General category: 

Started a new religion today, will be awesome

Starting into the week after a nice relaxing weekend felt somehow different today because just during breakfast I suddenly felt like starting a new religion is a good idea today. I immediately had a lot of nice ideas for this new religion, but couldn't start directly writing them down because I was busy. So one of the first but maybe less important rules for this religion is:

If you like to start a new religion go for it. There's no need to start immediatly. If you're busy with something else or just need some additional good ideas to start, start it any convenient time.

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: 

Pages

Subscribe to RSS - My Words

ad ubuntu linux

Greenpeace Energy

www.lobbycontrol.de