Convert or Change Variable

Hi Folks,

Can anybody tell me the correct syntax to rename a variable?


set conn_status to state of status as string

gives me as result a number from 0 to 10 - Works perfect!

now I want to write text instead of the number:


if conn_status = 0 then
conn_status = "Not Connected"
else if conn_status = 1 then
conn_status = "Dialing"
else if....

end if

set content of text field "status" of window "main" to conn_status

Result: not text but a number…

Any hints or tipps?

Thanks a lot!

Best Regards,

Stefan

Hi Bosstone,

You can’t just say x=1, but need the ‘set’ command

set x to 1

Anyway, using list might be better than writing a lot of if then-else if statements. e.g.

set conn_list to {“a”, “b”, “c”}
set conn_status to 1
set conn_status to item (conn_status + 1) of conn_list
conn_status
→ result: “b”

gl,

Hi,

variables can only be set with the set or copy command.

you can do it like this:

if conn_status = 0 then
set conn_status_msg to "Not Connected"
else if conn_status = 1 then
set conn_status_msg to "Dialing"
else if....

end if

set content of text field "status" of window "main" to conn_status_msg

Model: G5 dual 2,5 GHz
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi Stefan,

perfect - in my applescript book is this option not described…

so we have two ways:


set conn_list to {"Not Connected", "Dialing", "Connecting", "Error", "Connected", "Disconnecting",...}
set conn_status to item (conn_status) of conn_list

this is very elegant - but if you have numbers: 0,2,4,5,7,8 then you have a problem - you have to enter
some entreas two times…


if conn_status = 0 then
set conn_status_msg to "Not Connected"
end if

Is much more to write but is more flexible…

Thanks for your help…

Now I have to find out how to update a state…

Best Regards,

Stefan

I would prefer the list version.
For status numbers which are not available use an n/a entry
The first item in a list has the index 1, so you must add 1 to the number

set conn_list to {"Not Connected", "n/a", "Dialing", "n/a", "Connecting", "Error", "n/a", "Connected", "Disconnecting"}
set conn_status to item (conn_status + 1) of conn_list

Model: G5 dual 2,5 GHz
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

hmmm…

funny thing - compiling was fine but when building and running
I get an error message:

the variable conn_status_msg is not defined…


tell application "Internet Connect"
set conn_status to state fo status as string
if conn_status = 0 then
set conn_status_msg to "Not Connected"
else if conn_status = 1 then
set conn_status_msg to "Dialing"
end if
end tell
set content of text field "status" of window "main" to conn_status_msg

that’s correct, because you coerced the status number to a string
so no condition is true

Hi Stefan,

now it works - I have tried your suggestions - but instead of writing n/a I have to double the entrea - but
neverminds - the codes should not change…

thanks,

Stefan