Linux Frustrations

Ahhhh Linux, those who use you, claim to love you, but I propose they’re all just trying to sound superior.

I use Linux. I can’t stand it. I often joke that OSX takes one extra step compared to Windows. Want to move the window to the right side on Win 7? Drag it all the way to the right and it sticks 50% right. Want to do this in OSX? drag it till it’s about 50%, then resize it. Sure, can do it, but it’s one extra step. Comparatively, doing anything in Linux requires that you try it, fail, read a manual, take 5 extra steps AND pray. And even then, you don’t get the same result as everyone else. Linux flavors are as varied as grains of sand on the beach, and so, anything I post here will probably fall on blind ears unless you happen to be in the same place I was when I started. My apologies, I wish I had time to spin up a VM for every flavor of Linux to perform my explanations, but alas, I’d rather be coding. =D

Installing the Sun JDK in Ubuntu Linux

All I wanted was the Sun JDK on the new, shiny Ubuntu I spun up in VMWare. Eclipse (among other things) recommends you do this for performance reasons. So, I tried. I looked through the apt-get system, nothing. I looked at sun’s website and was greeted with a plethora of errors when I got the rpm, installed rpm using apt-get and then tried to rpm -ivh the package.
this is what my error partially looked like:

Failed dependencies:
	/bin/basename is needed by jdk-2000:1.7.0-fcs.i586
	/bin/cat is needed by jdk-2000:1.7.0-fcs.i586
	/bin/cp is needed by jdk-2000:1.7.0-fcs.i586
	/bin/gawk is needed by jdk-2000:1.7.0-fcs.i586
	/bin/grep is needed by jdk-2000:1.7.0-fcs.i586

the final solution was on happy-coding.com and went as so (I would have linked, but it was tough to notice that you had to do 3 steps and not just two–keepin it simple):

apt-repository "deb http://archive.canonical.com/ lucid partner"
sudo apt-get update
sudo apt-get install sun-java6-jdk

Why use Math.random? Use a Date instead

I’ve always wondered about this. Cache busting is a common need in my field, and I always see people doing something like the following:

Math.floor( Math.random() * 1000000 );

this will produce something like 809959 and *should* be different every time. If all you’re looking for is a unique number, why not use the Unix timestamp? When it comes to cache busting, if the same user is able to reload the same asset twice in the same millisecond, I say they deserve to get weird results.
That said, it’s not technically random, and it’s possible for your code to produce recurrences in the same millisecond, so use it wisely.
Javascript:

new Date().getTime()

Actionscript:

new Date().time()

PHP:

microtime();

PHP and SSH and SCP – exec, backtics, popen, or proc_open

I can SSH into a server using an ssh key I was given. It’s pretty simple, I just open a terminal and type in

ssh -i key.ssh me@myserver.com cms

and then it opens up like it’s a local box. I can mkdir, rm, and cp all I want. The problem is, what if I want to do similar functionality in PHP? There’s at least 4 ways to execute a command, but while some of them will work for some things, I’ve only succeeded in calling ssh one way. Here’s what I’ve learned.
SCP
Using backtics seems like the simplest solution and seems to work fine.

$scp = "scp -i key.ssh";
$scp .= "sourcefile.zip ";
$scp .= "me@myserver.com:/www/export/destfile.zip";
$scp_exec = `$scp`;

using ` around your command causes it to execute.

SSH
There’s only one way I’ve actually succeeded in running SSH commands on my server. There’s plenty of blog posts out there about how you might do it, but ultimately, they’re vague or incomplete. I hope this helps someone.

//build the array that indicates which part of the call does what
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read
1 => array("pipe", "w"), // stdout is a pipe that the child will write
2 => array("pipe", "w") // stderr is a pipe that the child will write
);
$mkdir = "ssh -i key.ssh me@myserver.com 'mkdir -v www/export/newfolder'"';
$mkdir_handler = proc_open($mkdir, $descriptorspec, $pipes);
$mkdir_write = stream_get_contents($pipes[1]);
$mkdir_result = "mkdir result: \n" . $pipes[1];
proc_close($mkdir_handler);

As far as I know, the ssh call is only executed when stream_get_contents is called. The result of this call is placed into the pipes array in the 1th item. Don’t forget to close your session!

Getting Named Parameters for a Component in Flash using JSFL

When working with components, using JSFL, you can get access to the selected Component using

fl.getDocumentDOM().selection[0]

If your component has parameters, you should be able to access each parameter’s name attribute, but for some reason, there’s a bug in Flash. You can still get the parameters, but it takes a little doing.
The following script will return an array of parameters for the selected component.

var attribs:Array = MMExecute("var a = [];" +
	"for(var i  in fl.getDocumentDOM().selection[0].parameters){" +
	"	a.push(i);" +
	"}" +
	"a.splice(a.length/2).join(',')").split(",");

How does it work?
When you use the for…in feature on the parameters array, it returns double the results that it should, given the length attribute. For example, if you were to say

for(var i = 0; i< fl.getDocumentDOM().selection[0].parameters.length; i++)

you might get 11 results, whereas with this:

for(var i  in fl.getDocumentDOM().selection[0].parameters)

you'd get 22, the same results, followed by a list of names. It's as though there's another named value array tacked on to the results of the for...in. I've used this to produce a double length array, which I then chop off half and return a joined String.
We then parse this string back into an array and can move through it.

Creating a swc from Flex

I spent a bit of time looking for a decent answer to this and only found it halfway in a video. Here we go:
To make a swc from a flex file

  1. Prepare your files
  2. Make a new Flex Library Project with the name you desire for your swc
  3. Copy your files into the src directory, maintaining directory structure where applicable
  4. In the Flex Library Build Path settings for the Library Project select the src directory
  5. Right click the project and select “Build Project”

Your swc will appear in the bin directory.