1

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 here but I can't seem to find it. Sorry for that.

UPDATE: I think I asked the question all wrong.

Let me retry, I have a list:

set ipList to {DNS1:"8.8.8.8", DNS2:"8.8.4.4"}

I would like to loop through the items. So have this coded with less code:

set IP_address to "8.8.8.8"
try
    set ping to do shell script ("ping -c 2 " & IP_address & "| head -2 | tail -1 |cut -d = -f 4")
    if ping contains "ms" then
        set Output1 to "DNS 1 UP"
    else if ping contains "timeout" then
        set Output1 to "DNS 1 DOWN"
    end if
end try
set IP_address to "8.8.4.4"
try
    set ping to do shell script ("ping -c 2 " & IP_address & "| head -2 | tail -1 |cut -d = -f 4")
    if ping contains "ms" then
        set Output2 to "DNS 1 UP"
    else if ping contains "timeout" then
        set Output2 to "DNS 1 DOWN"
    end if
end try

display dialog (Output1 & return & Output2) buttons {"OK"} default button 1 with title "Resultaat"

Again, I'm a newbie, I'm sorry

2

This would be my take on this:

set ipList to {"8.8.8.8", "8.8.8.6", "8.8.4.4"}
set Output1 to ""
set Output2 to ""
global Output1, Output2
repeat with i from 1 to number of items in ipList
    set this_item to item i of ipList
    my ipCheck(this_item, i)
end repeat


if Output1 is not "" or Output2 is not "" then
    display dialog (Output1 & Output2) buttons {"OK"} default button 1 with title "Resultaat"
end if

on ipCheck(IP_address, i)
    try
        set ping to do shell script ("ping -c 2 " & IP_address & "| head -2 | tail -1 |cut -d = -f 4")
        if ping contains "ms" then
            set Output1 to Output1 & return & "DNS" & i & "  UP"
        else if ping contains "timeout" then
            set Output2 to Output2 & return & "DNS" & i & " DOWN"
        end if
    end try
end ipCheck

enter image description here

0

You need to be asking for the 'item' [record] referred to by the list 'label' [record property]
The label [record property] is DNS1, the item [record] is the string "8.8.8.8"

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

You can easily just shorthand that to

set ipList to {DNS1:"8.8.8.8", DNS2:"8.8.4.4"}
display dialog DNS1 of ipList

Though I imagine it's part of a larger construction

Edit: btw, you cannot extract a record by its index. That only works for lists without predefined property:record structures.

Edit 2 after question edit.

Try something like this…

--debug only, to save actually doing the ping, swap to test
--set ping to "ms"
set ping to "timeout"

global ipList
global resultsList
global theString
set ipList to {"8.8.8.8", "8.8.4.4"}
set resultsList to {}
set theString to ""

repeat with IP_address in ipList
    --set ping to do shell script ("ping -c 2 " & IP_address & "| head -2 | tail -1 |cut -d = -f 4")
    if ping contains "ms" then
        set the end of resultsList to IP_address & " UP"
    else if ping contains "timeout" then
        set the end of resultsList to IP_address & " DOWN"
    end if
end repeat

--build dialog
repeat with theReturn in resultsList
    set theString to theString & (theReturn & return)
end repeat
display dialog (theString) buttons {"OK"} default button 1 with title "Resultaat"

NOTE: I'm not really used to working in Applescript. It treats globals & locals differently to what I'm used to, so my declarations at the top might be overkill, but it does function that way.

  • It is part of a bigger thing and maybe i've asked my question too limited. In the end I would like to loop through this list . – Johan Kuiper Dec 31 '14 at 12:44
  • not sure you can do that with records, if the list is of variable length. If it's fixed, then you can call DNS1 then DNS2 but you can't call by an 'unknown' record. Might be better to add raw data & iterate through it by index. This would appear to agree with me. The example given as answer doesn't work for me. return ipList's every item will give you all the values, but it doesn't link them to any specific record – Tetsujin Dec 31 '14 at 13:00
  • thanks for the response, i've updated my question to show what I need. I did see the question you linked to but I don't see the similarity to my question :-) – Johan Kuiper Dec 31 '14 at 13:07
  • it was referring to iterating a list containing record properties, which cannot be done [apparently]. Your edited question makes no reference to record properties, so I'm a bit stuck now. – Tetsujin Dec 31 '14 at 13:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .