Welcome to MacForumz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Can Applescript tell if computer is online?

 
   Macintosh computer (Home) -> Apple Scripts RSS
Next:  The RIAA goes completely insane  
Author Message
biz

External


Since: Dec 31, 2007
Posts: 25



(Msg. 1) Posted: Mon Dec 31, 2007 7:22 pm
Post subject: Can Applescript tell if computer is online?
Archived from groups: alt>comp>lang>applescript (more info?)

Hi there,
I would like to write a conditional Applescript. The script would
trigger itself every few hours and if the computer is not connected to
the Internet I would like it to execute some code. I have a bit of
experience with AS but have no idea how to go about this one.
Perhaps: "if Safari gets message "computer not connected to the
internet" then execute this code"...
Am I on the right track?

Thank you much

Karotto

 >> Stay informed about: Can Applescript tell if computer is online? 
Back to top
Login to vote
Sander Tekelenburg

External


Since: Nov 07, 2003
Posts: 742



(Msg. 2) Posted: Tue Jan 01, 2008 2:20 pm
Post subject: Re: Can Applescript tell if computer is online? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article
<a80b8e5e-7e80-4bf2-8215-497ef86c0e51 RemoveThis @h11g2000prf.googlegroups.com>,
biz RemoveThis @theplayspace.com.au wrote:

> I would like to write a conditional Applescript. The script would
> trigger itself every few hours and if the computer is not connected to
> the Internet I would like it to execute some code.

You'll need to define what constitutes "connected to the Internet".
There is no way to ceck for that condition. However, if you define what
it means to *you*, tere will be certain conditions that *can* be checked
for. You could for instance check if the machines has a
non-self-assigned IPv4 address:

property s_saIP : "269.254"
set s_IP4 to IPv4 address of (system info)
if s_IP4 does not start with s_saIP then
-- we are connected to a network
doStuff()
end if

on doStuff()
-- do stuff
end doStuff

This assumes [1] that the Mac uses IPv4, [2] that is set up to be
assigned an address (by a DHCP server), [3] that the network the Mac
conects to is actually connected to/part of "the Internet".

