Page 1 of 1

Open URL in clipboard

Posted: 03 Apr 2008 12:56
by jacky
Just a quick thing, that I happen to like ;)

This will simply open (with default web browser) the current URL in clipboard. I use a regexp to locate the URL, because I often select/copy URLs from files I show in XY (Raw) Preview, so I select a full line and not just the exact URL.

For example, on ReadmeXY.txt the line copied to the clipboard would be "Website http://www.xyplorer.com". Using this script I don't have to bother, the URL will be "extracted" and opened in one click :

Code: Select all

"Open URL in clipboard"
	regexreplace $url, <clipboard>, ".*(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?).*", "$1";
	open $url;

Re: Open URL in clipboard

Posted: 20 Oct 2010 08:42
by SkyFrontier
Updated to reflect (small) syntax changes:

Code: Select all

"Open URL in clipboard"
   regexreplace $url, "<clipboard>", ".*(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?).*", "$1";
   open $url;

Re: Open URL in clipboard

Posted: 20 Oct 2010 08:58
by admin
What syntax changes? :?

Re: Open URL in clipboard

Posted: 20 Oct 2010 09:03
by SkyFrontier
regexreplace $url, "<clipboard>",
-doesn't work without quotes...
Bad wording...?

Re: Open URL in clipboard

Posted: 20 Oct 2010 09:19
by admin
SkyFrontier wrote:regexreplace $url, "<clipboard>",
-doesn't work without quotes...
Bad wording...?
Works here. :?

Re: Open URL in clipboard

Posted: 20 Oct 2010 09:31
by SkyFrontier
admin wrote:
SkyFrontier wrote:regexreplace $url, "<clipboard>",
-doesn't work without quotes...
Bad wording...?
Works here. :?
Got it - works only for small portions of text (paragraphs?) (reason why I asked regEx support for readfile). :|

Re: Open URL in clipboard

Posted: 20 Oct 2010 09:33
by admin
SkyFrontier wrote:
admin wrote:
SkyFrontier wrote:regexreplace $url, "<clipboard>",
-doesn't work without quotes...
Bad wording...?
Works here. :?
Got it - works only for small portions of text (paragraphs?) (reason why I asked regEx support for readfile). :|
This has nothing to do with <clipboard>.

There was no syntax change.

Re: Open URL in clipboard

Posted: 20 Oct 2010 09:39
by SkyFrontier
Admin:
This has nothing to do with <clipboard>.
There was no syntax change.
That's what's my "got it" stands for... :wink:
Thanks!

Re: Open URL in clipboard

Posted: 20 Oct 2010 12:57
by TheQwerty
I'd imagine it's not so much the amount of text on the clipboard but rather the content, there is one syntax change that hasn't been reflected - the fact that RegExReplace is now a function so you really should be using:

Code: Select all

"Open URL in clipboard"
   $url = regexreplace "<clipboard>", ".*(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?).*", "$1";
   open $url;
And at that point the use of a variable feels a bit clumsy, so eliminating that and XY will require '(...)' for the RegexReplace and you have:

Code: Select all

"Open URL in clipboard"
   open regexreplace("<clipboard>", ".*(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?).*", "$1");
There's a few basic guidelines I typically follow when scripting:
1) Always use '(...)' for functions and I'll often use them for commands as well, but that's a habit I'm trying to break.
2) If the argument is a string or a variable containing one always quote it.
3) If there is no way a string could contain a variable that would need expanding use '...' otherwise "..." - no idea if this truly makes a difference but XY shouldn't need to parse the '...' strings which should improve performance (be it ever so slightly).

EDIT: For clarity.

Re: Open URL in clipboard

Posted: 20 Oct 2010 15:12
by SkyFrontier
Thank you, TheQwerty.
Particularly for the edit/didactic thing.
I'm trying to adapt such script in means of a full URL extractor, but the problem seems to be the regEx pattern itself - which I'm not even beginning to understand. :|
Feel free to post suggestions on this direction.

Re: Open URL in clipboard

Posted: 20 Oct 2010 20:46
by TheQwerty
What kind of and how much data are you placing on the clipboard?

Re: Open URL in clipboard

Posted: 20 Oct 2010 21:10
by SkyFrontier
@TheQwerty:

Code: Select all

