String Cleaning in AS3

At some point, any­one writ­ing any­thing for the inter­net will even­tu­al­ly need to clean a string to use as a url. I had a request to clean 1,100 of them and fig­ured it was time to write some­thing to make it eas­i­er. This code is in AS3 and was used for an Air appli­ca­tion I was writ­ing to assist in the post­ing of press releas­es.

The Problem

I had a large num­ber of arti­cles in one web­site need­ing to be migrat­ed to anoth­er, and I did not have access to the data­base they were being gen­er­at­ed from, so I had to do page rips of the source code on each page. (Actu­al­ly I may post some­thing about the code I wrote to do the job for me) So not to bore you with details, it is sim­plest to say I now had an xml file with all of the arti­cles split into old link, arti­cle title and arti­cle body. Today I am writ­ing about how I took the arti­cle title and cre­at­ed clean url’s to use for the nav­i­ga­tion.

The Code

//Declare your variables
var str:String;
var dirtyName:String;
var cleanName:String;

//This is the list of bad characters you would like to replace, in an array
var dirty:Array=new Array(RegExp(/Š/g),RegExp(/Ž/g),RegExp(/š/g),RegExp(/ž/g),RegExp(/Ÿ/g),RegExp(/À/g),RegExp(/Á/g),RegExp(/Â/g),RegExp(/Ã/g),RegExp(/Ä/g),RegExp(/Å/g),RegExp(/Ç/g),RegExp(/È/g),RegExp(/É/g),RegExp(/Ê/g),RegExp(/Ë/g),RegExp(/Ì/g),RegExp(/Í/g),RegExp(/Î/g),RegExp(/Ï/g),RegExp(/Ñ/g),RegExp(/Ò/g),RegExp(/Ó/g),RegExp(/Ô/g),RegExp(/Õ/g),RegExp(/Ö/g),RegExp(/Ø/g),RegExp(/Ù/g),RegExp(/Ú/g),RegExp(/Û/g),RegExp(/Ü/g),RegExp(/Ý/g),RegExp(/à/g),RegExp(/á/g),RegExp(/â/g),RegExp(/ã/g),RegExp(/ä/g),RegExp(/å/g),RegExp(/ç/g),RegExp(/è/g),RegExp(/é/g),RegExp(/ê/g),RegExp(/ë/g),RegExp(/ì/g),RegExp(/í/g),RegExp(/î/g),RegExp(/ï/g),RegExp(/ñ/g),RegExp(/ò/g),RegExp(/ó/g),RegExp(/ô/g),RegExp(/õ/g),RegExp(/ö/g),RegExp(/ø/g),RegExp(/ù/g),RegExp(/ú/g),RegExp(/û/g),RegExp(/ü/g),RegExp(/ý/g),RegExp(/ÿ/g),RegExp(/ /g));

//What you would like to replace them with, also in an array
var clean:Array=new Array("S","Z","s","z","Y","A","A","A","A","A","A","C",
"E","E","E","E","I","I","I","I","N","O","O","O","O","O","O","U","U","U","U"
,"Y","a","a","a","a","a","a","c","e","e","e","e","i","i","i","i","n"
,"o","o","o","o","o","o","u","u","u","u","y","y","_");

//Function to loop through the arrays and replace the bad characters in the string
function cleantheString(str:String):void {
for (var i:int=0; i<dirty.length; i++) {
str=str.replace(dirty[i],clean[i]);
}
cleanName=str.toLowerCase();
trace(cleanName);
}

//String to clean
dirtyName="My test name for the story";

//Send the string to the function
cleantheString(dirtyName);

//trace result: my_test_name_for_the_story

Building on the Code

With this as a start­ing point you can load any amounts of strings you need into the func­tion to cre­ate the clean urls. You can add oth­er char­ac­ters you need to replace into the arrays and in the func­tion you can add a .asp or .html to cre­ate the full path. In the one I built I also passed the clean name to a new File() to cre­ate the files and fill them with the code I need­ed for the pages. As with any code there are many dif­fer­ent ways to accom­plish the same goals, this is just one. As and exam­ple, you could put the Reg­Exp() into the func­tion instead of the array, and the list goes on. I hope this will help some­one who was look­ing for a way to clean up some strings.

Develop AIR in Aptana

I have long been a user of open source code and appli­ca­tions. My com­put­er dual boots to Lin­ux and to Win­dows XP. As such, I some­times look for ways to devel­op code in Lin­ux I cur­rent­ly devel­op on Win­dows. A few months back I decid­ed to try my luck with devel­op­ing AIR appli­ca­tions with­out any Adobe prod­ucts and was pleas­ant­ly sur­prised to find a large and thriv­ing com­mu­ni­ty doing the same thing with some very good soft­ware.

Aptana

Aptana Stu­dio 2 is an awe­some IDE, it took most of the things I was try­ing to do in Eclipse and made them eas­i­er and more intu­a­tive. It can of course do much more than AIR Appli­ca­tions and I am actu­al­ly using it right now to update this blog. The doc­u­men­ta­tion they offer is great, as you can see here with the instruc­tions they gave for installing
Aptana on Lin­ux.

