0

I'm splitting a string using osascript (was working this way, not with bash), and assigning the resulting array to a bash variable, and continuing with my bash script. I'm doing it like so:

tempArrayApplications=$(osascript >/dev/null <<EOF
    set oldDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "/"
    set theArray to every text item of "$noSplitString"
    set AppleScript's text item delimiters to oldDelimiters
    return theArray
EOF)

However, the command line returns the error that it went to the end of the file without finding the matching ')'. However, when I don't assign a bash variable to the osascript output, everything works fine, so I know it's not a problem with the AppleScript section. I ran shellcheck, and it doesn't detect any errors, and the other solutions seem to be related to an unclosed quote or unescaped character, but I don't seem to have that problem. Clearly it's due to trying to assign it to a bash variable, but for the life of me I don't know what I'm doing wrong. Thanks for the help.

javarookie
  • 49
  • 7

1 Answers1

0

Have you paused to consider that you're taking a bash variable ($noSplitString); inserting this into an AppleScript that splits the text using / as the delimiter; executing this AppleScript inside a bash command (osascript); then storing its output (which actually gets destroyed) in another bash variable ($tempArrayApplications)...?

My inclination would be to remove the AppleScript altogether (3 out of its 5 lines are redundant, anyway), and create the array from the string within bash.

So, given this:

noSplitString="item 1/item 2/item 3"

Then simply do this:

IFS='/'
tempArrayApplications=($noSplitString)

Now $tempArrayApplications will be an array with three items, starting at index 0 and ending at index 2. You can echo a specific element in the array like this:

echo "${tempArrayApplications[1]}" # "item 2"

IFS is the bash equivalent of the AppleScript text item delimiters. It typically has a default value of ⎵\t\n (where indicates a space character). More can be read about it in this article: Bash IFS: its Definition, Viewing it and Modifying it

CJK
  • 5,732
  • 1
  • 8
  • 26
  • Will accept this as the answer. I'm more familiar with Applescript, so when I can fall back to it I tend to do so, didn't realize it was this simple in bash though. – javarookie Jul 27 '18 at 16:33