Append selected text to one of the specified URLs, dependent on the first two letters of user's selected text

I would like to set-up a single BTT button to open a URL in a new browser tab, adding selected text to the end of the URL, however the base URL used will be different depending on the first two letters of the user's selected text. Examples below:

  1. "selected text = AA111" opens new tab in browser to "url_for_website1/search?q=AA111"
  2. "selected text = BB222" opens new tab in browser to "url_for_website2/search?q=BB222"
  3. "selected text = CC333" opens new tab in browser to "url_for_website3/search?q=CC333"

Below is Apple script that seems to work in an Automator service. Can anyone guide me as to how I could port this to a BTT button?

Many thanks in advance for any help from the community.

on run {input, parameters}
	-- Identify first two letters (example, AA / BB / CC) to match to desired base URL, then append the selected text to the end of the URL:

	if (input as string) begins with "AA" then
		open location "https://www.google.com/search?q=" & input
		return
	end if	
	
	if (input as string) begins with "BB" then
		open location "https://www.bing.com/search?q=" & input
		return
	end if
	
	if (input as string) begins with "CC" then
		open location "https://duckduckgo.com/?q=" & input
		return
	end if
	
end run

Add the Action "Save selected text in "selected_text" variable" and than run this script:

tell application "BetterTouchTool"
	set theText to get_string_variable "selected_text"
end tell

if theText begins with "AA" then
	open location "https://www.google.com/search?q=" & theText
	return
end if

if theText begins with "BB" then
	open location "https://www.bing.com/search?q=" & theText
	return
end if

if theText begins with "CC" then
	open location "https://duckduckgo.com/?q=" & theText
	return
end if
1 Like

Thanks Dirk, that works perfectly!