Technology blog oriented towards good design and impressive web applications.

IdleTogether Home

Archives Posts

Database of snippets of code for AS3

March 22nd, 2009 by Nicolas Noben

Here is a great little site I stumbled upon today.

It’s called java2s and it’s basically example source code (snippets) of many languages, organized by topic.

Here is the Flex / Flash / Actionscript category.

Do you know other ones (categorized)? If so, let us know.

snippets-code-flex-as3-flash.jpg

Filed under Tutorial, Air, Flex, Flash, All having No Comments »

Archives Posts

Class to Open object(s) from file with Flex in Air

February 24th, 2009 by Nicolas Noben

Before, I wrote about How to save one or more objects to file using this class.

Today it’s time to read it and bring back the object in Flex.

The class

package com.idletogether
{
	import flash.events.Event;
	import flash.filesystem.*;

	public class Opener
	{
		public static var file:File;

		public static function openFile() :void
		{
			file = new File();
			file.addEventListener(Event.SELECT, dirSelected);
			file.browseForOpen('');
		}

		public static function dirSelected(e:Event) :void
		{
			// this will be our object back
			var dat:Object = new Object();

			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			dat = fileStream.readObject();
			fileStream.close();

			some.object = dat.data;
			some.other.object = dat.structure;
		}

	}
}

How to use it

Simply…

import com.idletogether.Opener;
Opener.openFile();

This example is very basic, but it is kept simple for the sake of being easy to understand. From there you can build up a much more tailored system. This should get you started.

Use the Expressions panel of Flex and add a ‘watch’ to the variable ‘dat’ to see what came back in and if it is in good form.

HTH,
Cheers.

Filed under Tutorial, Air, Flex, All having 1 Comment »

Archives Posts

Class to Save object(s) to file with Flex in Air

February 23rd, 2009 by Nicolas Noben

The class

Feel free to use it for free or commercial projects.

package com.idletogether
{
	import flash.events.Event;
	import flash.filesystem.*;

	public class Saver
	{
		public static var file:File;

		public static function saveToFile() :void
		{
			// pick an unused extension
			file = new File("/filename.ext");
			file.addEventListener(Event.SELECT, dirSelected);
			file.browseForSave('');
		}

		public static function dirSelected(e:Event) :void
		{
			// this object will get saved to the file
			var dat:Object = new Object();

			dat.data = some.object;
			dat.structure = some.other.object;

			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.WRITE);
			fileStream.writeObject(dat);
			fileStream.close();
		}
	}
}

How to use it

It’s a static method to be called so, simply…

import com.idletogether.Saver;
Saver.saveToFile();

This will prompt the user to pick a destination and will suggest the file name “filename.ext”.

Please note that you can’t save DisplayObjects straight to file using writeObject. It’s a limitation.

From here, find out how to get the object back into flex in the next post.

HTH,
Cheers.

Filed under Tutorial, Air, Flex, All having 2 Comments »

Archives Posts

Apply drop shadow to text at runtime with Flex in MXML

February 22nd, 2009 by Nicolas Noben

A quick and easy one but I always end up looking for it and not finding it easy.

<mx:DropShadowFilter id="dropShaText" distance="1" angle="45" blurX="0" blurY="0" alpha="1" color="0x111111" />
<mx:Label text="Hello world" filters="{[dropShaText]}" />

font-drop-shadow-flex.png

Filed under Tutorial, Flex, All having No Comments »

Archives Posts

Working through Screens

February 11th, 2009 by Nicolas Noben

Working through Screens: 100 Ideas for Envisioning Powerful, Engaging, and Productive User Experiences in Knowledge Work.

It’s not the easiest read, but it has some great insight, and hey, can’t beat a free ebook.

Picture 4.jpg

Picture 3.jpg

Filed under Tutorial, News, All having No Comments »

Archives Posts

Capture CTRL+N / CMD+N in Flex/Air/AS3

