[Project_owners] run Shell Script on Mac with nsIProcess

Gary Johnson gary.johnson.53 at gmail.com
Thu Sep 6 12:18:17 PDT 2007


/*
I need to unzip files that have characters that FF does not currently support
I am looking at using unix commands to do my work
I am trying to get a shell process to run with nsIProcess on MAC OS 10
with no luck
I create the file gjtemp.sh fine

*/

function runShelScriptonMac(what)
{
//	get path to my extensions temp directory created during install
//  any existing directory will do
	var sstrpart1 = getFFpath('ProfD')
	var sstrpart2 = '/extensions/{2ea91495-764f-44ab-9639-dcb810fa1b43}/temp';

//	create a Moz file obj to that path

	var zipdir = Components.classes["@mozilla.org/file/local;1"]
						.createInstance(Components.interfaces.nsILocalFile);

	Components.utils.reportError("the path " + sstrpart1 + sstrpart2);
	zipdir.initWithPath( sstrpart1 + sstrpart2 );

//	create a file for Mac script comands
	var scriptfile = zipdir.clone();

	scriptfile.append("gjtemp.sh");

//	you have to write to it

	var fos = Components.classes["@mozilla.org/network/file-output-stream;1"]
					.createInstance(Components.interfaces.nsIFileOutputStream);
	fos.init(scriptfile, -1, -1, false);

//	shell commands are supposed to start with this

	var line = '#!/bin/sh' + "\n";

//	set up a change directory line in the script file to make sure I
end up in my temp dir

	line += "cd ";

	line += "/p ";

	line += "\"" + zipdir.path + "\"\n";

	fos.write(line, line.length);

//	run a test command and write results to a file called test1.out
	line = "set > test1.out";

	line += "\n";
	fos.write(line, line.length);
	fos.close();


	Components.utils.reportError("the command " + line);

//	Get an nsIProcess

	var process = Components.classes["@mozilla.org/process/util;1"]
						.createInstance(Components.interfaces.nsIProcess);

//	Create a file obj for the shell

	var sh = Components.classes["@mozilla.org/file/local;1"]
						.createInstance(Components.interfaces.nsILocalFile);

	sh.initWithPath("/bin/sh");

	process.init(sh);
//	process.init(scriptfile);  //this is what you do on windows

	var args = [scriptfile.path];

	Components.utils.reportError(args);


	try
	{
		process.run(true, args, args.length);
	}
	catch (err)
	{
		Components.utils.reportError(err);
	}

//	where are my results?  I must be doing something wrong
}

/*
String 			Meaning
ProfD 			profile directory
*/

function getFFpath(what)
{
	try
	{



		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		// get profile directory
		var myfileobj = Components.classes["@mozilla.org/file/directory_service;1"]
					.getService(Components.interfaces.nsIProperties)
					.get(what, Components.interfaces.nsIFile);

		return(myfileobj.path);
	}
	catch(err)
	{
		Components.utils.reportError(err);
	}
return false;
}

-- 
I hope to be the kind of person my dog thinks I am.   :)>

http://mywebpages.comcast.net/adgj/
http://wwwCookDotCom.com


More information about the Project_owners mailing list