Script "search selected text in"

Sharing and discussing custom search templates for LaunchBar
Post Reply
Pertusa
Posts: 5
Joined: Tue Dec 23, 2008 12:43 pm
Contact:

Script "search selected text in"

Post by Pertusa » Tue Nov 03, 2009 4:35 pm

Is there a way (perhaps a script) to "search selected text in" (Google, or whatever other search template)?

Now, when I want to look up a word or sentence in Google (just an example), the process is:
1. select the text
2. copy it
3. call Launchbar
4. press G+space
5. paste the word/sentence
6. press enter

As a translator, I'm constantly copy-pasting bits of my English translations and looking them up in Google (with "quotation marks") in order to check if my sentence is a common one (the more hits, the smoother the construction. I think there should be a way to speed up the general search process , so that when a piece of text is selected in your computer (web browser, word document...) you can directly search it with your desired search template without having to go through all 6 previous steps.

Thanks

iRounak
Rank 4
Rank 4
Posts: 191
Joined: Fri Oct 23, 2009 7:11 pm
Contact:

Re: Script "search selected text in"

Post by iRounak » Tue Nov 03, 2009 5:24 pm

Code: Select all

tell application "LaunchBar" to hide
delay 0.2
tell application "System Events"
   delay 0.3
   keystroke "c" using command down
         delay 0.2
   set thetext to the clipboard
       set thetext to thetext as text
   set theurl to "http://www.google.com/search?q=" & quote & thetext & quote
   open location theurl
end tell
Last edited by iRounak on Tue Nov 03, 2009 5:32 pm, edited 1 time in total.

iRounak
Rank 4
Rank 4
Posts: 191
Joined: Fri Oct 23, 2009 7:11 pm
Contact:

Re: Script "search selected text in"

Post by iRounak » Tue Nov 03, 2009 5:31 pm

Alternatively,
1. Select the text
2. Hold the keys with which you activate LaunchBar
3. Press tab
4. Run this script

Code: Select all

on handle_string(thetext)
   tell application "LaunchBar" to hide
   set theurl to "http://www.google.com/search?q=" & quote & thetext & quote
   tell application "System Events"
      open location theurl
   end tell
end handle_string

norbert
Objective Development
Objective Development
Posts: 648
Joined: Thu Nov 09, 2006 6:30 pm

Re: Script "search selected text in"

Post by norbert » Tue Nov 03, 2009 11:18 pm

Writing scripts for this purpose isn't necessary. That's what search templates are designed for. Search templates have the advantage to handle URL encodings properly (some search engines still expect e.g. ISO Latin 1 encoded URLs, but AppleScript generates UTF-8 encoded URLs).

The question is, how can we use search templates most efficiently with text that's selected in an application.

Answer: Instant Send + Instant Open

  1. Invoke Instant Send to send the selected text to LaunchBar.
  2. Type an abbreviation of the desired search template and keep the last abbreviation character pressed, a.k.a. Instant Open.

I highly recommend to configure a Modifier Tap for Instant Send, for example a single tap on the Option key. This allows you transfer the currently selected text to LaunchBar just by tapping the Option key.

And to get most out of Instant Open, assign short, one letter abbreviations to your most frequently used search templates. For example, use G to select the Google Exact Phrase search template.

So instead of the 6 steps mentioned above, it's now just a matter of two keystrokes, tap the Option key, then press and hold G.

Pertusa
Posts: 5
Joined: Tue Dec 23, 2008 12:43 pm
Contact:

Re: Script "search selected text in"

Post by Pertusa » Wed Nov 04, 2009 1:36 am

Many thanks to iRounak and norbert for your uprompt and useful replies.
Here's the feedback after trying all 3 options:
1st option by iRounak: it works perfectly fine
2nd option by iRounak: I found this one a bit more complicated, though I'll use this option for other purposes that I hadn't thought before!
3rd option (norbert): after activating the 'instant send' feature (which I did not use before) and using the Google Exact Phrase search template, the result is very similar to the 1st option but I now I don't even have to call Launchbar (I set the Modifier tap for instant send as single option, and the Google Exact Phrase search template as º (in Spanish keyboards this is just at the left of number 1), and the repeating time to the lowest, so I can run a query in a matter of 2 strokes and half a second... cool!
Thanks again to both of you, Launchbar is really getting better and better
Alberto

iRounak
Rank 4
Rank 4
Posts: 191
Joined: Fri Oct 23, 2009 7:11 pm
Contact:

Re: Script "search selected text in"

Post by iRounak » Wed Nov 04, 2009 3:24 am

the result is very similar to the 1st option but I now I don't even have to call Launchbar (I set the Modifier tap for instant send as single option, and the Google Exact Phrase search template as º (in Spanish keyboards this is just at the left of number 1), and the repeating time to the lowest, so I can run a query in a matter of 2 strokes and half a second... cool!


That is exactly why triggers (current selection >> search assigned a hotkey or script1 directly assigned a hotkey) are important. Abbreviations are useful but not a replacement for triggers.
To Pertusa,
FYI: Instant send does not work for text selected in non-Cocoa applications like Firefox. So instead of assigning a modifier tap to "Instant Send' its better to assign it for opeining LaunchBar and then hold a single key abbreavition for running the script.

Pertusa
Posts: 5
Joined: Tue Dec 23, 2008 12:43 pm
Contact:

Re: Script "search selected text in"

Post by Pertusa » Wed Nov 04, 2009 11:02 am

Pertusa wrote:Instant send does not work for text selected in non-Cocoa applications like Firefox. So instead of assigning a modifier tap to "Instant Send' its better to assign it for opeining LaunchBar and then hold a single key abbreavition for running the script.


You're right, I hadn't realized it doesn't work in non-Cocoa apps.
I've now assigned the key 'z' to script1, so that after having selected a bit of text in any application I just have to press CMD+space with fingers #3-->#2(my shortcut to call LB) and then Z (finger #4), 3 strokes but pretty much as quickly as before.
Interestingly, I found this second way to be more reliable, since the feature 'send text to LB' somehow did not always work as expected (i.e., there was occasional delay and I sometimes had to press the option key twice (though it's configured to work with only one press).
Thanks again!

Post Reply