Make url from filename component, launch url

Hi there,
I am trying to create a button that launches a url that is built by using a component of the file's name.
Extracting the component is no problem, but I don't know enough about internal commands or JS to complete the task.
Would someone kindly point me in the right direction?

Here is what I've tried.

1. Internal command
The command below correctly creates the url and copies it to the clipboard. Now how do I launch the url?

Clipboard COPYNAMES=nopaths REGEXP "\[(?:[^\]]+_)?tt=([^\]_]+)" "http://http://www.imdb.com/title/\1"

2. Script
The portion of script below should build the url. But the two sections starting with ??? show that I have no idea how to get the file name in the first place, then how to launch the url.

??? var fileName = ???

var pattern = /\[(?:[^\]]+_)?tt=([^\]_]+)/i;
var matchArray = pattern.exec(fileName);
if (matchArray != null) {
	var tt = matchArray[1]
        var url = "http://http://www.imdb.com/title/" + tt
???        // Go To url 
}

Thanks! :smiley:

This could be of help..

var cmd = data.func.command, tab = data.func.sourcetab; for (var iEnum = new Enumerator(tab.selected_files); !iEnum.atEnd(); iEnum.moveNext()){ var file = iEnum.item(); //your code here.. //break; here if you only want to make this work on the first selected file }
"Running" an url is easy with "Start ". Start.exe is a windows command.
My experience is, that omitting "Start" when launching Urls from DO works even better.
So in oldschool-button-language the plain url should do and in a script it's cmd.RunCommand("");

@tbone Thank you, that was very helpful!!!
:thumbsup:

It works perfectly, except for the break: if three files are selected, the button launches imdb three times instead of just once. It opens three times the same page, corresponding to the tt extracted for the first file.

Do you see what might be wrong in the button's code?

@script:jscript function OnClick(data) { var cmd = data.func.command, tab = data.func.sourcetab; var pattern = /\[(?:[^\]]+_)?tt=([^\]_]+)/i; for (var iEnum = new Enumerator(tab.selected_files); !iEnum.atEnd(); iEnum.moveNext()){ var file = iEnum.item(); var matchArray = pattern.exec(file); if (matchArray != null) { var tt = matchArray[1]; var url = "http://www.imdb.com/title/" + tt; cmd.RunCommand(url); break; } } }

Add a line with cmd.ClearFiles(); before the for-loop.

Awesome. Thanks, Leo! :slight_smile:

In case someone else wants to do something similar:

  • in the button, make sure to select function: script function

Version that only launches the url for the first selected file:

@script:jscript function OnClick(data) { var cmd = data.func.command, tab = data.func.sourcetab; var pattern = /\[(?:[^\]]+_)?tt=([^\]_]+)/i; cmd.ClearFiles(); enumFiles = new Enumerator(tab.selected_files); var file = enumFiles.item(); var matchArray = pattern.exec(file); if (matchArray != null) { var tt = matchArray[1]; var url = "http://www.imdb.com/title/" + tt; cmd.RunCommand(url); } }

Version that launches the url for each of the files selected:

@script:jscript function OnClick(data) { var cmd = data.func.command, tab = data.func.sourcetab; var pattern = /\[(?:[^\]]+_)?tt=([^\]_]+)/i; cmd.ClearFiles(); for (var iEnum = new Enumerator(tab.selected_files); !iEnum.atEnd(); iEnum.moveNext()){ var file = iEnum.item(); var matchArray = pattern.exec(file); if (matchArray != null) { var tt = matchArray[1]; var url = "http://www.imdb.com/title/" + tt; cmd.RunCommand(url); } } }

Good! o) I may throw in one last thought:
Put that into a function and add a toggle-var and you have a smallish all-in-one and even easier to (re-)use variation.
Giving that snippet a name by wrapping a function around, you don't even need to put a comment anywhere to make yourself or anybody else recognize quickly what these lines are about.