November 23rd, 2008 by Nicolas Noben

When you program features for (web) application, it’s often useful & good practice to allow users shortcuts for common actions.

This easy snippets shows how to capture ‘new’ or Control+N or Command+N on the Mac.

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);

private function keyDown(e:KeyboardEvent) :void
{
        if(e.commandKey || e.ctrlKey)
        {
                switch(e.keyCode)
                {
                        case Keyboard.N:
                                // do stuff
                        break;
                }
        }

}
Filed under Tutorial, Flex, Flash, All having No Comments »

Archives Posts

Easy Form Validation and Submit Button enable-disable in Flex 3

November 18th, 2008 by Nicolas Noben

The submit button is enabled/disabled automatically based on the form elements’ validations.

The form

<mx:Form x="0" y="90" width="100%" height="100%" id="form1" creationComplete="resetForm()">
	<mx:FormItem label="Email" width="100%">
		<mx:TextInput width="100%" id="txtEmail" change="validateUs()" />
	</mx:FormItem>
	<mx:FormItem label="Password" width="100%">
		<mx:TextInput width="100%" id="txtPassword" displayAsPassword="true" change="validateUs()"/>
	</mx:FormItem>
	<mx:FormItem width="100%">
		<mx:Button id="btnLogin" label="Login" width="85" height="25" click="loginUser()" />
	</mx:FormItem>
</mx:Form>

The validators

<mx:EmailValidator id="val1" source="{txtEmail}" property="text" required="true" />
<mx:StringValidator id="val2" source="{txtPassword}" property="text" required="true" minLength="2" />

The script

private function resetForm() :void
{
	btnLogin.enabled = false;
}
private function validateUs() :void
{
	btnLogin.enabled = (Validator.validateAll([val1,val2]).length == 0);
}
Filed under Tutorial, News, Flex, All having 2 Comments »

Archives Posts

Use Applications Full Screen in OS X Leopard (Hide the menubar)

August 28th, 2008 by Nicolas Noben

presentyourappslogo.png

The problem

Well, before Leopard I used this app called Menufela. It was good, but it died with Leopard and that’s that.

Since then it’s been frustrating. I like to program fullscreen, with textmate, coda, flex builder etc. So anything that takes screen real estate has to go. I love the approach of Writeroom for writers and Developers need a solution like this.

The Solution

Googling I ended up found this trick on MacOsxHints. It seems to work but it’s quite hard to implement.

Eventually, this donationware (free) application called Present Your Apps allows you to do just that, very easily.

The only down side is that it will ask you for a password and restart your application.

Please note: DO NOT USE the app MENUFELA (see ‘problem’, mentioned at the top of this post). I tried it again, it did say ‘does not play nice with leopard’, but it turned out to be a lot worse than expected.

Indeed, my computer wouldn’t boot anymore and I had to do a hell of a lot to get it back to normal.

presentyourapps.jpg

Filed under Tutorial, Rant, All having 2 Comments »

Archives Posts

Tile Windows in OS X Leopard

August 27th, 2008 by Nicolas Noben

ASAppIcon.pngOne thing that I really missed when moved from XP to OS X was the ability to tile windows and make use of them fullscreen.

Yes, it’s not for everyone, and it’s not for every apps but it’s damn handy when you are working on 2 or 3 different files in TextMate.

That’s where applescript black magic comes out. I found a few script, most of them were shit, except one.

bbs.macscripter.net -> use the second one, it has settings.

All you need to do is copy and paste that in SCRIPT EDITOR, save it as a script scpt in your documents folders or applications folders.

