Jump to content



Photo

How To Select A Directory - Windowedapplication

air select directory

  • Please log in to reply
15 replies to this topic

#1 Necuima

Necuima

    Advanced Member

  • Members
  • PipPipPip
  • 141 posts
  • LocationSunshine Coast, Queensland, Australia

Posted 26 February 2012 - 5:50 AM

Hi Larry,

As you know, I am a complete beginner with Flex, but what I've seen of your book so far has been very helpful. But I am keen to try something probably before I've gone far enough into the book, and it is more AIR focused than web focused, but maybe you or someone can help me.

All I'm trying to do so far is to enable the user to select a directory from the PC file system that the app is running on. In Flash Builder, I have the following:


<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication

xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="
library://ns.adobe.com/flex/spark"
xmlns:mx="
library://ns.adobe.com/flex/mx">

<fx:Script>
<![CDATA[

var file:File = new File();
file.addEventListener(Event.SELECT, dirSelected);
file.browseForDirectory(
"Select a Directory");

function dirSelected(e:Event):void {

trace(file.nativePath);
}
]]>

</fx:Script>
</s:WindowedApplication>
Flash Builder is giving me several errors which I don't understand:

On the addEventListerner line of code two errors are showing - 'access of undefined property dirSelected' and 'access of undefined property file'
For the following line (file.browserForDirectory) the error that shows is 'access of undefined property file'.

Any help will be appreciated, and thanks in anticipation.

Cheers, Necuima

  • 0

#2 Jason

Jason

    Newbie

  • Members
  • Pip
  • 13 posts
  • LocationPerth, Australia

Posted 26 February 2012 - 10:36 AM

Hey Necuima,

You were close but a few ActionScript issues, the main thing was that you can't instantiate classes or add event listeners outside of a function. You should always have an init function. Try the following:

It works on Flex 4.6 and AIR 3.1 using FlashDevelop (version 4 is awesome).

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init();">
<fx:Script>
<![CDATA[
import flash.filesystem.File;
public var file:File;
public function init():void {
file = new File();
file.addEventListener(Event.SELECT, dirSelected);
file.browseForDirectory("Select a Directory");
}
public function dirSelected(e:Event):void {
trace(file.nativePath);
}
]]>
</fx:Script>
</s:WindowedApplication>

  • 1

#3 Necuima

Necuima

    Advanced Member

  • Members
  • PipPipPip
  • 141 posts
  • LocationSunshine Coast, Queensland, Australia

Posted 27 February 2012 - 1:27 AM

Hi Jason,

Many thanks for the advice.

I made one small change - first importing mx.controls.Alert and then changing the trace to Alert.show. Voila!! Works like a charm.

Much appreciated.

Cheers from the Sunshine Coast.
  • 0

#4 Jason

Jason

    Newbie

  • Members
  • Pip
  • 13 posts
  • LocationPerth, Australia

Posted 27 February 2012 - 6:27 AM

No worries mate, glad I could help!

