Jump to content

Google Search Shortcut Script


BlueRabbit
 Share

Recommended Posts

Dear experts,

I thought this would be easy: All I want my script to do, is to copy the actual highlighted text (e.g. in a browser or in a pdf or anywhere) into the clipboard and the search for it via Google. (By the way I am surprised that a software for this simple and useful task does not already exist. At least I was not able to finde something like this - if there is, please simple tell me.)

So I wrote the following script:

#include <Clipboard.au3>
Send("^c")
ShellExecute("https://www.google.at/search?q=" & _ClipBoard_GetData())

Then, I copied a link to the start menu to allocate a windows shortcut on it. So all I have to do if I want to google a word in any program is to highlight it and press the shortcut. Fine.

Unfortunately, it turned out that it is not that easy, as the actual window looses its focus when the script gets executed and thus the highlighted word can not be copied. Ok, so I thought I simply add a Send("!{TAB}") before the Send("^c") so the last active window gets its focus again. Nooope - does not work. So I googled for this problem and did not really find a solution for it. Anybody has a recommendation for me to solve this problem? Either a working command to reset focus to the last active window, or a way that the script does not steal focus from the active window.

Thanks!

Edited by BlueRabbit
Link to comment
Share on other sites

Well there are a few ways to do this, that is what is so fun about scripting is you get to play around with it and find the way that you like best.

For your copy issue, the solution is HotKeySet() so I would try maybe something like this:

HotKeySet("!^c", "GoogleSearch") ;Set Ctrl+Alt+C to launch the function

Global $sOldClip ;Declare the varible(s) for clipboard

While 1 ;Keep the script open and running in the background
Sleep(10)
WEnd

Func GoogleSearch() ;Our Function
$sOldClip = ClipGet() ;Save Current Clipboard
Send("^c") ;Copy Selected Text to Clipboard
ShellExecute("https://www.google.at/search?q=" & ClipGet()) ;Navigate to search
ClipPut($sOldClip) ;Restore Old Clipboard
EndFunc

 

Link to comment
Share on other sites

Oh, I see. Your way the script would have to run in the background all the time. Well, if there is a way to avoid this, I would prefer it.

I wanted to run the script simply each time the (windows based) shortcut is pressed (which already works), not having the script running in the background all the time. Or are there any advantages, having the script running in the background all the time?

Link to comment
Share on other sites

Lots of advantages to having it running... like the script is running :)

I think what you should be more concerned with is if there is any real "disadvantage" to leaving it running, and in that case I would say no.

We have the Sleep(10) in the loop to prevent the script from taking up pretty much any cpu resources, and we set our hotkey combination to a key set that does not interact/interrupt normal workflow. 

If you wanted to run the script every time, most likely you would lose some of the inherent benefits I have in the example above.  Please explain more how your running the script from a hotkey (I assume just a hotkey assigned to a shortcut on the desktop?)

Link to comment
Share on other sites

Are you saying that when you hit your shortcut key, the window that you are currently on becomes inactive?

If so, what window becomes active?

Yeah, the active window becomes inactive if the script gets executes. Dont know what exactly is active after the execution of the script, maybe nothing? However, ALT+TAB brings the last window back active. I dont understand why the Send("!{TAB}") does not work equally.

 

I think what you should be more concerned with is if there is any real "disadvantage" to leaving it running, and in that case I would say no.

Well, I think a disadvantage is any running process may crash for any reason. And I really try to have as little processes running as possible - also if that small. But if there is no other option, I of course will go your way.

 

Please explain more how your running the script from a hotkey (I assume just a hotkey assigned to a shortcut on the desktop?)

Yes. But the shortcut is not on the desktop but in the start menu. You can also assign hotkeys to shortcuts there. (Sorry I confused the word "shortcut" with "hotkey" in my opening post.)

Edited by BlueRabbit
Link to comment
Share on other sites

Ok, so you lost focus to your .lnk file/desktop/start menu etc.

Your going to need to call back focus to your browser.  Since your browser is not active when the script is open you can't save the current active window or anything to recall it so you would need to manually look for it.  This part will require you to look up what your window is called.  Since your window will not always have the same title based on where your browsing, you need to do a partial match with the common parts.  I am using Chrome right now so an example would be.  I can use "Google Chrome" from the title and hope nothing else with that string is on my computer, or I can use the Class "Chrome_WidgetWin_1"

Use the built in Window Info Tool that comes with Autoit to find this information.

Opt("WinTitleMatchMode", 2) ;Set Title Match To "Any Part of String"
WinActivate("Google Chrome", "")

$sOldClip = ClipGet() ;Save Current Clipboard
Send("^c") ;Copy Selected Text to Clipboard
ShellExecute("https://www.google.at/search?q=" & ClipGet()) ;Navigate to search
ClipPut($sOldClip) ;Restore Old Clipboard

 

