[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 "]
" 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 "]
", because otherwise the API doesn't recognise it.
I have tried escaping the "]" 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) [Bonus Track]
"
The whitespaces get replaced with " " by my AppleScript handler.
The left [
stays as "[" (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 "]" 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("[", "[", trackreplace)
end if
if trackreplace contains "]" then
set trackeplace to replaceChars("]", "]", 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
CodePudding user response:
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