• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Need helps with my script
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Need helps with my script


  • Subject: Re: Need helps with my script
  • From: 2551 <email@hidden>
  • Date: Mon, 05 Jan 2015 18:25:11 +0700

On 5 Jan 2015, at 17:17, Thomas Labarre <email@hidden> wrote:

Hello list
I have an issue with the first part of my script. I am a beginner in Applescript and I can not define my variable "test_ping" in my block.
The script is to define the user if it is connected to the network or not.
If it is not, a simple message pop up, if it is, an information window gives the IP address of ethernet or wifi and the default router
I have a worry in the first part. If my gateway value is not correct, I have an error message other than the one I set.
My problem comes when no network interface is defined


I couldn't quite do all the translations, but here's a script I wrote some time ago that I think does what you want in the first part of your script. You should be able to easily bolt on the second part that you want to add.

This script will deteremine the IP Addresses of your router, your node on the internal network (aka 'Internal IP') and your public internet IP address (aka 'External IP') and allow you to copy them for use in Terminal or other applications

The script is set to display 'No connection' after a pretty short 3-second timeout. Feel free to increase that at the line in the script after the comment "# CHANGE THE DELAY HERE…". Just change the number '3' to something longer (the number = seconds). Alternatively,  just keep hitting the 'Try Again' button when the script runs as necessary.

Enjoy! 

Phil  


#START OF SCRIPT

property theNext : ""
property theNetwork : ""
property theRouter : ""
property theLocalNode : ""


on getIP()
try
set myTemp to do shell script "mktemp -t txt"
do shell script "curl -s http://checkip.dyndns.org &> " & myTemp & " &2> /dev/null"
# CHANGE THE DELAY HERE…
delay 3
set extIP to do shell script "sed 's/[a-zA-Z/<> :]//g' " & myTemp
if extIP = "" then
set my theNetwork to "No connection"
else if extIP contains "=" then
set theNetwork to "Can't get IP"
else
set theNetwork to extIP
end if
on error
set theNetwork to "No connection"
end try
end getIP

on getRouter()
try
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "gateway:"
set theGateway to do shell script "route get default | grep gateway"
set AppleScript's text item delimiters to oldDelims
set theRouter to the last word of theGateway
on error
set my theRouter to "No connection"
end try
end getRouter

on getLocalNode()
try
set theIP to (do shell script "ifconfig | grep inet | grep -v inet6 | cut -d\" \" -f2")
set theLocalNode to the last word of theIP
on error
set theLocalNode to "Can't get Local IP"
end try
end getLocalNode



on getCopyItem()
try
set theList to {"Router", "Local IP", "External IP"}
choose from list theList with prompt "Choose an item to copy:"
set myResult to (item 1 of the result)
if myResult = "Router" then
set the clipboard to theRouter
else if myResult = "Local IP" then
set the clipboard to theLocalNode
else
set the clipboard to theNetwork
end if
end try
end getCopyItem

on userInfo()
display dialog "Router: " & theRouter & return & return & "Local IP: " & theLocalNode & return & return & "External IP: " & theNetwork buttons {"OK", "Copy", "Try Again"} cancel button {"OK"} with title "Your IP addresses" default button "Try Again"
set theNext to the button returned of the result
return result
end userInfo

repeat
try
getRouter()
getIP()
getLocalNode()
userInfo()
if result = "Try Again" then
getIP()
else if theNext = "Copy" then
getCopyItem()
exit repeat
end if
on error
exit repeat
end try
end repeat

#END OF SCRIPT




 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Need helps with my script (From: Thomas Labarre <email@hidden>)

  • Prev by Date: JavaScript for Automation curiosity (was "Script that "Tells" Finder just Focuses on Finder instead of running")
  • Next by Date: Re: JavaScript for Automation curiosity (was "Script that "Tells" Finder just Focuses on Finder instead of running")
  • Previous by thread: Need helps with my script
  • Next by thread: [ANN] DisplayDroid - Beta testers wanted
  • Index(es):
    • Date
    • Thread