"Extract HTTP Links from Clipboard"
   regexreplace $url, "<clipboard>", ".*(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?).*", "$1";
   text $url, , , ,w;
Test stuff:

Code: Select all

http://www.xypl.com  	

You should also make sure that your login PATH is set up correctly such that you can execute the ruby, gem, rails, and git commands correctly. Setting these things up in your .bashrc (or equivalent) isn't sufficient, because it doesn't get evaluated by default when GUI applications like Aptana Studio 3 get launched

http://www.xyplorer234556.com
If getting your login PATH set up in this way isn't practical for you, you can work around the problem by launching Aptana Studio 3 from the command line, using the studio3 command line utility. This utility can be found at the top level of the Aptana Studio 3 installation folder, so you can put that folder in your PATH for convenience. (The command line utility can also be called after Aptana Studio 3 is running, to get it to open source code files for editing.) "Website http://www.xyplorer.com". 

test stuff
empty wording
If I enter the last paragraph, the (correct) output is:

Code: Select all

http://www.xyplorer234556.com
http://www.xyplorer.com
If I 1) add the last "test stuff/empty wording" portion or 2) the whole quoted text, 1) the e-mails will be reported (not desired) or 2) nothing will happen.

Thank you!

Re: Open URL in clipboard

Posted: 20 Oct 2010 22:00
by TheQwerty
This seems to work pretty well, but some notes...
1) This will parse the clipboard line by line, so if your data does not contain line breaks it will slow down quite a bit, and you should probably insert some before calling this.
2) Related to 1, is that if your URLs span multiple lines, tough luck.
3) This method really does not scale well. I don't think the regex engine XY uses is particularly up to snuff for this kind of task, but for what is meant to be accomplished in XY it works - remember XY's scripting is not meant to replace full functioning programming languages. (Sorry if you disagree Don, I may be wrong, but that's what I'm observing with the regex engine.)
4) The regex pattern I'm using is from John Gruber and it is meant to catch all sorts of URL-looking strings, and is well documented in that previous link. He also has a version that that is slightly reduced and will only work for web URLs. Also note that I had to escape ' in both of these.

I've also included one that is part of JGSoft's RegexBuddy library in the comments. It is not nearly as inclusive as either of Gruber's, but they may be better for you.

You should probably pick the simplest one that works for you; from simplest to not so that would be RegexBuddy, Gruber web only, Gruber all.

Code: Select all

"Extract URLs from Clipboard"
	$hay = "<clipboard>";
	
	// http://daringfireball.net/2010/07/improved_regex_for_matching_urls - All URLs
	$pattern = '\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:''".,<>?«»“”‘’]))';

	// http://daringfireball.net/2010/07/improved_regex_for_matching_urls - Only web URLs
	//$pattern = '\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:''".,<>?«»“”‘’]))';

	// http://www.regexbuddy.com/ - JGSoft - RegexBuddy - URL: Find in Full Text
	//$pattern = '\b((?:https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[A-Z0-9+&@#/%=~_|])';

	$results = '';

	$i = 0;
	$lines = GetToken("$hay", 'count', "<crlf>");
	while ($i < $lines) {
		$i++;
		$line = GetToken("$hay", "$i", "<crlf>");
		$match = RegexReplace("$line", ".*?$pattern", "$1<crlf>");	//Put a CRLF after each URL (and strip leading chars)
		if (Compare("$line", "$match")) { //If line was changed...
			$j = 0;
			$matches = GetToken("$match", 'count', "<crlf>");
			while ($j < $matches) {
				$j++;
				$group = GetToken("$match", "$j", "<crlf>");
				if (Compare("$group", "<crlf>") && Compare("$group", "")) {	//If not CRLF/empty...
					$groupMatch = RegexReplace("$group", "^$pattern$|^.*$", "$1");	//If line matches URL use it, else replace with nothing.
					if (Compare("$group", "$groupMatch") == 0) {	//If line was not changed....
						$results = "$results$groupMatch<crlf>";
					}
				}
			}
		}
	}

	Text "$results";

Re: Open URL in clipboard

Posted: 20 Oct 2010 22:21
by SkyFrontier
Thanks, TheQwerty!
It plays nicely with my source .txts so now I have (finally) a way to back-reference some research stuff I did months ago.
Also, edited my previous post - later I realized a potential bad usage by keeping it as it is (you may know what I'm talking about) so scratched the thing.