Link to comment
Share on other sites

... All I want my script to do, is to copy the actual highlighted text (e.g. in a browser or in a pdf or anywhere) into the clipboard and the search for it via Google. ...

 

There are existing solutions if i wanted to do that in the browser only. I want the script to work in every program that displays text, just as the clipboard actually does too.

Once again the question: Why does Send("!{TAB}") not work? (Basically, this guy seems to have the same problem like me: https://www.autoitscript.com/forum/topic/146030-windows-8-alttab-emulation/)

Edited by BlueRabbit
Link to comment
Share on other sites

Well if alt+tab is truly how you want to do this, all you should have to do is this:

Send("!{TAB}")

It works for me, however if it does not work for you, try this:

send("{altdown}{tab}{altup}")

If its still to fast, add sleep between the different parts or slow down sendkey speed via Opt()

Edited by ViciousXUSMC
Link to comment
Share on other sites

Well if alt+tab is truly how you want to do this, all you should have to do is this:

Send("!{TAB}")

It works for me, however if it does not work for you, try this:

Do you have win8? Dont think so as I just found a reasonable explanation why this is not working any more in win 8: http://www.autohotkey.com/board/topic/84771-alttab-mapping-isnt-working-anymore-in-windows-8/

A number of security changes have been implemented in Windows 8 which prevent programs which are not Metro apps from injecting keys that would remove you from the Metro environment. The way this works is that the keyboard shortcut is ignored if it doesn't come from a physical keyboard or a program that has the appropriate permissions. [...] In order to have the proper permissions, the program must be built with UIAccess (see http://msdn.microsof...y/ms742884.aspx).

I am currently working on a workaround and I think I will soon be successful :)

Edited by BlueRabbit
Link to comment
Share on other sites

I do have Win 7, Win 8.1, and Win 10 computers at my desk.  

Looks like your on the right track, its not working on my 8.1 computer.

Best Solution?  Use Alt+Escape :)

Send("!{ESC}")

 

Nice! :)

I just got a working workaround:

ShellExecute("C:\Users\UpdatusUser\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\Window Switcher.lnk")
Sleep(1)
Send("{ENTER}")

This one works finde, but

Send("!{ESC}")

is definately more elegant.

Thanks boys! :)

Edited by BlueRabbit
Link to comment
Share on other sites

As some friends of mine already asked me to publish this when I got it working. Here is the working code:

Send("!{ESC}")
Sleep(1)
$sOldClip = ClipGet()
Send("^c")
Sleep(1)
ShellExecute("https://www.google.at/search?q=" & ClipGet())
ClipPut($sOldClip)

Install instructions:

1. Install AutoIt if not already done
2. Create new file "Google Hotkey.au3" containing the code above on any place you want to store the script.
3. Create a shortcut to the just created script.
4. Create a new folder in your start menu. (E.g. C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\AutoIt Hotkeys)
5. Move the shortcut into this new start menu folder.
6. Now you can assign a desired hotkey to the shortcut by right clicking on the shortcut file (register "Shortcut", field "Shurtcut key"). I simply used F9.
7. That's it. Just highlight any word in any program and google for it super quickly. Speed up your workflow! :)

 

Notes:
.) You may change google.at to another top level domain, dependent on your country.
.) If it is not working on your machine, try to raise the sleep values.
.) You may use this script for any other search on another hotkey, like dict.cc on F10, amazon on F11 etc.
 

Special thanks to ViciousXUSMC.

Edited by BlueRabbit
Link to comment
Share on other sites

My suggestions

Add in the restore current clipboard code like I have shown above, nothing more frustrating that having something copied you need/want and losing it on accident because you ran a script.

Compile your script to an .exe so that if your friends want it they can run it without needing to install Autoit, better yet Compile an installer for it so that it puts the shortcut, the .exe and everything in its place, and can uninstall it later on if desired.

You can use a .ini file and some simple code to read the "search" location so that it can be changed by the user without needing to recompile the .exe and let them customize it upon install. 

Use ClipGet() instead of _ClipBoard_GetData() its native to Autoit and will perform the same for your needs. 

Especially if distributing this to others, I would go the route we showed earlier with the script open in the background and a While Loop, this way users do not need to worry about shortcuts, and placing files around or any of that.  Add another hotkey to exit the script at any time.  Then you can just give them the .exe and they run it and it works, nothing else required. 

Regards,

Edited by ViciousXUSMC
Link to comment
Share on other sites

I have added the restore old clipboard feature and edited the clipget function to your suggestions, which of course is smarter. I edited the code above.

Unfortunately, I have no time any more for reading in on how to create a installation script and a proper ini file. I think thats enough so far. I am very happy with my new tool.

Thanks again!

Edited by BlueRabbit
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...