Selected text : Google it, Open URL or Open folder

Post your working scripts, libraries and tools for AHK v1.1 and older
QobbAlt
Posts: 8
Joined: 21 Feb 2017, 13:43

Selected text : Google it, Open URL or Open folder

29 Jun 2017, 09:38

Hi!

I want to share this function I've been working on for a while now.
Basically, it'll open your selected text either in your browser, in a new Explorer window or execute a Google search depending on the type of your selected text.

Example:
If you highlight those examples and run press the hotkey:
C:\Program Files\AutoHotkey
-> will open a new Explorer at the selected directory
-> will open the selected URL in the default browser
AutoHotKey
-> will open a new tab in your default browser and execute a google search

Code: Select all

OpenHighlighted()
{
	MyClipboard := "" ; Clears variable

	Send, {ctrl down}c{ctrl up} ; More secure way to Copy things
	sleep, 50 ; Delay
	MyClipboard := RegexReplace( clipboard, "^\s+|\s+$" ) ; Trim additional spaces and line return
	sleep, 50
	MyStripped := RegexReplace(MyClipboard, " ", "") ; Removes every spaces in the string.
	
	StringLeft, OutputVarUrl, MyStripped, 8 ; Takes the 8 firsts characters
	StringLeft, OutputVarLocal, MyStripped, 3 ; Takes the 3 first characters
	sleep, 50
	
	if (OutputVarUrl == "http://" || OutputVarUrl == "https://")
	{
		TrayTip,, URL: "%MyClipboard%" ;
		Sleep,50
		Run, "%MyStripped%"
		Return
	}
	else if (OutputVarLocal == "C:/" || OutputVarLocal == "C:\" || OutputVarLocal == "Z:/" || OutputVarLocal == "Z:\" || OutputVarLocal == "R:/" || OutputVarLocal == "R:\" ||)
	{
		TrayTip,, Windows: "%MyClipboard%" ;
		Sleep,50
		Run, %MyClipboard%
		Return
	}
	else
	{
		TrayTip,, GoogleSearch: "%MyClipboard%" ;
		Sleep,50
		Run, "http://www.google.com/search?q=%MyClipboard%"
		Return
	}
	Return
}
I'm 100% sure it can be cleaner and prettier, but for now, it works great!

Let me know if you have any comment/idea/improvement.

Enjoy!
Last edited by QobbAlt on 29 Jun 2017, 17:20, edited 3 times in total.
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Highlight text : Google it, Open URL or Open folder

29 Jun 2017, 09:50

This looks like single line of code.

Code: Select all

Run % "http://www.google.com/search?q=" Clipboard
Where is the "Highlight" part ?
Don't you mean yellow (or some other color) block ???
QobbAlt
Posts: 8
Joined: 21 Feb 2017, 13:43

Re: Highlight text : Google it, Open URL or Open folder

29 Jun 2017, 10:05

IMEime wrote:This looks like single line of code.

Code: Select all

Run % "http://www.google.com/search?q=" Clipboard
This line of code does the last part if the function, if the highlighted text is no URL nor Local path.
Where is the "Highlight" part?
You have to highlight some text, then press a hotkey (I assigned #g to run OpenHighlighted() ). The script will do the rest.
Don't you mean yellow (or some other color) block ???
I'm not sure to understand what you mean by yellow block
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Highlight text : Google it, Open URL or Open folder

29 Jun 2017, 10:40

this is highllight text
캡처.jpg
캡처.jpg (383.11 KiB) Viewed 6710 times
QobbAlt
Posts: 8
Joined: 21 Feb 2017, 13:43

Re: Highlight text : Google it, Open URL or Open folder

29 Jun 2017, 10:52

I see!
I meant selected text then. When you select it with your mouse :)

edit: I corrected the thread title
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Selected text : Google it, Open URL or Open folder

06 Jul 2017, 11:44

Nice! Tnx 4 sharing
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Selected text : Google it, Open URL or Open folder

06 Jul 2017, 15:20

I've improved it a tiny bit using StrReplace instead of RegExReplace in one instance, and removing a bit of repetition in the if lines.

Code: Select all

q::
OpenHighlighted()
return

OpenHighlighted()
{
	MyClipboard := "" ; Clears variable

	Send, {ctrl down}c{ctrl up} ; More secure way to Copy things
	sleep, 50 ; Delay
	MyClipboard := RegexReplace( clipboard, "^\s+|\s+$" ) ; Trim additional spaces and line return
	sleep, 50
	MyStripped := StrReplace(MyClipboard, " ", "") ; Removes every spaces in the string.

	StringLeft, OutputVarUrl, MyStripped, 8 ; Takes the 8 firsts characters
	StringLeft, OutputVarLocal, MyStripped, 3 ; Takes the 3 first characters
	sleep, 50

	if (OutputVarUrl == "http://" || OutputVarUrl == "https://")
		Desc := "URL", Target := MyStripped
	else if (OutputVarLocal == "C:/" || OutputVarLocal == "C:\" || OutputVarLocal == "Z:/" || OutputVarLocal == "Z:\" || OutputVarLocal == "R:/" || OutputVarLocal == "R:\" ||)
		Desc := "Windows", Target := MyClipboard
	else
		Desc := "GoogleSearch", Target := "http://www.google.com/search?q=" MyClipboard

	TrayTip,, %Desc%: "%MyClipboard%" ;
	Sleep,50
	Run, %Target%
	Return
}
This is a nice script, thanks for sharing, I've had a similar script for fair while, possible things to consider would be:
- are the Sleep lines necessary?
- can the Google search handle special characters like &, in which case, some conversion to URI might be necessary
- open any registry keys in RegEdit or some other registry editor
- if the path is not found, open the folder instead (if it exists)
Cheers.

The folder/registry ideas are just little embellishments really, but I eventually had use for them and added them to the script.
Last edited by jeeswg on 06 Jul 2017, 18:51, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Selected text : Google it, Open URL or Open folder

06 Jul 2017, 15:32

Suggestion:
Change

Code: Select all

sleep, 50 ; Delay
to

Code: Select all

ClipWait
This will be more reliable when copying to the clipboard.

All the other sleeps could be removed also.
jayson

Re: Selected text : Google it, Open URL or Open folder

24 Aug 2017, 05:20

Could you add to the code in selecting a specific potion of the page opened and copy to clipboard?
zhotkey
Posts: 14
Joined: 21 Jun 2017, 08:22

Re: Selected text : Google it, Open URL or Open folder

24 Aug 2017, 08:33

The URL doesn't work properly for me. If the highlighted URL begins with http:// it just does a Google search on it. If it starts with https:// it works properly.
tonbao

Re: Selected text : Google it, Open URL or Open folder

12 Sep 2018, 23:36

SendMode Input
RegRead, OutputVar, HKEY_CLASSES_ROOT, http\shell\open\command
StringReplace, OutputVar, OutputVar,"
SplitPath, OutputVar,,OutDir,,OutNameNoExt, OutDrive
browser=%OutDir%\%OutNameNoExt%.exe

^!s::
{
BlockInput, on
prevClipboard := ClipboardAll
VarSetCapacity(prevClipboard, 0)
Clipboard=
Send, ^c
BlockInput, off
ClipWait
if !(ErrorLevel) {
Clipboard := RegExReplace(RegExReplace(Clipboard, "\r?\n"," "), "(^\s+|\s+$)")
If SubStr(ClipBoard,1,7)="http://"
Run, % Clipboard
else
Run, % "http://www.google.com/search?hl=en&q=" Clipboard
}
Clipboard := prevClipboard
return
}
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Selected text : Google it, Open URL or Open folder

29 Jan 2019, 03:36

Xtra wrote:
06 Jul 2017, 15:32
Suggestion:
Change

Code: Select all

sleep, 50 ; Delay
to

Code: Select all

ClipWait
This will be more reliable when copying to the clipboard.

All the other sleeps could be removed also.
Correct. In general, one should be careful about using Sleep. Don't put it in the script, unless you understand it's clearly needed. There are times you will indeed need the Sleep command, but be sure by testing the script and seeing how it works without them and looking at putting in other commands. And in the opposite, like if were using the old AutoScriptWriter or AHKScriptWriter to record your actions and make a script, then remove the sleep commands (after testing), unless proven necessary.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 200 guests