PDA

View Full Version : a little help w/ applescript reading from a list


danjmw
10-17-2006, 08:45 PM
I'm having trouble understanding why I can not read from a list with a variable created from input (please keep in mind I'm new to applescript and scripting in general). I have a list constructed as follows:

set maskValuesA to {bit8:"0", bit9:"128", bit10:"192", bit11:"224", bit12:"240", bit13:"248", bit14:"252", bit15:"254"}
set maskValuesB to {bit16:"0", bit17:"128", bit18:"192", bit19:"224", bit20:"240", bit21:"248", bit22:"252", bit23:"254"}
set maskValuesC to {bit24:"0", bit25:"128", bit26:"192", bit27:"224", bit28:"240", bit29:"248", bit30:"252", bit31:"254"}

the script pulls an integer from a table (the integer is a bit value of a subnet from an IP host/network definition) and attaches the string "bit" to it in the following line (targetIPsub is the integer):

set targetIPbit to "bit" & targetIPsub

the script is to then run targetIPbit through the list to retrieve the correct subnet value:

if targetIPsub is less than "16" then
set ipMask to targetIPbit of maskValuesA
set targetIPmask to "255." & ipMask & ".0.0"
else
if targetIPsub is less than "24" then
set ipMask to targetIPbit of maskValuesB
set targetIPmask to "255.255." & ipMask & ".0"
else
if targetIPsub is less than "32" then
set ipMask to targetIPbit of maskValuesC
set targetIPmask to "255.255.255." & ipMask
else
set targetIPmask to "255.255.255.255"
end if
end if
end if

if I have the script print out targetIPsub it shows the value correctly, but I receive the following error (w/ a value between 24 and 31):

Can't get targetIPbit of {bit24:"0", bit25:"128", bit26:"192", bit27:"224", bit28:"240", bit29:"248", bit30:"252", bit31:"254"}.

now, if I declare targetIPbit directly, such as:

set targetIPbit as bit24

then I can pull from the table. what am I missing?

hayne
10-17-2006, 10:49 PM
After the statement:

set targetIPbit to "bit" & targetIPsub

the variable targetIPbit contains a string value - e.g. "bit24" if targetIPsub is "24".
The string value "bit24" is not the same as the label bit24
The latter is what you need to use when getting an element of the record.

In other words, you need to refer to record elements by name and you can't store the name in a variable - you need to use the names literally.
So you will need to change to some other method of access.
One easy (if slightly messy) way would just be to use a set of 'if .. then' statements to get the value.
You could hide this messiness away in a subroutine.

But you don't seem to be doing anything that requires AppleScript.
AppleScript is mostly useful when you want to interact with OS X applications.
If you are doing some computation, it is likely easier to do it in some other scripting language like Bash or Perl.

danjmw
10-17-2006, 11:04 PM
that's what I suspected. there's more to the script that does call on Terminal so Applescript does work. I've also looked at extending the if ... then down through all 32 options, but it was very messy. I'm looking for a cleaner way. if there a way to convert the string into something that could be used as an element, such as writing the string somewhere where it could then be pulled as a usable element of a variable? I tried writing it to a temporary file and then reading it back, but had the same problem.

hayne
10-17-2006, 11:09 PM
there's more to the script that does call on Terminal so Applescript does work
What exactly are you doing with this script?
E.g. what are you telling the Terminal app to do?
It seems likely to me that you should be writing a shell script, not an AppleScript.

Note also that there are lots of subnet calculator scripts already written - I think Google will find them for you.