0

[EDIT] Solved. The actual issue was a typo in one of the handler variables, and not a zsh issue, as I initially thought.

I am trying to prevent zsh from replacing the code "%5D" for the ] right square bracket with the actual character "]".

For my purposes - calling the last.fm API via curl in shell script, to get info about a track from Apple Music - it needs to stay as "%5D", because otherwise the API doesn't recognise it. I have tried escaping the "%5D" with backslashes or a second "%" but that doesn't work, it still gets replaced with "]"

Here's an example of what I'm trying to do and what is not working:

The original string of the track's name: "YESTODAY (Extended Version) [Bonus Track]"

How it needs to be spelled for the API to work: "YESTODAY+(Extended+Version)+%5BBonus+Track%5D"

The whitespaces get replaced with "+" by my AppleScript handler. The left [ stays as "%5B" (I guess because it is immediately followed by more letters and therefore zsh cannot recognise it as code and replace it). The right ] is generally the last character of the track string, not followed by anything and thus "%5D" is recognised by zsh and written as "]" .

How do I fix this? Any help is greatly appreciated :)

For reference: The part of the AppleScript that is supposed to replace the "[ ]" with code. (This is obviously not the complete script). If you run this, it replaces the [ ] correctly. The issue only arises once the entire API call is made with do shell script curl_command because of zsh interpreting the code.

 set trackreplace to "YESTODAY (Extended Version) [Bonus Track]"

 if trackreplace contains "[" then
        set trackreplace to replaceChars("[", "%5B", trackreplace)
    end if
    
    if trackreplace contains "]" then
        set trackeplace to replaceChars("]", "%5D", trackreplace)
    end if
    
    on replaceChars(find, replace, subject)
        set savedelims to AppleScript's text item delimiters
        set AppleScript's text item delimiters to find
        set subject to text items of subject
        set AppleScript's text item delimiters to replace
        set subject to (subject as string)
        set AppleScript's text item delimiters to savedelims
        return subject
    end replaceChars
Samu
  • 1
  • 3
  • Using the `urlencode` function from [this post](https://stackoverflow.com/questions/23852182/i-need-to-url-encode-a-string-in-applescript) instead of `replaceChars` solves the issue – danielhoherd Jun 08 '22 at 20:51
  • This has nothing to do with zsh, it's because you have a typo in the second assignment -- it should set `trackreplace` instead of `trackeplace`. – Gordon Davisson Jun 08 '22 at 22:23
  • @GordonDavisson Thank you so much!! This solved it! I'm kinda embarrassed the issue was a typo haha.. This is my first time coding anything, and I spent a solid 2 hrs googling and trying to solve this, thinking it was a zsh issue! Anyway, it works now! Thx again :) – Samu Jun 09 '22 at 09:13
  • @Samu I didn't spot it for a while either. I ran your code exactly as above and got the expected result (because `set trackeplace` is the last command, that result is shown in Script Editor), but as soon as I tried to do anything with `trackreplace` -- *anything at all* -- I got the version with "]", so that's what made me look closer. – Gordon Davisson Jun 09 '22 at 17:33

1 Answers1

0

It was not a zsh issue but actually just a typo in one of the handler variables (trackeplace instead of trackreplace), as pointed out by @GordonDavisson

Samu
  • 1
  • 3