applescript pass list or list of records to shell script

Hi All,

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.

Thanks, rob

Hi robdut,

It’s telling you that you cannot coerce a record to string. That’s the first thing it’s doing, but you can coerce a record to list.

set the_record to {a:"abc", d:"def"}
the_record as list

gl,
kel

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"

Yes, for some reason i thought I once saw a post about some way to pass the list along. It’s probably easiest to do my old way.

set pseudoList to ""

Repeat with i from 1 to count of theList
set pseudoList to pseudoList & item i of theList & "|"
end repeat

The in the shell script do a split on string with the “|” char and there’s your list.
Was just curious. Like your external record file idea too.

Thanks Nigel, Rob

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.

Hi,

Another thing you can do is the trick with the clipboard. Something like this:

set the_record to {a:"ab", c:"cd"}
set the clipboard to the_record
the clipboard

Then you have key value pairs.

gl,
kel