Open URL in clipboard

Discuss and share scripts and script files...
Post Reply
jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Open URL in clipboard

Post 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;
Proud XYplorer Fanatic

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Open URL in clipboard

Post 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;
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

admin
Site Admin
Posts: 60624
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Open URL in clipboard

Post by admin »

What syntax changes? :?

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Open URL in clipboard

Post by SkyFrontier »

regexreplace $url, "<clipboard>",
-doesn't work without quotes...
Bad wording...?
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

admin
Site Admin
Posts: 60624
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Open URL in clipboard

Post by admin »

SkyFrontier wrote:regexreplace $url, "<clipboard>",
-doesn't work without quotes...
Bad wording...?
Works here. :?

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Open URL in clipboard

Post 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). :|
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

admin
Site Admin
Posts: 60624
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Open URL in clipboard

Post 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.

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Open URL in clipboard

Post 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!
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Open URL in clipboard

Post 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.

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Open URL in clipboard

Post 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.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Open URL in clipboard

Post by TheQwerty »

What kind of and how much data are you placing on the clipboard?

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Open URL in clipboard

Post 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!
Last edited by SkyFrontier on 20 Oct 2010 22:17, edited 1 time in total.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Open URL in clipboard

Post 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";

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Open URL in clipboard

Post 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.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Post Reply