Re: Applescript, Leopard, and dialup modems
Re: Applescript, Leopard, and dialup modems
- Subject: Re: Applescript, Leopard, and dialup modems
- From: Brian Christmas <email@hidden>
- Date: Tue, 19 Aug 2008 17:27:35 +1000
On 19/08/2008, at 4:06 PM, Donald Hall wrote:
I am trying to update an old mail checking script from Tiger to work
in Leopard. The original used "Internet Connect" to determine the
state of the dialup connection so that it could be made to be the
same after the script ran as before - i.e. if you were not connected
to the Internet before the script checked the mail, you were
immediately disconnected after the check.
"Internet Connect" is not part of Leopard. Can anyone suggest a way
(involving Applescript) of determining the state of a dialup
connection in Leopard?
Thanks,
Don
--
Donald S. Hall, Ph.D.
Apps & More Software Design, Inc.
email@hidden
http://www.appsandmore.com
G'day Don
Are you using my old script 'Dialup Mail checker' from Macscripters by
any chance?
If so, I'll post a query at Macsripters to look for an answer, but as
I'm not on dialup and more, it might be difficult for me to test any
changes.
Here's the script if anyone here can suggest an answer....
Regards
Santa
-- Dial-up Mail Checker
-- Version 1.4
-- Not Bloody Copyright
-- By Santa 2006
-- Sunny Land of Oz
-- email@hidden
-- I owe much of the scripting to folks at MacScripters.
-- http://macscripter.net/
-- NOTE : Must be saved as a 'Stay Open' application
-- BUT, you can use any name, it will still work.
-- Rename & save one as a 'run once' App.
-- and one as 'run every X minutes' App.
-- This Application is designed to work with timed or untimed phone
calls,
-- but also picks up if you have other active connections, such as
broadband.
-- Edit the Application script to set parameters.
-- ======================================
-- Set Mail.app Preferences/General to 'Check for mail manually'. =
-- Set PPP Options to 'Connect Automatically when needed' =
-- if you want
to
. =
-- ======================================
-- NOTE that if you have the sounds turned off in Mail.app, and
-- have 'DisplayMailMessage' set to 'false' then you'll get no feedback
-- when there's new mail; in fact, this App will seem to be not working.
-- ========= Stuff you can change ================
global interval
-- Set this for time between checking mail.
-- (it's in minutes)
set interval to 10 -- minutes
global DisplayMailMessage
-- Set this to 'false' if you don't want to see
-- the 'New Mail' message, otherwise 'true'.
set DisplayMailMessage to true
global RunJustOnce
-- Set this to 'true' if you want to only check
-- for 'New Mail' once, then Quit, otherwise 'false'.
-- Note that existing connections stay open.
set RunJustOnce to false
global IveGotUntimedCalls
-- This is for the Aussies.
-- This is as read. If you're lucky enough to have untimed
-- phone calls, then set this to 'true'; then it won't hang up
-- after checking your eMail. If you want it to hang up,
-- (if not allready online), then leave as 'false'.
set IveGotUntimedCalls to false
-- ======================================
-- Don't fiddle!
global the_count
global NewMailFlag
global AllReadyOnline
set AllReadyOnline to "FirstRun"
property repeat_time : 60
my mainloop()
on idle
if RunJustOnce then
if NewMailFlag then
my ShowMailWindow()
end if
my QuitQuick()
end if
if the_count = interval or the_count > interval then
my mainloop()
end if
set the_count to the_count + 1
return repeat_time
end idle
on mainloop()
set the_count to 0
if AllReadyOnline is not "IP_AllreadyConnected" then -- Found another
connection before, so bypass
tell application "Finder"
try
tell application "System Events"
tell window 1 of process "Internet Connect"
if exists button "Cancel" then
tell application "Network Utility"
quit
delay 5
end tell
end if
end tell
end tell
end try
try
if not (exists process "Internet Connect") then
activate application "Network Utility"
my SendWindowToDock()
end if
end try
end tell
-- This will be picked up at each pass, so if user has disconnected,
will execute as per settings
if my CheckOnLine() is equal to 8 then
my SendWindowToDock()
set AllReadyOnline to "AllreadyConnected"
else
if my CheckOnLine() is equal to 1 then
display dialog "Internet Connect appears to be hung." buttons "So,
I'll Quit"
my QuitQuick()
end if
if my CheckIPAddress() is not equal to "OffLine" then -- We've
found an IP address!
set AllReadyOnline to "IP_AllreadyConnected"
my DisconnectInternetConnect()
my QuitInternetConnect()
if RunJustOnce then
set TagOn to " just the once?"
else
set TagOn to " every " & interval & " minutes?"
end if
display dialog "It appears that you have an Internet connection
other than a dial-up one." & return & return & "You have an IP
address of " & my CheckIPAddress() & return & return & "Do you want to
use this application to check your mail" & TagOn buttons {"Quit this
application", "Yes please"}
if the button returned of the result = "Yes please" then
my GetMail()
return
else
my QuitQuick()
end if
end if
tell application "Network Utility"
activate
end tell
my SendWindowToDock()
tell application "System Events"
tell application "Network Utility"
«event netcconn»
set x to 0
try
repeat until «class RAsb» of «class RSst» of «class ccnf» is
equal to 8
delay 0.2
set x to x + 1
if x = 300 then
display dialog "There seems to be a problem." & return &
return & "I've waited for a minute, but there's no dial up connection
to the server." & return buttons "So, I'll Quit"
my QuitQuick()
end if
end repeat
set AllReadyOnline to "HadToConnect"
end try
end tell
end tell
end if
end if
my GetMail()
if IveGotUntimedCalls is false and AllReadyOnline is "HadToConnect"
then
my DisconnectInternetConnect()
if RunJustOnce then my QuitInternetConnect()
end if
end mainloop
-- ======= Sub-Routines =============
on GetMail()
tell application "Finder"
if not (exists process "Mail") then
tell application "Mail"
activate
set visible of windows to false
end tell
end if
end tell
tell application "Mail"
set ExistingUnread to unread count of inbox
set ExistingOutbox to count of messages in outbox
check for new mail
delay 0.2
repeat until background activity count = 0
delay 0.1
end repeat
set NewUnread to (unread count of inbox) - ExistingUnread
set NewMailFlag to NewUnread > 0
set NewOutbox to count of messages in outbox
set SentOutbox to ExistingOutbox - NewOutbox
end tell
if ExistingUnread = 0 then
set OldDisplayString to "no old unread ones."
else
if ExistingUnread = 1 then
set OldDisplayString to "one older unread one."
else
set OldDisplayString to ExistingUnread & " older unread ones."
end if
end if
set SentString to ""
if SentOutbox = 1 then set SentString to return & return & "One eMail
was sent"
if SentOutbox > 1 then set SentString to return & return & SentOutbox
& " eMails were sent."
if NewUnread > 0 then
if NewUnread = 1 then
set NewDisplayString to "eMail, and "
else
set NewDisplayString to "eMails, and "
end if
if DisplayMailMessage then
tell application GetMyName()
activate
display dialog "You have " & NewUnread & " new " &
NewDisplayString & OldDisplayString & SentString buttons {"Stop this
window", "Continue checking", "I'll read mail please"} with icon note
giving up after interval * 60
end tell
set ButtonReturned to the button returned of the result
if ButtonReturned is "I'll read mail please" then
ShowMailWindow()
else
if ButtonReturned is "Stop this window" then set
DisplayMailMessage to false
end if
end if
end if
end GetMail
on ShowMailWindow()
activate application "Mail"
tell application "Mail"
try
set IsVisible to visible of window 1
if IsVisible is false then
my ShowWindow()
end if
on error
my ShowWindow()
end try
end tell
end ShowMailWindow
on ShowWindow()
tell application "System Events" to tell application process "Mail"
to tell menu bar 1 to tell menu bar item 3 to tell menu 1 to tell menu
item 2
click
end tell
end ShowWindow
on SendWindowToDock()
tell application "System Events"
try
if exists application "Network Utility" then
repeat with btn in (buttons of (get window 1 of application
process "Internet Connect" of application "System Events"))
if value of attribute "AXSubrole" of btn is "AXMinimizeButton" then
perform action "AXPress" of btn
exit repeat
end if
end repeat
end if
end try
end tell
end SendWindowToDock
on CheckOnLine()
tell application "Network Utility"
-- Are we already online? Should be 8 if yes.
try
set temp to «class RAsb» of «class RSst» of «class ccnf»
on error
set temp to 1
end try
end tell
return temp
end CheckOnLine
on CheckIPAddress()
do shell script "curl http://checkip.dyndns.org/ | grep 'Current IP
Address' | cut -d : -f 2 | cut -d \\< -f 1"
if result = "" then
return "Offline"
else
return (characters 2 through -1 of result) as text
end if
end CheckIPAddress
on DisconnectInternetConnect() -- We're gonna disconnect!
tell application "Finder"
if not (exists process "Internet Connect") then
tell application "Network Utility"
activate
my SendWindowToDock()
end tell
end if
tell application "Network Utility"
«event netcdcon»
end tell
end tell
end DisconnectInternetConnect
on QuitInternetConnect()
tell application "Finder"
tell application "Network Utility"
quit
end tell
end tell
end QuitInternetConnect
-- Use this so the App can be any name.
on QuitQuick()
tell application "System Events"
try -- Should stop the application with no dialogs and no items saved.
do shell script "kill -9 " & (unix id of processes whose name is my
GetMyName())
end try
end tell
end QuitQuick
on GetMyName()
set temp to (name of (info for (path to me)))
set temp2 to replace_chars(temp, ".app", "")
return temp2
end GetMyName
on replace_chars(this_text, search_string, replacement_string)
set StoreDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to StoreDelimiters
return this_text
end replace_chars
_______________________________________________
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