Help with Java code

Hi,

the code below comes from http://www.inspiredgeek.com/2010/12/06/launch-windows-application-directly-from-firefox-a-retake/ and is used to launch windows executables from FireFox through Custom Buttons.

I'm running this on a thumbdrive (FireFox portable) and I need to code to point to the drive itself. In the first line I can set the path to the directory where the apps are found, but I don't know how to tell it to look in its own directory or one up, as the drive's letter changes from time to time.

Any ideas/

Thanks,
Jay
 
var sCuts=dirList("c:/");
var obj=this;
var nMItem=function(label,img,path){
try{
    path = path.replace(/\\/g,'\/');
  var m=document.createElement('menuitem');
  m.setAttribute('label',label);
  m.f=obj.startProcess.bind(obj);
  m.setAttribute('oncommand','this.f("'+path+'")');
  m.setAttribute('class','menuitem-iconic');
  m.setAttribute('image',img);
  m.tooltipText = "Shortcut to "+path;
  return m;
  }
    catch(ex)
    {
        window.alert("Error 1 : " + ex.message);
    }
}

this.startProcess=function(path){
try{
var file = Components.classes['@mozilla.org/file/local;1']
    .createInstance(Components.interfaces.nsILocalFile);
var process = Components.classes['@mozilla.org/process/util;1']
    .getService(Components.interfaces.nsIProcess);
 path = path.replace(/\//g,'\\');
 file.initWithPath(path);
 file.launch();
  }
    catch(ex)
    {
        window.alert("Error 2 : " + ex.message);
    }
}

var mP=createMenu(sCuts);

function createMenu(sCuts)
{
try
{
	var mP=document.createElement('menupopup');
	for (var i=0;i<sCuts.length;i++){
	 if (sCuts[i] =='separator'){
	  mP.appendChild(document.createElement('menuseparator'));
	 }else{
		//alert(sCuts.length);
	  mP.appendChild(nMItem(sCuts[i] [0],sCuts[i] [1],sCuts[i] [2]));
	 }
	}
	return mP;
  }
    catch(ex)
    {
        window.alert("Error : " + ex.message);
    }

}

function dirList(sFilePath)
{
    sFilePath = sFilePath.replace(/\//g,'\\');
	//alert(sFilePath);
    try
    {
        var dir = Components.classes["@mozilla.org/file/local;1"]
                    .createInstance(Components.interfaces.nsILocalFile);
	if (!dir) return false;
        dir.initWithPath(sFilePath);
	//mP.appendChild(file.);
		var contents=dir.directoryEntries;

		var apps=new Array;
	while(contents.hasMoreElements())
	{
	  var i= contents.getNext();
	  var file=i.QueryInterface(Components.interfaces.nsILocalFile);
		if(file.isSymlink())
		{
			apps.push([file.leafName,this.image,file.path]);		
		}
	}
	return apps;
    }
    catch(ex)
    {
        window.alert("Error : " + ex.message);
    }
}


try{
	this.orient="horizontal";
	this.appendChild(mP);
	this.type='menu';
}
catch(ex)
{
  window.alert("Error : " + ex.message);
}
	
	/*---------------------- Show Focus Block Start ------------------------*/

this.style.opacity = '.65';
this.setAttribute('onmouseover', 'custombuttons.ButtonBrt(event);' );
this.setAttribute('onmouseout', 'custombuttons.ButtonDim(event);' );

/*----------------------- Show Focus Block End -------------------------*/

Open in new window

jiiins2Asked:
Who is Participating?
 
jiiins2Author Commented:
This does the trick:  
Components.classes["@mozilla.org/file/directory_service;1"].getService( Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile).path

Open in new window

0
 
for_yanCommented:
In JavaScript you can check for existence of a file
through FileSystemObject.
Below is an example of how you can do it.

You can make a loop through possible
drive letters of the thumbdrive and when you find
your filepath existing you'll find the letter of your thumbdrive.

You can probably use DriveType property of the
FileSystemObjecct
http://msdn.microsoft.com/en-us/library/ys4ctaz0%28v=vs.85%29.aspx#Y114
to check the letter before you chek the file existence.
I'm not sure of the syntax.

I guess not all browsers will allow you to use ActiveX though,
but at leats this is some way, maybe it could work.

<script type="text/javascript">

// initialize ActiveXObject and create an object of Scripting.FileSystemObject.

var fso = new ActiveXObject("Scripting.FileSystemObject");

 

// if condition to check whether the specified file exists or not.
if(fso.FileExists("C:\\Temp\\myFolder\\file.txt"))

{
document.write("File.txt exists.");

}

else

{
document.write("File.txt does not exist.");

}

 

fso = null;

</script>

Open in new window


0
 
jiiins2Author Commented:
Thanks. Or is there any chance that FireFox passes its own location to extensions and add-ons?
0
 
jiiins2Author Commented:
It's exactly the answer to my question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.