newbie question, getting a value from a list with a string


What I try to achieve is that the dialog should output the IP adres that is in the list.

set ipList to {DNS1:"", DNS2:""}
set input to "DNS1"
set output to input of ipList
display dialog output

it gives an error:
error "input of {DNS1:"", DNS2:""} kan niet worden opgevraagd. " number -1728 from input of {DNS1:“”, DNS2:“”}

If I do:

set output to DNS1 of ipList

it works, so my guess I should do something with variable input.

I’ve been googling for some time now but I can’t seem to find a hint. I’m pretty sure the answer is already somewhere on the but I can’t seem to find it. Sorry for that.

You can access the labeled properties of a record only through their name, not through the contents of a variable. There isn’t any property labeled input in your record.

The problem you are encountering is that DNS1 is part of a record [which is actually similar to but slightly different from a list].

To access / reference an item from a list or record you have to use code similar to that which is in your second scriplet…that is:

set input to item <<insert item number>> of list


set input to  <<property name>> of record

I hope that this helps,


[Edited to correct the accessing information from a record]

Hi Joel,
I tried before, doesn’t work, you can’t get something with item if you use records.


While it’s true that you can only access record properties using their labels (not text representations of those labels), you can cheat a little like this:

set ipList to {DNS1:"", DNS2:""}
set input to "DNS1"
set output to (run script "on run argv -- argv is the parameter list passed to this script.
return " & input & " of item 1 of  argv
end" with parameters {ipList})
display dialog output

Or cheat a little like this, (I thought you might want to differ between the DNS1 and DNS2 “records”, so here you can seek, instead of just choosing the first record. There are scripts here to extract properties from records, some of which Nigel Garvey has made. There also used to be a List&Records scripting addition that is made by Late Night Software.

set ipList to {DNS1:"", DNS2:""}
set sought to "DNS1"

# save old contents of clipboard here . . . 
set the clipboard to ipList as record
set ml to the clipboard as list
set found to "Not found"
repeat with i from 1 to ((count ml) - 1) by 2
	if item i of ml is sought then
		set found to item (i + 1) of ml
		exit repeat
	end if
end repeat
display dialog found
-- > ""

Nigel Garveys method above, is far better, I just didn’t think of it, while I made this one. :slight_smile:

And if you’re running Yosemite:

use scripting additions
use framework "Foundation"

set ipList to {DNS1:"", DNS2:""}
set anNSDictionary to current application's NSDictionary's dictionaryWithDictionary:ipList
set output to (anNSDictionary's objectForKey:"DNS1") as text
display dialog output

Hey Johan,

Nigel’s and Shane’s methods work well for the circumstances.

ASObjC gives some nice options for working with records that aren’t otherwise available to AppleScript.

Nevertheless AppleScript records are a notoriously limited, and therefore I’ve been using text-records and one of the regex osaxen available since the mid '90s.

The currently available osax for adding regex to AppleScript is the Satimage.osax

With it your lookups are lightning fast, and your ipList management is as easy as editing text.

set ipList to "
set inputVar to "DNS1"

	set foundIP to find text inputVar & ":([\\d.]+)" in ipList using "\\1" with regexp and string result
on error
	set foundIP to false
end try


Here is another variation, that uses vanilla AppleScript, and works towards text records. We do add the colon to the property here.

set ipList to {DNS1:"", DNS2:""}
	set ipText to "" & ipList
on error e
	set f to text ((offset of "{" in e) + 1) thru ((offset of "}" in e) - 1) of e
end try
set {astid, text item delimiters, sought} to {text item delimiters, "DNS2:", "Not found"}
if (count text items of f) > 1 then
	set mt to text item 2 of f
	set text item delimiters to ","
	set imed to text item 1 of mt
	set text item delimiters to "\""
	set sought to text item 2 of imed
end if
set text item delimiters to astid
return sought

Removed an unnecessary if test, and removed escaped double ticks.