How to escape an ampersand in a search?

I made a little script app that’s triggered from a keyboard key when iTunes is running.
Especially with tracks that are on a compilation, I like to manually search for the actual original release years instead of having the compilation year as the tag, so I do this quite a lot.
Here’s the script:


tell application "iTunes"
	if player state is playing then
		set theArtist to (get artist of current track)
		set theSong to (get name of current track)
	end if
end tell

set searchphrase to (theArtist & " " & theSong)

tell application "Safari"
	activate
	open location "https://www.google.com/search?q=" & searchphrase
end tell

It works great most of the time, but if the artist or song name contains “&” which is not completely uncommon, the search phrase gets clipped there and nothing after the ampersand gets included in the search phrase. For example, if it’s in the artist name already, then even the song name will get omitted completely.
What’s the best way to prevent this clipping and escape the ampersand altogether? I don’t think the ampersand character is necessary for search, if you already have the words gathered from the artist + song names.

Hi.

It’s “%26” instead of the ampersand in the search phrase. I’d imagine that spaces and punctuation would need to be escaped too. Maybe like this (untested):

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

tell application "iTunes"
	if player state is playing then
		set theArtist to (get artist of current track)
		set theSong to (get name of current track)
	end if
end tell

set searchphrase to (theArtist & " " & theSong)

-- Percent-escape anything in the search phrase which isn't a letter or a digit.
set searchphrase to current application's class "NSString"'s stringWithString:(searchphrase)
set escapedSearchphrase to (searchphrase's stringByAddingPercentEncodingWithAllowedCharacters:(current application's class "NSCharacterSet"'s alphanumericCharacterSet())) as text

tell application "Safari"
	activate
	open location "https://www.google.com/search?q=" & escapedSearchphrase
end tell

Thanks, your version works, that does the trick!
I had no problem with other characters, only the ampersand. Like so:

These are all valid text that get passed onto the search phrase correctly:
Catfish
Cat fish
Cat-fish
Cat’fish
Cat,fish
Cat.fish

This text gets cut short from the ampersand onwards:
Cat & fish

And it’s fixed now, thanks!

The problem is that Safari has an open location command which overrides the standard addition open location command.

-- use system GURLGURL event. URL is invalid and does nothing until you replace the space with "%32"
open location "https://www.google.com/search?q=hello world"

-- Use safari's own open location and the entire URL is encoded according to the RFC documents and reserved characters are not escaped. 
tell application "Safari"
	activate
	open location "https://www.google.com/search?q=hello world"
end tell

The standard addition command is the GURLGURL AppleEvents used by the system like revealing a file in the finder or open command on the command line. It requires and URL encoded string in order to work and will open the webpage in the default browser.

Safari applies the standard percent escaping (according to RFC documents) over the entire URL. That means that reserved characters like ampersands are not escaped (Reserved characters are “!*'();:@&=+$,/?#[]” to be exactly). You can also simply search and replace those reserved characters using text item delimiters because the reserved characters is constant and limited or use Nigel’s non-standardized approach.

For the record: The URL encoder in AST works according to the RFC standard thus like Safari.