n00b AppleScript question....please help

I am trying to write a script that will search wikipedia for a section of highlighted text. I am having two issues.

  1. When running the Script the 1st time it appears to work fine. However if I run it a second time it searches the previously highlighted term and the new highlighted term together which is not what I want.
    ie highlight “tree” it will search tree then highlight “leg” it will search “treeleg”
  2. Is it possible to assign a shortcut to this script if I am running it in firefox. Or maybe a option in the secondary click menu?

Any Help would be appreciated, thanks it advance.



tell application "Finder" to activate

set the clipboard to " "




tell application "Firefox" to activate
tell application "System Events"
	tell process "Firefox"
		tell menu bar 1
			set the clipboard to " "
			click menu item "Copy" of menu "Edit"
		end tell
	end tell
end tell

set theSelectedText to the clipboard
set searchText to " "
set searchText to theSelectedText
open location ("http://en.wikipedia.org/wiki/" & searchText)
set newText to switchText of searchText from " " to "_"
to switchText of currentText from SearchString to ReplaceString
	set storedDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to SearchString
	set currentText to currentText's text items
	set AppleScript's text item delimiters to ReplaceString
	set currentText to currentText as Unicode text
	set AppleScript's text item delimiters to storedDelimiters
	currentText
	set searchText to currentText
end switchText



property search_URL : "http://en.wikipedia.org/wiki/"

set the search_URL to (search_URL & newText)

open location search_URL

Model: MacBook Pro
AppleScript: 2.2
Browser: Firefox 2.0.0.14
Operating System: Mac OS X (10.5)

Hi,

here a shorter version, the perl script does the URL escaping


set the clipboard to " "
activate application "Firefox"
tell application "System Events"
	tell menu bar 1 of process "firefox-bin" -- or "Firefox"
		click menu item "Copy" of menu "Edit"
	end tell
end tell
open location "http://en.wikipedia.org/wiki/" & (do shell script "perl -e 'use URI::Escape; print uri_escape(\"" & (the clipboard) & "\")';")

To assign a shortcut to the script you need a tool like Butler, QuicKeys, FastScripts, QuickSilver.

Alaskan:

Here’s a little something I whipped up for someone a while ago. It doesn’t do the copying so it’s just my take on calling the search.

set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set clp to every text item of (the clipboard)
set AppleScript's text item delimiters to "+"
set clp to clp as Unicode text
set AppleScript's text item delimiters to oldTID
open location ("http://en.wikipedia.org/wiki/Special:Search?search=" & clp & "&go=Go")

I used to run this from the scripts menu in the menu bar (not FastScripts (http://www.red-sweater.com/fastscripts/) though that would work as well as what Stefan suggested).

Enjoy!

Jim Neumann
BLUEFROG

Thanks you guys both suggestions work great!