In the past I have resorted to turning a list into a comma delimited string to pass to a shell script. This time I have a list of records. Is it possible to pass a list (or list of records) via do shell script? Can’t seem to find anything about this.
set sampleListOfRecords to {{name:"ralph,serial:“123”},{name:"Rob,serial:“345”}}
do shell script ("test.sh "& sampleListOfRecords)
##in this case an expect script
#!/usr/bin/expect
set file_record_List [lindex $argv 0]
puts "list of records = $file_record_List"
exit
When run it throws error saying “can’t make sampleListOfRecords into unicode text” Perhaps it is just telling me to do it the old way. Actually I couldn’t make a list of records into a string really so it would have to be parallel lists which I’m not keen on.
But only do that if it doesn’t matter which value’s which.
¢ Record properties are identified by label. They’re not ordered.
¢ List items are identified by order. They don’t have labels.
¢ Simply coercing a record to list doesn’t guarantee a particular order in the list. If it matters which item’s which, you should script the individual transfers of the values from the record to the list in the order you want.
But what’s the point of passing a record or a list to a shell script? The only shell script which can understand them is ‘osascript’. For its benefit, you could write the record to a temporary file and have osascript read it from the file (and act upon it, of course).
set r to {a:1, b:2, c:3, d:"Fred"}
set accessRef to (open for access file ((path to temporary items as text) & "AS record.dat") with write permission)
try
set eof accessRef to 0
write r to accessRef
on error msg
display dialog msg buttons {"OK"} default button 1
end try
close access accessRef
do shell script "osascript -e 'set r to (read file ((path to temporary items as text) & \"AS record.dat\") from 1 as record)' -s s"
If you’re sure the items in the list doesn’t need to be quoted form or the items are already in quoted form your can use something like this.
set args to {}
set end of args to "Hello"
set end of args to "World!"
set end of args to quoted form of "
Goodbye"
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set r to do shell script "echo " & args
set AppleScript's text item delimiters to oldTID
return r
The list is coerced into text because the object left of the ampersand is an string object. AppleScript will try to coerce the object right from the ampersand into the same object type as on the left side and concatenate them. If it fails it will turn into an list.