Display dialog list as string

I’m just going through items trying to expand on my AppleScript knowledge and I was experimenting with a simple script. I have used the statement:

display dialog b as string

where b is a list, on occasion, for troubleshooting purposes. I used this script:

set l to {"1", "a", "g", 2, 7, 9}
set b to {} as list
repeat with a in l
	if class of a is integer then
		set b's end to a
	end if
end repeat
log (class of b)
display dialog (b) as string

and where the display dialog is, it just displays an empty dialog box (with the usual ok & cancel buttons). However, if I add “as list” to the end of “set b’s end to a”, the display dialog works as expected. Eventhough in both instances the class of b is a list, why is it that without the “as list” the dialog is empty?

PreTech

Notice the results.

This turns a into a list each time; Probably not what you want for the actual script, but it does coerce to a string.

set b's end to a as list
--> {{2}, {7}, {9}}

Here, it’s seems you’re getting references instead of actual values.

set b's end to a
--> {item 4 of {"1", "a", "g", 2, 7, 9}, item 5 of {"1", "a", "g", 2, 7, 9}, item 6 of {"1", "a", "g", 2, 7, 9}}

When that happens (or better[?] yet, always) , ask for the contents explicitly:

set l to {"1", "a", "g", 2, 7, 9}

set b to {}
repeat with a in l
	if class of a is integer then
		set b's end to a's contents
	end if
end repeat

log (class of b)
display dialog (b as string) --> 279 (assuming text delimiters aren't changed.)
return b
--> {2, 7, 9}

Thanks Bruce!

I have another question then. I actually just wrote the script to play with the log function (which I had never used before). I suppose I can see (especially now that you point it out) that the line:

set b's end to a

results in a list of references because “a” is a reference in the first place and also the other version results in a list of lists. The question is, though, did you just know this or is there some way I can get script editor to show me what the results actually are (that I may be unaware of)? I ask this because using the log function for “log a” in the repeat just gives the integers and of course the conditional statement comes back correctly.

PreTech

I saw that by returning b at the end of the script.

set l to {"1", "a", "g", 2, 7, 9}

set b to {}
repeat with a in l
	if class of a is integer then
		set b's end to a -- removed the explicit `contents` part
	end if
end repeat

log (class of b)
display dialog (b as string)
return b

I may be missing the point at a glance, but why not:

set Lst to {"1", "a", "g", 2, 7, 9}
display dialog (integers of Lst as string)
--> 279
log (text of Lst as string)
--> 1ag

As an adder to that, it works without ‘contents of’ as well:


set Lst to {{1, "a", "g", 2, 7, 9}, {"a", "b", 2, 3, 4}}
repeat with aLst in Lst
	log (integers of aLst as string)
end repeat

Yields:

If PreTech actually wants a list of integers, then both of those will work great Adam. :slight_smile:

Thanks to you both!

When I started playing around with this and the log statement I actually wanted an easy way to set another variable to the integers of the list. I tried something like “set b to every item of l whose class is integer” which doesn’t work at all. Of course the simple solution escaped me. :rolleyes:

Other than in idle handlers, I have not really used the return statement either. That’s quite useful. It will probably help immensely!

Thanks again.

PreTech