JScript issue launching Chrome URL

Hi,

I've created script to search Google in Chrome for the name of the currently selected file/folder. It simply get the file/folder name, removes the path, trims white space, encodes the URI, then opens it in Chrome.

Everything works fine except the last Chrome RunCommand line. Instead of opening a single Chrome Tab with the search, it opens numerous tabs, one even includes the drive letter of the selected file which no longer exists in the variable being passed to the command! Even the RunCommand Clipboard SET works correctly.

Any idea's why it's not passing the final variable correctly? Is this a bug or something I've coded incorrectly? Any help would be greatly appreciated.

Thanks.

function OnClick(clickData) {

	// Confirm file/folder selected
	var selected_files = clickData.func.sourcetab.selected;
	var num_files = selected_files.count;
	if (num_files < 1) { return; }
	
	// Get selected file/folder fullpath
	var enumFiles = new Enumerator(selected_files);
	var currentFile = enumFiles.item();

	// Extract filename from fullpath
	var query = String(currentFile.name).replace(/^.*[\\\/]/, '')

	// Trim white space and encode
	query = encodeURI(query.replace(/^\s+|\s+$/gm,''));

	// Display query string
	var dlg = clickData.func.Dlg;
	dlg.message = "Query: " + query;
	dlg.Show();

	DOpus.Output(query);
	DOpus.create.command.RunCommand("Clipboard SET " + query);
	DOpus.create.command.RunCommand("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe google.com/search?q=" + query);
    
	return;

}

Finally figured it out. The % characters in the encoded URI were being interpreted as environment variables and the respective values injected into the command. Escaping these like you would in bat files seems to work.

All I needed to do was add the following line before RunCommand:

query = query.replace(/%/g,"%%");

1 Like

Just for interests sake, you don't need to run Chrome directly for this sort of thing.

You could just use this code:

DOpus.create.command.RunCommand("https://www.google.com/search?q=" + query);

The URL would launch in your default browser.

1 Like