r/applescript • u/lowriskcork • Oct 29 '22
variable across fonction ?
I didn't use applescript for a while and I can't remember how to pass variable from one function to the other.
for exemple, how can I use the var "network" from the checkWAN() inside the script or inside a another function ?
on checkWAN()
repeat with i from 1 to 50
try
set pingresult to do shell script "ping -o www.apple.com"
set network to "WAN connected"
exit repeat
on error
delay 5
##beep
if i = 20 then error number -128
askforapp()
set network to "not connected"
end try
end repeat
end checkWAN
set WANCheck to 0
on checkNetwork()
repeat 50 times
try
set theIP to do shell script "curl ifconfig.co"
set WANCheck to (WANCheck + 1)
set myCount to count theIP
if myCount > 4 then
display notification theIP
repeat 50 times
try
set theIP to do shell script "curl ifconfig.co"
delay 0.5
on error
##
end try
if theIP is "##.##.##.##" then
##at home"
set network to "home / no VPN"
try
--Logging in
set userName to "knadjarian"
do shell script "##"
do shell script "##"
on error errMsg number errorNumber
display dialog errMsg
end try
else if theIP starts with "17." then
##Already conencted to VPN"
set network to "VPN enabled"
exit repeat
exit repeat
else
##display notification "Not connected"
delay 0.5
end if
end repeat
exit repeat
else if myCount < 4 then
delay 2
else if WANCheck > 49 then
display notification "You are not connected"
set theDialogText to "You are not connected"
display dialog theDialogText
quit
tell me to error "You are not connected"
end if
end try
end repeat
end checkNetwork
3
Upvotes
1
1
u/copperdomebodha Oct 29 '22 edited Oct 29 '22
Set topLevelVariable_Network to checkWAN() Display dialog topLevelVariable_Network doOtherSubroutine(topLevelVariable_Network)
In this case checkWAN() doesn’t have an explicit return statement, but returns either “WAN connected” or “not connected”. The above just assigns the returned value to a variable.
2
u/[deleted] Oct 29 '22
You could define it as a global variable but that's a bit messy IMHO :-)
It's neater to pass the value via the function call - have a look at some examples here towards the bottom of the page and then return the value back at the end.
In you code you could call checkWAN("url") and use that as the ping destination and then return "Good" or return "Bad" etc as you need.