Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to URL-encode a string in AppleScript

My script searches a website for songs, but when there are spaces it doesn't search, you have to add underscores. I was wondering if there was a way to replace my spaces with underscores. Could you please use my current code below to show me how to do it?

set search to text returned of (display dialog "Enter song you wish to find" default answer "" buttons {"Search", "Cancel"} default button 1)
open location "http://www.mp3juices.com/search/" & search
end
like image 368
pfych Avatar asked May 25 '14 04:05

pfych


4 Answers

Note: The solution no longer works as of Big Sur (macOS 11) - it sounds like a bug; do tell us if you have more information.

Try the following:

set search to text returned of (display dialog "Enter song you wish to find" default answer "" buttons {"Search", "Cancel"} default button 1)
  do shell script "open 'http://www.mp3juices.com/search/'" & quoted form of search
end

What you need is URL encoding (i.e., encoding of a string for safe inclusion in a URL), which involves more than just replacing spaces. The open command-line utility, thankfully, performs this encoding for you, so you can just pass it the string directly; you need do shell script to invoke open, and quoted form of ensures that the string is passed through unmodified (to be URI-encoded by open later).

As you'll see, the kind of URL encoding open performs replaces spaces with %20, not underscores, but that should still work.

like image 162
mklement0 Avatar answered Nov 03 '22 00:11

mklement0


mklement0's answer is correct about url encoding but mp3juices uses RESTful URLs (clean URLs). RESTful URLs want's to keep the URL human readable and you won't see/use typical hex values in your url presenting an ASCII number. A snake_case, as you have mentioned (is false), but it is pretty common to use an substitution for whitespaces (%20) (and other characters) in RESTful URLs. However the slug of an RESTful must be converted to RESTful's own RESTful encoding before it can be handled by standard URL encoding.

set search to text returned of (display dialog "Enter song you wish to find" default answer "" buttons {"Search", "Cancel"} default button 1)
set search to stringReplace(search, space, "-")
do shell script "open 'http://www.mp3juices.com/search/'" & quoted form of search

on stringReplace(theText, searchString, replaceString)
    set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, searchString}
    set textItems to every text item of theText
    set AppleScript's text item delimiters to replaceString
    set newText to textItems as string
    set AppleScript's text item delimiters to oldTID
    return newText
end stringReplace

EDIT: updated the code, unlike the question mentioned that spaces are converted to underscores, mp3juice uses hyphens as substitution for whitespaces.

like image 42
dj bazzie wazzie Avatar answered Nov 02 '22 23:11

dj bazzie wazzie


An update on this, despite the fact that the answer is 3 years old, as I faced the same problem: on recent versions of macOS/OS X/Mac OS X (I think, 10.10 or later), you can use ASOC, the AppleScript/Objective-C bridge:

use framework "Foundation"

urlEncode("my search string with [{?@äöü or whatever characters")

on urlEncode(input)
    tell current application's NSString to set rawUrl to stringWithString_(input)
    set theEncodedURL to rawUrl's stringByAddingPercentEscapesUsingEncoding:4 -- 4 is NSUTF8StringEncoding
    return theEncodedURL as Unicode text
end urlEncode

It should be noted that stringByAddingPercentEscapesUsingEncoding is deprecated, but it will take some time until it’s removed from macOS.

like image 24
BlueM Avatar answered Nov 03 '22 00:11

BlueM


URL encoding in AppleScript

For a general use case (for me at the moment to pass any ASCII url containing chars like #, &, ß, ö to the bit.ly API), I stumbled upon a nice code snippet that instantly added full support to my ShortURL clipboard pasting shortcut. Here's a quote from source:

i was looking for a quick and dirty way to encode some data to pass to a url via POST or GET with applescript and Internet Explorer, there were a few OSAXen which have that ability, but i didn't feel like installing anything, so i wrote this thing (works with standard ascii characters, characters above ascii 127 may run into character set issues see: applescript for converting macroman to windows-1252 encoding)

Notes

  • Double encoding should be duly noted.
  • Not tested on non-ASCII URLs.
  • Tested on OS X 10.8.5.

Code

on urlencode(theText)
    set theTextEnc to ""
    repeat with eachChar in characters of theText
        set useChar to eachChar
        set eachCharNum to ASCII number of eachChar
        if eachCharNum = 32 then
            set useChar to "+"
        else if (eachCharNum ≠ 42) and (eachCharNum ≠ 95) and (eachCharNum < 45 or eachCharNum > 46) and (eachCharNum < 48 or eachCharNum > 57) and (eachCharNum < 65 or eachCharNum > 90) and (eachCharNum < 97 or eachCharNum > 122) then
            set firstDig to round (eachCharNum / 16) rounding down
            set secondDig to eachCharNum mod 16
            if firstDig > 9 then
                set aNum to firstDig + 55
                set firstDig to ASCII character aNum
            end if
            if secondDig > 9 then
                set aNum to secondDig + 55
                set secondDig to ASCII character aNum
            end if
            set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string
            set useChar to numHex
        end if
        set theTextEnc to theTextEnc & useChar as string
    end repeat
    return theTextEnc
end urlencode
like image 30
ddelange Avatar answered Nov 03 '22 01:11

ddelange