@script:jscript var FirstFileOnly = true; /////////////////////////////////////////////////////////////////////////////// function OnClick(data) { LaunchUrl(data, FirstFileOnly); } /////////////////////////////////////////////////////////////////////////////// function LaunchUrl( data, firstFileOnly){ var cmd = data.func.command, tab = data.func.sourcetab; var pattern = /\[(?:[^\]]+_)?tt=([^\]_]+)/i; cmd.ClearFiles(); for (var iEnum = new Enumerator(tab.selected_files); !iEnum.atEnd(); iEnum.moveNext()){ var file = iEnum.item(); var matchArray = pattern.exec(file); if (matchArray != null) { var tt = matchArray[1]; var url = "http://www.imdb.com/title/" + tt; cmd.RunCommand(url); } if (firstFileOnly) break; } }

Simple and elegant... Thank you, great call. :slight_smile:
I'll be writing a tut showcasing situations where something like this can be useful.

Nice script. :thumbsup:

I was looking for something similar, but searches IMDB for the details on the movie, based on the filename, so I modified this a bit, and thought you might find a use for it.

After using Filebot to rename movies as I like them, for my Plex server, the names always look like this:

Ex Machina (2015) - [HD 1080p x265 1920x804] [AAC 6ch].mkv

Always the movie name first, then the year. The rest isn't useful for the IMDB search, so my regex only finds the Year, in parens, then extracts it + everything to the left of it, and finally, launches the web browser to search IMDB for that name + the year.

A couple things of note, that are different from yours;
I added .name_stem to the regex search, so that it only searches the actual filename(s), instead of the entire path. it seemed like a better idea to me. :slight_smile:
I also wasn't sure what the purpose of having the "only search first file" script/option; I thought "why not just select a single file/the file you want, to do the search for then?" lol, so what I did was mish-mash tBone's example into a function that just checks whether 1 file is selected, or multiple files are selected; and set the true/false flag depending on how many files were selected, so that it's fully automatic now. You don't have to worry about how many are selected, you just select either 1 file, or as many as you want, and it'll handle them as usual. Not sure, if that's ideal for your situation, but, up to you. :smiley:
I was having trouble with cmd.RunCommand and URLs, in that even with URL Encoding, it was acting strange, so I switched to wscript instead.

What I do is, select "File Types" from the DO menu, select the "movies" file group, then add a context-menu item, where I entered this script. I went out and dug up a small IMDB icon to add to the item as well, so now I just right-click on any movie file and the "Search on IMDB" item is there.

All-in-all, very useful script! Thanks guys, for the head start!

@disablenosel
@script:jscript

function OnClick(data) {

	var tab = data.func.sourcetab;
	var count = tab.selstats.selfiles;

	var FirstFileOnly = (count == 1) ? true : false;

	LaunchURL(data, tab, FirstFileOnly);
}

function LaunchURL(data, tab, firstFileOnly) {

  var cmd = data.func.command;
  
  var pattern = /.*\(\d{4}\)/;
  
  cmd.ClearFiles();
  
  for (var iEnum = new Enumerator(tab.selected_files); !iEnum.atEnd(); iEnum.moveNext()) {
  
	var file = iEnum.item();
	var matchArray = pattern.exec(file.name_stem);

	if (matchArray != null) {

		var url = 'find?ref_=nv_sr_fn&q=' + encodeURI(matchArray[0]) + '&s=all';
		var oShell = new ActiveXObject("WScript.Shell");

		oShell.Run('http://www.imdb.com/' + url);
	}
	if (firstFileOnly) break;
  }
}

Hi
actually i also wanted to do the same thing
does this script needs OPUS 10 or higher?
i tried to add this script to button but nothin happens.

There was no scripting support in Opus 10 (except for Rename Scripts). Anything that uses scripting, outside of the Rename Presets area (and many of the newer things even within that area), will not work with such an old version.

This hasn't changed in the last two days. :slight_smile:

Yeah i agree..
Actually i been away ..rather very busy.
And lot did changed,its just im not updated Yet.
Its on my mind to buy new version ..
if budget allows.
:smirk: