Yet another newbie here. But a reverent one at that.
I need to be able to add quotation marks before and after the selected words that will be searched in Google when using the “search in Google” contextual menu. I use Google a lot while writing to validate literal expressions I come up with by seeing examples of usage on various web pages. Sometimes, I just need to see the bare number of results and thus see if an expression is common or not. Currently, I need to add the quotation marks manually every time I use the function. That means up to maybe 50 times a day.
English being my second language, this is a practical way for me to know if I’m making sense.
Ideally, “Search in Google” would remain as is. Another contextual menu would appear just below and say “Search Google (exact)” or something equivalent.
Obviously, all this if “Search in Google” is a script, or if it is feasible to add a contextual menu item that would appear in the same contexts.
The following script (put in ~/Library/Scripts) will show up in your script menu. It constructs a Google search from whatever text is in the clipboard.
set tSearch to the clipboard
open location ("http://www.google.com/search?q=%22" & (my Make_Search_String(tSearch)) & "%22&ie=UTF-8&oe=UTF-8")
to Make_Search_String(tText)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set tText to (text items of tText)
set AppleScript's text item delimiters to "+"
set tText to tText as string
set AppleScript's text item delimiters to space
set tText to (text items of tText)
set AppleScript's text item delimiters to "+"
set tText to tText as string
set AppleScript's text item delimiters to astid
return tText
end Make_Search_String
That works perfectly. Is it possible to skip the copy to clipboard step and have the script use the selected text directly instead? It would be Nirvana.
I’ve searched in the AppleScript Language Guide but I can’t find anything that refers to a text selection, not literally that is. My apologies if I missed the obvious.
This doesn’t avoid the clipboard, but prepended to Adam’s script, may do what you want:
tell application "System Events"
set process_name to name of first process whose frontmost is true
end tell
delay 0.3
tell application "System Events"
tell application process_name
keystroke "c" using command down
end tell
end tell
tell me to activate
delay 0.3
(I’m not sure the ‘tell me’ line is necessary, but some app needs to be active to make use of the clipboard.)
It doesn’t seem to work, though. It goes all the way to Google, but beeps at some point and puts whatever string is already in the clipboard in the search field. So, it doesn’t copy the selected text.
The application from which the script is called doesn’t seem to make a difference.
Wow, I barely understand what you guys are talking about. Haha 3 days will get you far in programming.
Anyway,
I would think that the problem is that what you want to copy to the clipboard isn’t being selected.
I don’t know how to fix that, because I am VERRY new, but I think that’s the problem.
You might try putting:
tell application "System Events"
tell application process_name
keystroke "a" using command down
end tell
end tell
that right before
"
tell application “System Events”
tell application process_name
keystroke “c” using command down
end tell
end tell
"
in your script.
Actually, no, you wouldn’t likely want to select all prior to running the script…
The following is working fine (for me) on bits of selected text… run either from Script Editor or called from FastScripts. (I think It will fail as a standalone app).
You may have to fiddle a bit with the delays.
Peter B.
tell application "System Events"
set process_name to name of first process whose frontmost is true
end tell
delay 0.3
tell application "System Events"
tell application process_name
keystroke "c" using command down
end tell
end tell
tell me to activate
delay 0.3
-- Following courtesy of Adam Bell
set tSearch to the clipboard
open location ("http://www.google.com/search?q=%22" & (my Make_Search_String(tSearch)) & "%22&ie=UTF-8&oe=UTF-8")
to Make_Search_String(tText)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set tText to (text items of tText)
set AppleScript's text item delimiters to "+"
set tText to tText as string
set AppleScript's text item delimiters to space
set tText to (text items of tText)
set AppleScript's text item delimiters to "+"
set tText to tText as string
set AppleScript's text item delimiters to astid
return tText
end Make_Search_String
For a reason beyond me, the copy to the clipboard doesn’t happen. The script beeps and the string that was in the clipboard before the call is what is put in the Google search field. If the clipboard is empty, a piece of the script itself is pasted in the search field.
I’ve tested it with Numbers, NovaMind, Safari, TextEdit and Preview so far. This said, Adam’s part works fine and I’m already using it intensively. I could spare the copy and the travel to the scripts menu, but it is much better than adding quotation maks in Google’s search field (positionning the cursor, typing, repositioning the cursor, typing) which is prone to frequent errors on my part.
It appears you’re using the script from Apple’s standard script menu… (which I just tried, and duplicated your reported result). I’m so used to the convenience of being able to call scripts from the keyboard rather than going to the menu, I tend to forget other people don’t use similar keyboard utilities.
There are a number of them available; I happen to use FastScripts and can recommend it.
I don’t know why the script behavior is actually different between Apple’s script menu and FastScript’s own menu, but having the option to call a script (such as the one in question) from a key combo is, as you might put it ‘Nirvana’ compared to the alternative.
You’re right. FastScripts does it. A hundred runs would happen in a few days, so I bought it right away. The sad thing is that the script works fine when called from the FastScripts menu, but it doesn’t form a keyboard shortcut. It’s back to the beep and what was in the clipboard before. It’s ironic that using a keyboard shortcut prevents the script from executing command-c. Nirvana can wait, I guess.
FastScripts has preferences which may be set from the menu bar icon. The first item in the menu - ‘FastScripts’ - has a submenu including ‘Preferences’ where key board commands are set.
Again, the delays in the script may have to be adjusted for proper operation… try delay 1 and work back towards the lower settings.
I wasn’t clear, sorry. I assigned the script a shortcut in FastScripts, but that doesn’t work. When the script is called with the FastScripts shortcut, the copy to clipboard part of the script is ignored or creates the error that beeps, and the search field in Google is filled with the text that was in the clipboard before the call, not with the currently selected text.
I have to say that I now have a routine that uses two left-hand keyboard shortcuts in sequence with the command key in both (copy and run the script). That’s pretty fast. This is a huge improvement over my pre-applescript routine.
Okay… if you’re content with that, I should leave well enough alone… but I’ll emphaszize that the delays between script segments can be critical to proper script operation.
For what it’s worth… the code below worked for me. Save as File Format: application, leaving the Options unchecked. You can launch the resulting script app from the Scripts Menu, or you can add it to the Dock and run it from there – simply select some desired text and then click on the docked script icon.
tell application "System Events"
keystroke tab using command down
keystroke "c" using command down
end tell
delay 0.5
-- followed by Adam Bell's script:
set tSearch to the clipboard
open location ("http://www.google.com/search?q=%22" & (my Make_Search_String(tSearch)) & "%22&ie=UTF-8&oe=UTF-8")
to Make_Search_String(tText)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set tText to (text items of tText)
set AppleScript's text item delimiters to "+"
set tText to tText as string
set AppleScript's text item delimiters to space
set tText to (text items of tText)
set AppleScript's text item delimiters to "+"
set tText to tText as string
set AppleScript's text item delimiters to astid
return tText
end Make_Search_String