Beyond installing and such, the plu­g­ins they pro­vide allow the devel­op­ment of AIR appli­ca­tions to be seam­less across all the lan­guages. There is a ton of infor­ma­tion
already writ­ten on how to get AIR devel­op­ment up and run­ning avail­able on the web. The Aptana site has some good infor­ma­tion here and there are quite a few blogs already writ­ten on how to cre­ate a basic appli­ca­tion.

I hope this will point some­one in the direc­tion of open source and allow any­one with­out Adobe Flash or Adobe Flex who is inter­est­ed in AIR Devel­op­ment to get start­ed as
quick­ly as pos­si­ble.

Keywords for Digital Assets

Any pho­tog­ra­ph­er, pro­fes­sion­al, ama­teur or week­end dab­bler, will even­tu­al­ly accu­mu­late a large amount of images. One of the best tools you will have in sort­ing and iden­ti­fy­ing is key­words. Sim­ply put, a key­word is a sin­gle word describ­ing the con­tents of a pho­to­graph. You may think a sin­gle word does not seem very pow­er­ful in terms of try­ing to find sin­gle images in a library of thou­sands, but it is. Let me show you why and give you a few tips on how to use them effec­tive­ly.

Lets start with a sim­ple exam­ple. You are a cor­po­rate pho­tog­ra­ph­er who spe­cial­izes in prod­uct pho­tog­ra­phy, but also have expand­ed into envi­ron­men­tal por­traits and jour­nal­is­tic pho­tog­ra­phy show­ing peo­ple build­ing or work­ing on the prod­ucts. Over the course of a busy year you can gen­er­ate thou­sands of images, after a few years you may need to look back and find some spe­cif­ic images. If you used a few key­words while archiv­ing your work you could save your­self hours of search­ing.

Remem­ber key­words describe the con­tent of your pho­to­graph, not the appli­ca­tion. I am mak­ing this point because you may be tempt­ed to put titles of pub­li­ca­tions, cus­tomers names, and oth­er infor­ma­tion about how the pho­to­graph was used into key­words. Try to stop your­self from wan­der­ing down that path, it is impor­tant infor­ma­tion, but is bet­ter suit­ed in the descrip­tion or oth­er fields in meta­da­ta. Key­words should be sim­ple and come from a grow­ing library of very spe­cif­ic words.

Back to our exam­ple, while you were sav­ing your images you used just the sim­ple key­words of peo­ple and prod­uct. With these two words you can slice your pile of images by peo­ple (every image with a per­son in it), prod­uct (every image with prod­uct in it) and peo­ple and prod­uct (every image with both peo­ple and prod­uct). Por­traits would come up with peo­ple, prod­ucts would come up with prod­ucts, envi­ron­men­tal por­traits would be found by peo­ple and prod­ucts. As your library of key­words grows, you can cut up your pile of images into small­er and small­er chunks. Exam­ple: peo­ple, prod­uct, ros­es, out­side — every image you took of peo­ple work­ing with ros­es out­side would be in this pile of images. Com­bine this with a loca­tion and cus­tomer in meta­da­ta and you can find any image eas­i­ly. For now lets see how to actu­al­ly apply and store key­words.

Adobe Bridge Keywords

This is Adobe Bridge, the ver­sion used for these instruc­tions is CS3, but the steps are the same up to the cur­rent CS5.

 

 

 

To see this par­tic­u­lar view of the Bridge, select the Meta­da­ta Focus, the num­ber 3 in the low­er right cor­ner.

 

If you would like to keep your cur­rent view but open up the Key­word pan­el to work with, just use the Win­dow Menu and select Key­word Pan­el

 

The ini­tial set­up of the Key­word Pan­el is real­ly not much use, but is a good start­ing point for using the tools to add and remove items from your key­word list.

At the bot­tom of the pan­el you will see a trash can, plus sign (+) and an angle arrow with a plus sign. The garbage is used to throw out key­words you do not need, the big plus sign is to add cat­e­gories and the plus sign with the angle is used to add key­words below the cat­e­gories. Using these you can rebuild the list to match the key­words you would like to use.

I have edit­ed this one to show how the key­words in our exam­ple may have been laid out. You can use the main cat­e­gories as key­words, such as the com­pa­nies name, flow­ers, prod­ucts or any oth­er work you may find help­ful. The sub­cat­e­gories would be a bit more infor­ma­tion speak­ing to the main cat­e­go­ry. Exam­ple, flower — rose or engine — pis­ton.

Once you have your list you just need to select the image you would like to add key­words to and check the box­es. The shift and con­trol short­cuts work to edit mul­ti­ple files. Try to keep the key­words sim­ple and just one word. My last bit of advice would be to apply all of your meta­da­ta, key­words, copy­right, and every­thing to your raw files imme­di­ate­ly after down­load­ing. This way no mat­ter how you edit or re-pur­pose them, the infor­ma­tion will still be with them and you can find all of the ver­sions at once.