Thursday, October 28, 2010

Communicating between Javascript and Actionscript in an AIR application

Using the HTMLLoader class you can communicate between javascript and actionscript in the same application. This means that you can trigger AS3 functions from a loaded HTML page through javascript (and vice versa). Here is a quick sample:

// AS3 code:

var windowBounds:Rectangle = new Rectangle(0,0,1024,768);
var htmlLoader:HTMLLoader = HTMLLoader.createRootWindow(true, null, false, windowBounds );
htmlLoader.runtimeApplicationDomain = ApplicationDomain.currentDomain;
htmlLoader.load(new URLRequest('myfile.html'));

htmlLoader.addEventListener(Event.COMPLETE, onComplete);

private function onComplete(e:Event):void{
// creates a new javascript function in the HTML page that map to the AS3 function
// you don't need to define these functions at all in the JS -- they become available automatically
htmlLoader.window.jsFunction = flashFunction;
}

private function flashFunction(msg:String):void{
trace("message: " + msg);
}


Once the onComplete() function runs, it creates the link between the newly created HTML javascript function and the AS3 function -- which can then be called directly in the HTML:

HTML code:

onclick="jsFunction('called from js');"

Wednesday, October 27, 2010

Round time to nearest 30 seconds

I had a requirement to round the time for a video (in seconds) to the nearest 30 seconds. Not being so strong in math, I am grateful to be better at Googling. So here it is for anyone else in a similar situation:

Math.round(myTime/30)*30

Awesome and simple!

Wednesday, October 20, 2010

Titanium gradient for buttonbar

Doesn't seem that the gradient works for the buttonbar either. So here is the workaround:

myButtonBar = Ti.UI.createButtonBar({
id:'buttonbar',
height:40,
backgroundColor:'#663399',
style:Titanium.UI.iPhone.SystemButtonStyle.BAR,
labels:['Close Keyboard >']
});

Just use a background color and the Titanium.UI.iPhone.SystemButtonStyle.BAR as the style -- and the app will create a gradient for you -- from lighter to darker.

Tuesday, October 19, 2010

Show and hide hidden files on a Mac

This command shows the hidden files in Finder on a Mac:

$ defaults write com.apple.finder AppleShowAllFiles TRUE
$ killall Finder

and this hides them again:

$ defaults write com.apple.finder AppleShowAllFiles FALSE
$ killall Finder

Friday, October 8, 2010

Titanium does not currently support the clipboard

Just a heads up for anyone that might stumble on this accidentally. Titanium Mobile does not currently support copying to the clipboard. I should have checked that before I coded a copy feature into my app...

Thursday, October 7, 2010

Time Machine issues when hard drive is partitioned

I have a Mac Book Pro (Snow Leopard) and set up Time Machine using a 1 TB Seagate external USB hard drive. The first time I set it up, I partitioned the drive into 600GB and 400GB and used the 600GB partition for Time Machine -- leaving the 400 GB partition for standard backups. However I found that the behavior of Time Machine was very buggy. For instance, if I tried to enter TM with any programs open, the Mac would freeze and I would have to restart.

So in order to use TM I had to close every program I had running. Secondly, about a month ago I maxed out the 600GB and TM stopped working altogether. It would just fail when I tried to back up.

In any event, I reformatted the drive to a single 1TB partition and now Time Machine works exactly as advertised...

UPDATE (November 5): After a few months I have had another error that the HD cannot be used by Time Machine and cannot be repaired. Either the HD is defective for Time Machine just doesn't play nice with it...

Creating a Titanium gradient button for iPhone

This took a while to figure out. I wanted to get a gradient button in Titanium but there were a number of variables that didn't seem to work properly. Here is a finished button with a gradient from black to lighter gray:

closeButton = Titanium.UI.createButton({
title:'Close',
width:150,
height:40,
style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN,
borderRadius:10,
font:{fontSize:16,fontFamily:_fontFamily,fontWeight:'bold'},
backgroundGradient:{type:'linear',
colors:['#000001','#666666'],
startPoint:{x:0,y:0},
endPoint:{x:2,y:50},
backFillStart:false},
borderWidth:1,
borderColor:'#666'
});

It seems that a few items are critical to make it work:

1. You have to define the style as "style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN" -- BORDERED will not work and if you don't have a style it also won't show.
2. Make sure that the gradient property for backFillStart is false.
3. There seems to be a bug when you put in #000000 as one of the colors. You need to set it to #000001 (or something like that), in order to get black to show