Thursday, April 28, 2005

opening the optical drive tray using javascript

My new machine has been working pretty well since I built it. I have been building a lot of Flash, Flex, .NET and general web stuff on it with no problems.

There is one flaw with the machine though. The button on my case for my optical drive does not match the button on the optical drive, so every time I want to open my drive tray, I have to go to My Computer >> right click on the drive letter and click eject.

I found numerous examples of how to open the cd tray using vbscript, but doing so rendered the drive unusable until I killed the wscript.exe process(es) from Windows Task Manager.

I re-wrote the vbscript into Javascript and it works great. Here is the code I am using:


var WMP;
var CDS;
WMP = new ActiveXObject("WMPlayer.OCX.7");
CDS = WMP.cdromCollection;
CDS.item(0).eject();
CDS = null;
WMP.Close();
WMP = null;


To use it quickly, you can simply open a text editor, copy and paste the above text, and save the file on your desktop with a name like "cdopen.js." When you want to open the drive, just double-click the file on your desktop.

I put the file in a folder called "shortcuts" on my c drive, which I have added to my PATH environment variables. I picked up this little trick from LifeHacker last week, and it has been a great tool. This way I can hit my Windows key, hit "r" (for "run"), type "cdopen.js", hit enter and "ta-da" my drive tray opens.

I actually took this even one step further by creating a shortcut to the js file in the shortcuts folder and naming it "cd". So now, I just hit Windows Key >> type "r" >> type "cd" >> hit enter and there I go.

0 Comments:

Post a Comment

<< Home