What I try to achieve is that the dialog should output the IP adres that is in the list.
set ipList to {DNS1:"8.8.8.8", DNS2:"8.8.4.4"}
set input to "DNS1"
set output to input of ipList
display dialog output
it gives an error:
error "input of {DNS1:"8.8.8.8", DNS2:"8.8.4.4"} kan niet worden opgevraagd. " number -1728 from input of {DNS1:“8.8.8.8”, DNS2:“8.8.4.4”}
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.
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:"8.8.8.8", DNS2:"8.8.4.4"}
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:"8.8.8.8", DNS2:"8.8.4.4"}
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
-- > "8.8.8.8"
Nigel Garveys method above, is far better, I just didn’t think of it, while I made this one.
use scripting additions
use framework "Foundation"
set ipList to {DNS1:"8.8.8.8", DNS2:"8.8.4.4"}
set anNSDictionary to current application's NSDictionary's dictionaryWithDictionary:ipList
set output to (anNSDictionary's objectForKey:"DNS1") as text
display dialog output
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 "
DNS1:8.8.8.8
DNS2:8.8.4.4
"
set inputVar to "DNS1"
try
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:"8.8.8.8", DNS2:"8.8.4.4"}
try
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
Edit
Removed an unnecessary if test, and removed escaped double ticks.