6

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
pfych
  • 850
  • 1
  • 13
  • 32

8 Answers8

6

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
3

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.

dj bazzie wazzie
  • 3,472
  • 18
  • 23
3

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.

BlueM
  • 3,658
  • 21
  • 34
3

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

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
ddelange
  • 1,037
  • 10
  • 24
2

If you need to get the URL as a string (not just feed it into open which does a nifty job of encoding for you) and you're not above using a little Automator, you can throw some JavaScript into your AppleScript:

enter image description here enter image description here enter image description here

encodeURIComponent is a built in JavaScript function - it is a complete solution for encoding components of URIs.

For copy/pasters, here are all three scripts in the above Automator chain:

on run {input, parameters}
    return text returned of (display dialog "Enter song you wish to find" default answer "" buttons {"Search", "Cancel"} default button 1)
end run
function run(input, parameters) {
    return encodeURIComponent(input);
}
on run {input, parameters}
    display dialog "http://www.mp3juices.com/search/" & input buttons {"okay!"} default button 1
end run
Sandy Gifford
  • 7,219
  • 3
  • 35
  • 65
  • automator application has one downside that I can't get around. It accept folder and files. If say I want input to be the url invoke when `openLocation` get called, there isn't afaik to catpure that input. As much as I love to pipe applescript with javascript, I have to still to either one to accept the input url. – ychz Jun 19 '23 at 22:18
  • @ychz I since the dialog is opened by the first AppleScript, wouldn't that be a limitation of that and not Automator? – Sandy Gifford Jun 20 '23 at 20:50
  • This is what I'm talking about. https://imgur.com/BYw34Kj Automator application force input to be folder and files, which you can see the top banner, while applescript doesn't have this limitation on its won. I would love have automator application work in this case. – ychz Jun 21 '23 at 18:52
0

I was hunting around for URL encoding and decoding and came across this helpful link.

Which you can use like so:

set theurl to "https://twitter.com/zackshapiro?format=json"
do shell script "php -r 'echo urlencode(\"" & theurl & "\");'"
# gives me "https%3A%2F%2Ftwitter.com%2Fzackshapiro%3Fformat%3Djson"

set theurl to "https%3A%2F%2Ftwitter.com%2Fzackshapiro%3Fformat%3Djson"
return do shell script "php -r 'echo urldecode(\"" & theurl & "\");'"
# gives me "https://twitter.com/zackshapiro?format=json"

Or as functions:

on encode(str)
    do shell script "php -r 'echo urlencode(\"" & str & "\");'"
end encode

on decode(str)
    do shell script "php -r 'echo urldecode(\"" & str & "\");'"
end decode
Zack Shapiro
  • 6,648
  • 17
  • 83
  • 151
0

Just so it's said, AppleScriptObjC allows us to use NSString to do the encoding. The script is complicated by the fact that different parts of the URL allow different characters (all of which I've added options for) but in most cases the 'query' option will be used.

See NSCharacterSet's dev page (the section called "Getting Character Sets for URL Encoding") for descriptions of the various URL parts.

use AppleScript version "2.4" -- Yosemite 10.10 or later
use framework "Foundation"

property NSString : class "NSString"
property NSCharacterSet : class "NSCharacterSet"

-- example usage
my percentEncode:"some text" ofType:"query"

on percentEncode:someText ofType:encodeType
    set unencodedString to NSString's stringWithString:someText
    set allowedCharSet to my charSetForEncodeType:encodeType
    set encodedString to unencodedString's stringByAddingPercentEncodingWithAllowedCharacters:allowedCharSet
    return encodedString as text
end percentEncode:ofType:

on charSetForEncodeType:encodeType
    if encodeType is "path" then
        return NSCharacterSet's URLPathAllowedCharacterSet()
    else if encodeType is "query" then
        return NSCharacterSet's URLQueryAllowedCharacterSet()
    else if encodeType is "fragment" then
        return NSCharacterSet's URLFragmentAllowedCharacterSet()
    else if encodeType is "host" then
        return NSCharacterSet's URLHostAllowedCharacterSet()
    else if encodeType is "user" then
        return NSCharacterSet's URLUserAllowedCharacterSet()
    else if encodeType is "password" then
        return NSCharacterSet's URLPasswordAllowedCharacterSet()
    else
        return missing value
    end if
end charSetForEncodeType:
Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17
0

The Python Approach:

  1. Find your python3 path (which python3) or if you don't have it, install using brew or miniconda
  2. Now try this:

python_path = /path/to/python3

set search_query to "testy test"

tell application "Google Chrome"
    set win to make new window
    open location "https://www.google.com/search?q=" & url_encode(q)
end tell

on url_encode(input)
    return (do shell script "echo " & input & " | " & python_path & " -c \"import urllib.parse, sys; print(urllib.parse.quote(sys.stdin.read()))\"
")
end url_encode

credits to @Murphy https://stackoverflow.com/a/56321886

Matt Groth
  • 470
  • 4
  • 20