For Flex AIR development I suggest grabbing a copy of Adobe AIR: Create - Modify - Reuse, I only use it for code examples and seeing how things could be done in the real world. It's a little old covering Flex 3 but it shouldn't be a big deal. Not a huge fan of Wrox books but this one is very practical when it comes to Flex AIR applications (aren't too many books on it either). Larry's AIR book covers HTML/CSS + JavaScript which is rather different.
  • 1

#5 Necuima

Necuima

    Advanced Member

  • Members
  • PipPipPip
  • 141 posts
  • LocationSunshine Coast, Queensland, Australia

Posted 2 March 2012 - 6:22 PM

Hi Larry & Jason,

As mentioned above, I am quite new to Flash but I am trying to use Flash Builder to generate a windowed application that reads all the files in a directory, does some processing on them, and writes the changed files out to another directory.

But it seems that I need a mix of synchronous functions and asynchronous functions and I'm getting in a muddle as to how to handle this.

I have some logic which enables the user to select the input directories and the output directories and that works fine.

Then I have a button to start the processing logic and that starts it off OK.

The logic flow is..

. delete all files in the output directory (works OK)
. use getDirectoryListing to build an array of File objects, one for each file in the input directory (they are jpg image files)
. then I want to use a for loop to loop through each file, resize the jpg image and then write the resized image out

- reading the first input file synchronously works fine.
- but then I need to use the loader class to load the just-read file which is now a bytearray, but loadbytes is asynchronous.
- I wrote an event listener OK which fires up an 'onloadcomplete' function when the loader has finished loading, BUT I don't want any other processing to continue until the onloadcomplete function has done it's logic.
- And then I want to go back to the point just after the loader function was called and continue synchronous processing, i.e., back through the for loop to process the next jpg image file.

Is this possible?

Any advice will be muchly appreciated. Thanks in anticipation, Necuima.
  • 0

#6 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,947 posts
  • LocationState College, PA (USA)

Posted 7 March 2012 - 9:41 PM

Sorry for the delay. I've been out of town on vacation. I'm catching up and will try to reply in a couple of days.
  • 0

#7 Necuima

Necuima

    Advanced Member

  • Members
  • PipPipPip
  • 141 posts
  • LocationSunshine Coast, Queensland, Australia

Posted 9 March 2012 - 3:09 AM

Hi Larry,

No worries - no great urgency. Would it help if I included the code that I have written so far?

Cheers from Oz.
  • 0

#8 Necuima

Necuima

    Advanced Member

  • Members
  • PipPipPip
  • 141 posts
  • LocationSunshine Coast, Queensland, Australia

Posted 11 March 2012 - 2:54 AM

Hi Larry,

I am getting more and more confused with this - one might say hopelessly befuddled!

Here's part of my logic flow code:


// now go through the files in the input directory and resize them if they are jpg image files

var inContents:Array;
inContents = dirin.getDirectoryListing();
// creates an array of file objects - one for each file in the directory

for (i = 0; i < inContents.length; i++)
{

// Alert.show(inContents.nativePath);
nayme = inContents.name; // the i index disappears when I post this????
Alert.show(
"Nayme = " + nayme);

if (nayme.substr(-4, 4) == ".jpg" || nayme.substr(-5, 5) == ".jpeg")
{
// we have a file name that indicates that the file is a jpg file
Alert.show(
"About to process " + nayme);

// processing goes here....(removed for the example)
p++;
// counts the number of images processed
}

else
{
[i]// not a jpg file suffix

Alert.show(
"Skipped " + nayme);
s++;
[i]// counts the number of files not processed

}
}
[i]// end for loop





But the weird thing that I cannot understand is that the 'About to process' and 'Skipped' Alert.shows show BEFORE the 'Nayme = name' Alert.show. It's like it is backwards. This dis-order of sequential processing is, I think, behind part of the larger problem I am having with this code, but I'd like to solve each bit at a time.

Maybe Flex is the wrong facility to use for this application which is primarily a batch operation?

Your or others who may be able to help's advice will be much appreciated.

Thanks, Necuima.
  • 0

#9 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,947 posts
  • LocationState College, PA (USA)

Posted 14 March 2012 - 5:00 PM

Sorry for the delay. I keep meaning to get to this thread, but it warrants a thoughtful reply and I'm being pulled in multiple directions right now. Let me see if I can get too it today.
  • 0

#10 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,947 posts
  • LocationState College, PA (USA)

Posted 14 March 2012 - 11:05 PM

Okay, I haven't done anything like this in Flex myself, but I think it would be possible. The order of your alerts is surprising.

Anyway, I see what you mean about the issues with synchronous vs. asynchronous events. What about instead of using a loop, you have the processing code invoke the processing of the next file? That way tying the synchronous events to the asynchronous and you should be able to smooth out the wrinkles.
  • 0

#11 Necuima

Necuima

    Advanced Member

  • Members
  • PipPipPip
  • 141 posts
  • LocationSunshine Coast, Queensland, Australia

Posted 15 March 2012 - 1:36 AM

Hi Larry,

OK, I will give it a try, but right now my main computer is in the shop for major repairs so I probably won't be able to do that try for more than a week.

My main PC just freezes up for no apparent reason so I have left it with a technician that I trust.

Cheers from Oz courtesy my wife's laptop!
  • 0

#12 Necuima

Necuima

    Advanced Member

  • Members
  • PipPipPip
  • 141 posts
  • LocationSunshine Coast, Queensland, Australia

Posted 26 March 2012 - 4:58 AM

Hi Larry,

Well, my PC is 'back on the air' again thankfully!

You are right - I was able to re-jig the code so that the for loop was not needed in the same context. Many thanks for the suggestion.

The application just cycles through every jpg image file in a selected input directory and resizes them (down) and writes the downsized images out with the same file name but in the selected output directory.

But the quality of the downsized images isn't great! I have the same function in a PHP script using GD and the image quality is much better for similar sized image files.

Any suggestions to improve the image quality in the windowed application?

Many thanks again in anticipation,

Cheers from Oz.
  • 0

#13 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,947 posts
  • LocationState College, PA (USA)

Posted 26 March 2012 - 7:59 AM

Glad that helped. As far as improving the image quality, unfortunately I've never done that kind of image manipulation using Flex, so I couldn't offer any advice. But I'll be curious to see what you find out by looking online.
  • 0

#14 Necuima

Necuima

    Advanced Member

  • Members
  • PipPipPip
  • 141 posts
  • LocationSunshine Coast, Queensland, Australia

Posted 26 March 2012 - 6:39 PM

Hi again, Larry,

I was wondering if the Flash windowed application could be used to select the directories as per its current functionality, and then this directory information passed over to a modified PHP script for it to do the actual image resizing? Can this be done in an AJAX sort of way (via localhost, as a remote hosted PHP script would probably be too slow due to the uploading and downloading of all the image files)? Ideally, the user would only see the windowed application user interface (mx: Form) as at present, with the 'work' being done behind the scenes via PHP/localhost, and when that was all done, a message to that effect given to the user via the windowed application form display. Would there be Flash security model implications?

Any suggestions/advice will be most welcomed.

Thanks, Necuima.
  • 0

#15 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,947 posts
  • LocationState College, PA (USA)

Posted 28 March 2012 - 6:36 PM

Is this the same issue as in the new thread you created?
  • 0

#16 Necuima

Necuima

    Advanced Member

  • Members
  • PipPipPip
  • 141 posts
  • LocationSunshine Coast, Queensland, Australia

Posted 28 March 2012 - 7:00 PM

Hi Larry,

It is related, so let's close this thread - OK?

I have been Googling like mad and there seems to be lots of evidence that a windowed Flex app can indeed communicate with a PHP script.

As always, I am very appreciative of your advice.

Cheers from Oz.

P.S., I did look into the jpeg encoder options as you suggested earlier, but it is not clear to me how to actually use them, so for the time being at least, I am not pursuing that option, rather focusing on using the PHP / GD functionality that I already have but accessing it from a Flex/windowed app..
  • 0