Then in Quicksilver, add a triggers to call that script. Mine is Control+` and VOILA, TextMate has wicked Tiled Windows now.

here are my settings

property horizontalSpacing : -4 — sets the horizontal spacing between windows
property verticalSpacing : -4 — sets the vertical spacing between windows
property maxRows : 1
property maxCols : 3

-4 because otherwise, there is gap, even at 0 (due to drop shadows).

Note: it doesn’t work well with Finder or Flex Builder ;-)

tiled windows textmate osx.jpg

I attach the script for archiving purposes and in case that site falls off the earth.

All credits go to isherman and dennis from bbs.macscripter.net for the following:

--tile windows of frontmost applications in a grid
--this script is useful for
--multiple window chatting
--working side by side of several windows of the same app

--make need to make it as a stay open application later
--for now assume that it is opened and closed per invokation

property horizontalSpacing : -4 -- sets the horizontal spacing between windows
property verticalSpacing : 0 -- sets the vertical spacing between windows
property maxRows : 1
property maxCols : 3

on run {}
	local a
	set userscreen to my getUserScreen()

	--display dialog (getFrntApp() as string)
	try
		set applist to getFrntApp()
		if length of applist = 0 then
			return
		end if
		set a to item 1 of getFrntApp()
	on error the error_message number the error_number
		display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
	end try

	try
		tileScriptable(a, userscreen)
	on error the error_message number the error_number
		--display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
		try
			tileUnscriptable(a, userscreen)
		on error the error_message number the error_number
			display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
		end try
	end try

end run

on tileScriptable(a, screen)
	local i, c
	set i to 1
	tell application named a
		set theWindows to every window of application a whose visible is true and floating is false and ¬
			modal is false -- and miniaturized is false
		set c to count theWindows
		if c = 0 then
			return
		end if
		set tiles to calTileBounds(c, screen, 1)
		repeat with theWindow in theWindows
			my tileScriptableWindow(a, theWindow, item i of tiles)
			set i to i + 1
		end repeat
	end tell
end tileScriptable

on tileUnscriptable(a, screeninfo)
	-- unscriptable app
	local i, c
	set i to 1
	tell application "System Events"
		set theWindows to (every window of application process a)
		--set theWindows to my filterUnscriptableInvisible(theWindows)

		set c to count theWindows

		if c = 0 then
			return
		end if

		--display dialog screeninfo as string giving up after 5
		set tiles to my calTileBounds(c, screeninfo, 1)
		repeat with theWindow in theWindows
			--display dialog (class of visible of theWindow)
			my tileUnScriptableWindow(a, theWindow, item i of tiles)
			set i to i + 1
		end repeat

	end tell
end tileUnscriptable

on filterUnscriptableInvisible(ws)
	-- filter out from ws windows that are docked
	set newws to {}
	set docklist to getNamesDocked()
	--display dialog (docklist as string)
	repeat with theWindow in ws
		if name of theWindow is not in docklist then
			set end of newws to theWindow
		end if
	end repeat

	--display dialog (count newws)
	return newws
end filterUnscriptableInvisible

on getNamesDocked()
	tell application "System Events" to tell process "Dock"'s list 1
		set l to name of UI elements whose subrole is "AXMinimizedWindowDockItem"
	end tell

	return l
end getNamesDocked

on tileScriptableWindow(a, w, bound)
	tell application a
		set bounds of w to bound
	end tell
end tileScriptableWindow

on tileUnScriptableWindow(a, w, bound)
	tell application "System Events"
		--display dialog (count position of w)
		set AppleScript's text item delimiters to " "

		set position of w to {(item 1 of bound), (item 2 of bound)}

		-- why the -5?
		set size of w to {(item 3 of bound) - (item 1 of bound) - 5, ¬
			(item 4 of bound) - (item 2 of bound) - 5}
		--display dialog (count properties of w)
	end tell
end tileUnScriptableWindow

on calTileBounds(nWindows, screen, direction)
	-- return a list of lists of window bounds
	-- a simple tile algo that tiles along direction (current only 1=horizontal)

	local nRows, nColumns, irow, icolumn, nSpacingWidth, nSpacingHeight, nWindowWidth, nWindowHeight
	set {x0, y0, availScreenWidth, availScreenHeight} to screen
	set ret to {}

	set nRows to (nWindows div maxCols)
	if (nWindows mod maxCols) ≠ 0 then
		set nRows to nRows + 1
	end if

	if nRows < maxRows then
		set nSpacingHeight to (nRows - 1) * verticalSpacing
		set nWindowHeight to (availScreenHeight - nSpacingHeight) / nRows
	else
		set nSpacingHeight to (maxRows - 1) * verticalSpacing
		set nWindowHeight to (availScreenHeight - nSpacingHeight) / maxRows
	end if

	repeat with irow from 0 to nRows - 1
		if nRows ≤ maxRows and irow = nRows - 1 then
			set nColumns to nWindows - irow * maxCols
		else
			set nColumns to maxCols
		end if
		set nSpacingWidth to (nColumns - 1) * horizontalSpacing
		set nWindowWidth to (availScreenWidth - nSpacingWidth) / nColumns
		set nTop to y0 + (irow mod maxRows) * (verticalSpacing + nWindowHeight)
		--display dialog "Top: " & nTop buttons {"OK"} default button 1
		repeat with icolumn from 0 to nColumns - 1
			set nLeft to x0 + (icolumn) * (horizontalSpacing + nWindowWidth)
			set itile to {¬
				nLeft, ¬
				nTop, ¬
				nLeft + nWindowWidth, ¬
				nTop + nWindowHeight}
			set end of ret to itile
			--display dialog item 3 of itile as string
			--set itile to {x0 + (icolumn - 1) * wgrid, y0, wgrid, hgrid}
			--set item 3 of itile to ((item 1 of itile) + (item 3 of itile))
			--set item 4 of itile to ((item 2 of itile) + (item 4 of itile))
		end repeat
	end repeat

	return ret
end calTileBounds

on getFrntApp()
	tell application "System Events" to set frntProc to ¬
		name of every process whose frontmost is true and visible ≠ false
	return frntProc
end getFrntApp

on getUserScreen()
	-- size of the menubar
	tell application "System Events"
		set {menuBarWidth, menuBarHeight} to size of UI element 1 of application process "SystemUIServer"
		--display dialog "Menubar width: " & menubarWidth & ", height: " & menubarHeight
		set dockApp to (application process "Dock")
		set {dockWidth, dockHeight} to size of UI element 1 of dockApp
		--display dialog "Dock width: " & dockWidth & ", height: " & dockHeight
		set dockPos to position of UI element 1 of dockApp
		--display dialog "Dock x: " & (item 1 of dockPos) & ", y: " & (item 2 of dockPos)
	end tell

	-- size of the full screen
	(*
{word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number, ¬
word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number}
*)
	tell application "Finder"
		set screenSize to bounds of window of desktop
		set screenWidth to item 3 of screenSize
		set screenHeight to item 4 of screenSize
	end tell
	--display dialog "Screen width: " & screenWidth & ", height: " & screenHeight

	-- by default, set the available screen size to the full screen size
	set availableWidth to screenWidth
	set availableHeight to screenHeight - menuBarHeight
	set availableX to 0
	set availableY to menuBarHeight

	--determine the userscreen origin and size

	-- case 0: hidden dock
	-- if (item 1 of dockPos < 0 or item 1 of dockPos ≥ screenHeight) then
	-- no need to change anything
	-- end if

	-- case 1: bottom dock
	if ((item 2 of dockPos) + dockHeight = screenHeight) then
		set availableHeight to availableHeight - dockHeight
	end if

	-- case 2: left dock
	if (item 1 of dockPos = 0) then
		set availableWidth to availableWidth - dockWidth
		set availableX to dockWidth
	end if

	-- case 3: right dock
	if ((item 1 of dockPos) + dockWidth = screenWidth) then
		set availableWidth to availableWidth - dockWidth
	end if

	return {availableX, availableY, availableWidth, availableHeight}
end getUserScreen
Filed under Tutorial, News, All having 5 Comments »