For some reason (Tiger's) Standard Additions allows checking forthe
primary IPv4 address only. If you use IPv6 or care about different
network interfaces (Ethernet, Airport, FireWire, etc.), you'll prolly
need to use do shell script.

> I have a bit of
> experience with AS but have no idea how to go about this one.
> Perhaps: "if Safari gets message "computer not connected to the
> internet" then execute this code"...
> Am I on the right track?

Yes, except that the Web, let alone Safari, is not "the Internet". It's
perfectly possible to be connected to the Internet and yet not be able
to fetch something off the Web.

--
Sander Tekelenburg, <http://www.euronet.nl/~tekelenb/>

Mac user: "Macs only have 40 viruses, tops!"
PC user: "SEE! Not even the virus writers support Macs!"

 >> Stay informed about: Can Applescript tell if computer is online? 
Back to top
Login to vote
biz

External


Since: Dec 31, 2007
Posts: 25



(Msg. 3) Posted: Wed Jan 02, 2008 5:38 am
Post subject: Re: Can Applescript tell if computer is online? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Jan 2, 4:50 am, Sander Tekelenburg <u... RemoveThis @domain.invalid> wrote:
> In article
> <a80b8e5e-7e80-4bf2-8215-497ef86c0... RemoveThis @h11g2000prf.googlegroups.com>,
>
>  b...@theplayspace.com.au wrote:
> > I would like to write a conditional Applescript. The script would
> > trigger itself every few hours and if the computer is not connected to
> > the Internet I would like it to execute some code.
>
> You'll need to define what constitutes "connected to the Internet".
> There is no way to ceck for that condition. However, if you define what
> it means to *you*, tere will be certain conditions that *can* be checked
> for. You could for instance check if the machines has a
> non-self-assigned IPv4 address:
>
> property s_saIP : "269.254"
> set s_IP4 to IPv4 address of (system info)
> if s_IP4 does not start with s_saIP then
>    -- we are connected to a network
>    doStuff()
> end if
>
> on doStuff()
>    -- do stuff
> end doStuff
>
> This assumes [1] that the Mac uses IPv4, [2] that is set up to be
> assigned an address (by a DHCP server), [3] that the network the Mac
> conects to is actually connected to/part of "the Internet".
>
> For some reason (Tiger's) Standard Additions allows checking forthe
> primary IPv4 address only. If you use IPv6 or care about different
> network interfaces (Ethernet, Airport, FireWire, etc.), you'll prolly
> need to use do shell script.
>
> > I have a bit of
> > experience with AS but have no idea how to go about this one.
> > Perhaps: "if Safari gets message "computer not connected to the
> > internet" then execute this code"...
> > Am I on the right track?
>
> Yes, except that the Web, let alone Safari, is not "the Internet". It's
> perfectly possible to be connected to the Internet and yet not be able
> to fetch something off the Web.
>
> --
> Sander Tekelenburg, <http://www.euronet.nl/~tekelenb/>
>
> Mac user: "Macs only have 40 viruses, tops!"
> PC user: "SEE! Not even the virus writers support Macs!"

Thank you for your reply but this does not work. I always get
"10.37.129.3" as IPv4 address regardless if I am online or not. I am
using Leopard.

Thank you
 >> Stay informed about: Can Applescript tell if computer is online? 
Back to top
Login to vote
biz

External


Since: Dec 31, 2007
Posts: 25



(Msg. 4) Posted: Wed Jan 02, 2008 5:14 pm
Post subject: Re: Can Applescript tell if computer is online? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Jan 3, 11:20 am, Jolly Roger <jollyro... RemoveThis @pobox.com> wrote:
> In article <1ia44xt.13adiv31by0hzyN%itera...@inode.at>,
>  itera...@inode.at (Michael Hawelka) wrote:
>
>
>
> > <b... RemoveThis @theplayspace.com.au> wrote:
>
> > > if the computer is not connected to
> > > the Internet I would like it to execute some code
>
> > how about something like
>
> > set theResult to ""
> > try
> >  set theResult to (do shell script "ping -o -t 2www.google.com")
> > end try
> > if (theResult contains "1 packets received") then
> >  display alert ("i am online")
> > else
> >  display alert ("i am offline")
> > end if
>
> But what if google.com goes down for maintenance?
>
> --
> Note: Please send all responses to the relevant news group. If you
> must contact me through e-mail, let me know when you send email to
> this address so that your email doesn't get eaten by my SPAM filter.
>
> JR

Thanks. That almost work. I guess I conceiled some information. What
you told me works great if my ethernet cable is unplugged. However,
sometimes my router deletes my password and user id. It is for these
situations I need the Applescript. When that happens I am online but I
can only reach my ISP's site telling me that Ihave the wrong password.
Unfortunately, I can still ping websites (although I cannot browse the
we because I always get my ISP's website). So the script can actually
tell if I can go to a website (like google. and I don't care if it's
down for maintanance. Happens rarely). Sorry I did not disclose all
information . thanks for your help
 >> Stay informed about: Can Applescript tell if computer is online? 
Back to top
Login to vote
Michael Hawelka

External


Since: Jan 03, 2008
Posts: 1



(Msg. 5) Posted: Wed Jan 02, 2008 6:48 pm
Post subject: Re: Can Applescript tell if computer is online? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

<biz.RemoveThis@theplayspace.com.au> wrote:

> if the computer is not connected to
> the Internet I would like it to execute some code

how about something like

set theResult to ""
try
set theResult to (do shell script "ping -o -t 2 www.google.com")
end try
if (theResult contains "1 packets received") then
display alert ("i am online")
else
display alert ("i am offline")
end if


Mike
 >> Stay informed about: Can Applescript tell if computer is online? 
Back to top
Login to vote
Jolly Roger

External


Since: Sep 09, 2006
Posts: 3119



(Msg. 6) Posted: Wed Jan 02, 2008 6:48 pm
Post subject: Re: Can Applescript tell if computer is online? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <1ia44xt.13adiv31by0hzyN%iterate4@inode.at>,
iterate4.RemoveThis@inode.at (Michael Hawelka) wrote:

> <biz.RemoveThis@theplayspace.com.au> wrote:
>
> > if the computer is not connected to
> > the Internet I would like it to execute some code
>
> how about something like
>
> set theResult to ""
> try
> set theResult to (do shell script "ping -o -t 2 www.google.com")
> end try
> if (theResult contains "1 packets received") then
> display alert ("i am online")
> else
> display alert ("i am offline")
> end if

But what if google.com goes down for maintenance?

--
Note: Please send all responses to the relevant news group. If you
must contact me through e-mail, let me know when you send email to
this address so that your email doesn't get eaten by my SPAM filter.

JR
 >> Stay informed about: Can Applescript tell if computer is online? 
Back to top
Login to vote
Sander Tekelenburg

External


Since: Nov 07, 2003
Posts: 742



(Msg. 7) Posted: Thu Jan 03, 2008 9:55 am
Post subject: Re: Can Applescript tell if computer is online? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article
<4a8b5a8e-d136-4040-8919-03177f12141d.TakeThisOut@e10g2000prf.googlegroups.com>,
biz.TakeThisOut@theplayspace.com.au wrote:

[... condition to check]

> I can still ping websites (although I cannot browse the
> we because I always get my ISP's website).

Then try something like this:

set theResult to ""
try
set theResult to (do shell script "curl -I http://ww.google.com")
end try
if (theResult contains "HTTP/1.1 ") then
display alert ("i am online")
else
display alert ("i am offline")
end if

--
Sander Tekelenburg, <http://www.euronet.nl/~tekelenb/>

Mac user: "Macs only have 40 viruses, tops!"
PC user: "SEE! Not even the virus writers support Macs!"
 >> Stay informed about: Can Applescript tell if computer is online? 
Back to top
Login to vote
biz

External


Since: Dec 31, 2007
Posts: 25



(Msg. 8) Posted: Thu Jan 03, 2008 4:13 pm
Post subject: Re: Can Applescript tell if computer is online? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Jan 4, 12:56 am, Sander Tekelenburg <u... RemoveThis @domain.invalid> wrote:
> In article
> <4a8b5a8e-d136-4040-8919-03177f121... RemoveThis @e10g2000prf.googlegroups.com>,
>
>  b...@theplayspace.com.au wrote:
>
> [... condition to check]
>
> > I can still ping websites (although I cannot browse the
> > we because I always get my ISP's website).
>
> Then try something like this:
>
> set theResult to ""
> try
>  set theResult to (do shell script "curl -Ihttp://ww.google.com")
> end try
> if (theResult contains "HTTP/1.1 ") then
>  display alert ("i am online")
> else
>  display alert ("i am offline")
> end if
>
> --
> Sander Tekelenburg, <http://www.euronet.nl/~tekelenb/>
>
> Mac user: "Macs only have 40 viruses, tops!"
> PC user: "SEE! Not even the virus writers support Macs!"

Thanks for the suggestion but it does not work. The script still
thinks I am online even if I can only reach my ISP's website. I am
surprised there is not a simple parameter that can tell if I actually
can load a website like google or not. Any more suggestions or are we
out of options? Thank you much.
 >> Stay informed about: Can Applescript tell if computer is online? 
Back to top
Login to vote
biz

External


Since: Dec 31, 2007
Posts: 25



(Msg. 9) Posted: Thu Jan 03, 2008 5:05 pm
Post subject: Re: Can Applescript tell if computer is online? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Jan 4, 11:13 am, b....RemoveThis@theplayspace.com.au wrote:
> On Jan 4, 12:56 am, Sander Tekelenburg <u....RemoveThis@domain.invalid> wrote:
>
>
>
> > In article
> > <4a8b5a8e-d136-4040-8919-03177f121....RemoveThis@e10g2000prf.googlegroups.com>,
>
> >  b...@theplayspace.com.au wrote:
>
> > [... condition to check]
>
> > > I can still ping websites (although I cannot browse the
> > > we because I always get my ISP's website).
>
> > Then try something like this:
>
> > set theResult to ""
> > try
> >  set theResult to (do shell script "curl -Ihttp://ww.google.com")
> > end try
> > if (theResult contains "HTTP/1.1 ") then
> >  display alert ("i am online")
> > else
> >  display alert ("i am offline")
> > end if
>
> > --
> > Sander Tekelenburg, <http://www.euronet.nl/~tekelenb/>
>
> > Mac user: "Macs only have 40 viruses, tops!"
> > PC user: "SEE! Not even the virus writers support Macs!"
>
> Thanks for the suggestion but it does not work. The script still
> thinks I am online even if I can only reach my ISP's website. I am
> surprised there is not a simple parameter that can tell if I actually
> can load a website like google or not. Any more suggestions or are we
> out of options? Thank you much.

I GOT IT! Thanks alot. Just had to change the wording a bit (if
contains "google" then...) and now it works fine. Thanks again
 >> Stay informed about: Can Applescript tell if computer is online? 
Back to top
Login to vote
Sander Tekelenburg

External


Since: Nov 07, 2003
Posts: 742



(Msg. 10) Posted: Fri Jan 04, 2008 2:34 am
Post subject: Re: Can Applescript tell if computer is online? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article
<384431b0-3178-4239-a376-410395b0abfd DeleteThis @e10g2000prf.googlegroups.com>,
biz DeleteThis @theplayspace.com.au wrote:

> On Jan 4, 12:56 am, Sander Tekelenburg <u... DeleteThis @domain.invalid> wrote:

[...]

> >  set theResult to (do shell script "curl -Ihttp://ww.google.com")

That's not what I posted. Look carefully at my origial text. You removed
a space -- an essential one (as all characters are, in programming ;)).

Either you somehow messed up the quoting manually, or your newsclient
(Google apparently), screwed things up. Given that your quoting habit
sucks anyway, my bet is that Google is to blame. I suggest you upgrade
to a real newsclient. Plenty of options: <http://www.newsreaders.com/>.

In article
<847897e1-6163-4631-a1b2-89b74e839355 DeleteThis @s12g2000prg.googlegroups.com>,
biz DeleteThis @theplayspace.com.au wrote:

> I GOT IT! Thanks alot. Just had to change the wording a bit (if
> contains "google" then...) and now it works fine. Thanks again

You're welcome, but your change makes it less reliable. There is no
guarantee that the result will contain the string "google". That string
just happens to be in what Google's servers output today, and only for
certain addresses, and very likely only to certain IP addresses, etc.

What I posted is far more reliable. If it doesn't work, you're running
something else than what I posted. (Every character counts, so be
careful when copy&pasting code -- especially beware of linewraps
introduced by newsclients, newsservers, etc.)

--
Sander Tekelenburg, <http://www.euronet.nl/~tekelenb/>

Mac user: "Macs only have 40 viruses, tops!"
PC user: "SEE! Not even the virus writers support Macs!"
 >> Stay informed about: Can Applescript tell if computer is online? 
Back to top
Login to vote
Jolly Roger

External


Since: Sep 09, 2006
Posts: 3119



(Msg. 11) Posted: Fri Jan 04, 2008 2:34 am
Post subject: Re: Can Applescript tell if computer is online? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <user-AF708D.05585604012008.DeleteThis@textnews.euro.net>,
Sander Tekelenburg <user.DeleteThis@domain.invalid> wrote:

> I suggest you upgrade
> to a real newsclient. Plenty of options: <http://www.newsreaders.com/>.

MT-NewsWatcher is arguably the best news client available for Mac OS X.

--
Note: Please send all responses to the relevant news group. If you
must contact me through e-mail, let me know when you send email to
this address so that your email doesn't get eaten by my SPAM filter.

JR
 >> Stay informed about: Can Applescript tell if computer is online? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
New to Applescript - Hello, I'm brand new to Applescript - I've done a lot with VBS but never touched Applescript. Are there some good script repositories online? AppleScript Editors? Manuals? Any help will be greatly appreciated!!! TIA, Bill bill@2burkes.com

Applescript RSS - Is there any way to acquire an RSS feed using Applescript? I'd like to be able to specify the RSS URL, then have Applescript consume the most recent 5 entries. Thanks, A

Leopard, Firefox & Applescript - Can anyone tell me why a script I wrote to log in to my bank no longer works in Firefox since I installed Leopard? Can it by fixed? Thanks

applescript for search with google - Hello, I'm a newbie to Macs and Mac scripting. In TextEdit, under TextEdit>Services, there's an item called Search with Google. Does anyone know whether this is an Applescript and if so, what it says? I'm hoping to adapt it to another text editing..

Is there a quick reference to applescript - hello, i'm looking for a reference to applescript. i have read "apple-script for absolute beginners" (bert altenburg), but it was too less information in it. what i want is a more theoretical approach. i want to know about ALL types and ALL co...
   Macintosh computer (Home) -> Apple Scripts All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]