Reputation: 601
This is just driving me nuts. I am trying to read a file in bash, remove duplicates, sort, and then display a "list choice" window via applescript.
My $DATALOG file is formatted like this:
field1 field2
field1 field3
field1 field4
etc...
Applescript=awk '{print $2}' $DATALOG | awk ' !x[$0]++' | sort -u | tr "_" " "| sed 's/^/\"/' | sed 's/$/\"/' | tr "\n" "," | sed 's/.$//'
Now, that line works GREAT. in $Applescript, I get an output like this:
"field 2","field 3", "field 4"
Which is exactly waht I want.
Now, I take that output, and add the backslash before the quotes, and the applescript parts.
Applescript=`echo "tell application \"System Events\" to return (choose from list {$Applescript})"| sed 's/\"/\\\"/g'`
And this gets me exactly what I want:
tell application \"System Events\" to return (choose from list {\"field 2\",\"field 3\",\"field 4\"})
Now, I try the osascript command:
osascript -e $Applescript
And I get an error:
4:4: syntax error: Expected expression but found end of script. (-2741)
So, I add quotes:
osascript -e "$Applescript"
And I get an error:
17:18: syntax error: Expected expression, property or key form, etc. but found unknown token. (-2741)
I can't tell what the hell's going on here, so I decide to COPY an echo of $Airport and try that as a variable.
Airport=
tell application \"System Events\" to return (choose from list {\"field 2\",\"field 3\",\"field 4\"})
AND THAT WORKS WITHOUT ANY MODIFICATION.
So....
I need to figure out how to do this without having to set my variables permanently.
Upvotes: 1
Views: 3582
Reputation: 85045
Don't try to make it more complicated than needed. Take advantage of the shell's two string quote characters to form one shell word as the value for the osascript -e argument:
Applescript=$(awk '{print $2}' $DATALOG | awk ' !x[$0]++' | sort -u | tr "_" " "| sed 's/^/\"/' | sed 's/$/\"/' | tr "\n" "," | sed 's/.$//')
osascript -e 'tell application "System Events" to return (choose from list {'"$Applescript"'})'
Also, it's a good idea to avoid the use of backticks to do command substitution; the $(command)
form is preferred because it is much easier to construct correct commands even when dealing with complex nestings.
Upvotes: 6