I am recently experiencing unstable Internet connection I just want to make a program with applescript that reminds me when my Internet is reachable so I can go online. Basically the program checks the Internet by pinging google.com for example. I know I can run shell script using applescript, but the problem is, how to get the return value of the ping and put it in an if statement?
Pure AppleScript solution:
set testIP to chkUP("http://www.apple.com") or chkUP("http://www.google.com")
if testIP then
display dialog "Internet Connection is UP"
else
display dialog "Internet Connection is DOWN"
end if
to chkUP(theURL)
return (count (get ((theURL as URL)'s host & {dotted decimal form:""})'s dotted decimal form)) > 0
end chkUP
I've added two scripts.
The first one checks your internet connection once.
The second runs in the background every minute and displays a popup if it can reach the internet.
Script 1
try
set thePing to do shell script "/sbin/ping -o -c 5 8.8.8.8"
on error
set thePing to "no internet connection"
end try
Script 2
(*
1. Save as an Application: Script Editor > File > Export… > File Format: Application
2. Check "Stay open after run handler"
3. Run the app or add it to your login items: System Preferences > Users & Groups > User > Login Items > Press the "+" button
# http://stackoverflow.com/questions/43250408/applescript-determine-internet-reachability
*)
on idle
try
set thePing to do shell script "/sbin/ping -o -c 5 8.8.8.8"
if thePing does not contain "error" then
display dialog "The internet connection is up"
end if
on error
set thePing to "error: no internet connection"
end try
delay 60
end idle
Related
I can connect to Lexmark printers via telnet to access the configuration menu and I'm trying to find a way to configure them scripting the commands (there is more than 200 printers).
After successful connection here is the menu in the telnet window :
MAIN MENU
1. Set IP address Options
2. Set IPv6 address Options
3. Set IP Protocol enables
4. Set MTU......................................... ( 1500 )
5. Set restricted server list
6. Set lpd options
7. Set SNMP community name......................... ( public )
A. Save Changes
X. Exit current menu
Selection: 1
Is there a way I can select for example the option 1 via a batch or shell script even if this is a telnet connection ?
On windows, you could run Telnet scripting tool by this command: "%_path%\TST10.exe" /r:"%_path%\telnet.txt" /o:%_out_file% with pre-created telnet.txt file:
echo %hostname% 23>telnet.txt
echo WAIT "Selection:">>telnet.txt
echo SEND "1\m">>telnet.txt
Description of telnet.exe file: First line establish connection to host, second line insctruct program to wait till string "Selection:" was received (this should guarantee whole menu was drawed in screen) and last line send keypress "1" and CRLF. Than you can continue by same way with another possible sub-menu screens.....
I want to take a screen capture of a dynamic webpage. Content changes every hour of the day.
My script works fine but:
1. I want to make sure the script is active while the iMac (OS X
Yosemite Version 10.10.5) is in sleep modus or when the screen saver is active.
2. Second problem is that the screen capture must be my desired webpage
and not the active window.
set dFolder to "~/Desktop/screencapture/"
do shell script ("mkdir -p " & dFolder)
set i to 0
repeat 24 times
do shell script "open -a Safari http://www.lipsum.com/"
do shell script ("screencapture " & dFolder & "frame-" & i & ".png")
delay 3600 -- Wait for 3600 seconds.
set i to i + 1
end repeat
I don't believe you can do what you want with the tool you have chosen. I think you need to use webkit2png.
Go to GitHub and download the webkit2png script from here. It is just a Python script in a single file. You will need to edit it around line 420 so it looks like this (there are 3 lines added in the middle):
# Hide the dock icon (needs to run before NSApplication.sharedApplication)
AppKit.NSBundle.mainBundle().infoDictionary()['LSBackgroundOnly'] = '1'
# Handles ATS HTTPS requirement introduced in El Cap
if options.ignore_ssl_check:
AppKit.NSBundle.mainBundle().infoDictionary()['NSAppTransportSecurity'] = dict(NSAllowsArbitraryLoads = True)
app = AppKit.NSApplication.sharedApplication()
Now you can download your website whether the screensaver is running or not and regardless of which window has focus using:
./webkit2png --ignore-ssl-check -W 800 -H 600 -F -o MYSITE http://www.lipsum.com/
You can use this with your existing Applescript - just put your "frame-" & i after the MYSITE part.
You will probably need to put the full path instead of ./webkit2png, so, depending where you save the script, you may need to use something like /Users/Geonemec/webkit2png --ignore-ssl-check ...
You can get help on the options it accepts by running:
webkit2png -h
set dFolder to "~/Desktop/screencapture/"
do shell script ("mkdir -p " & dFolder)
set i to 1
repeat 2 times
set DeTijd to time string of (current date)
tell application "Terminal"
activate
do script ("webkit2png --ignore-ssl-check -F -o PR" & DeTijd & " " & "http://www.google.com")
end tell
delay 3600 -- Wait for 3600 seconds = 1 hour.
set i to i + 1
end repeat
This works fine.
Tx for the help.
I'm trying to make an Applescript that connects to a list local ssh machines, with each connection opening in a new terminal window. Prior to attempting the ssh connection, I'd like to ping the client to see if it's available: if it is, run the ssh command, if not then iterates to the next client. When I run the script it seems to work for the first connection but then gives me --> error number -10004 for the remaining clients (and hangs the debugger). Any feedback would be greatly appreciated, thanks!
set hosts to {"10.2.0.199", "10.2.0.11", "10.2.0.91", "10.2.1.591", "10.2.0.41"}
set uname to {"asus_client01", "asrock_comp", "msi003", "gigabyte4", "intel05client"}
tell application "Terminal"
activate
repeat with i from 1 to the count of hosts
set this_uname to item i of uname --extract individual username
set this_host to item i of hosts as string --extract iPv4
set uname_host to this_uname & "#" & this_host
set hostUp to true
try
do shell script "ping -c 1 -t 5 " & this_host
on error
set hostUp to false
display dialog this_host & " seems to be down."
delay 2
end try
if hostUp then
do shell script "ssh " & uname_host
end if
end repeat
end tell
There is a difference between do shell script and do script. The difference is that do shell script is part of the standard script addition and will open an non-interactive shell, execute the given string, and return stdout back to you without any help from another application like Terminal. do shell script should never been used in any other tell application block except itself (me) because you violate some AppleScript securities you can find in AppleScript release and technical notes. do script command is part of AppleScript command in the application Terminal. do script will enter the given string in the targeted window and execute that like you have typed in Terminal yourself. do script is only supported by Terminal application and can't be used outside of it.
So it's either do shell script:
do shell script "ping -o stackoverflow.com
or do script by using the Terminal
tell application "Terminal"
do script "ping -o stackoverflow.com"
end tell
So the total script could look something like this:
set hosts to {"10.2.0.199", "10.2.0.11", "10.2.0.91", "10.2.1.591", "10.2.0.41"}
set uname to {"asus_client01", "asrock_comp", "msi003", "gigabyte4", "intel05client"}
--security check: hosts list can't be longer than uname
if (count of hosts) > (count of uname) then return
repeat with i from 1 to count hosts
repeat 1 times -- simulate continue
set currentAddress to item i of hosts
set currentHostname to item i of uname
if not ((do shell script "ping -o -t 5 " & currentAddress & "&>dev/null && echo yes || echo no") as boolean) then
exit repeat -- continue
end if
tell application "Terminal"
do script "ssh " & currentHostname
end tell
end repeat
end repeat
I was wondering if it is possible for an applescript application to run a shell script, then quit before the execution of the shell script is completed. This would be useful when running a server for a given amount of time. Instead of needing the applescript to be constantly running in the background, is there any way to run a function independently?
set server_name to text returned of (display dialog "Choose server." default answer "")
set success to do shell script "if [ -f \"/Users/jessefrohlich/Documents/Minecraft/" & server_name & "/minecraft_server.jar\" ]; then
echo 1;
else
echo 0;
fi"
if success is equal to "1" then
do shell script "cd /Users/jessefrohlich/Documents/Minecraft/" & server_name & "
java -Xmx1024M -Xms1024M -jar minecraft_server.jar"
else
display dialog "Sorry, the file you chose is invalid."
end if
When the above is run as an application, it will launch the server properly. However, the application runScript.app will continue to run. The server will keep running even if the applescript is force quit. Is there any way to have it quit automatically, as soon as the server is launched?
Thanks
Try this. Good luck.
-- "> /dev/null" redirects standout out to nowhere land
-- - you can use some other file path if you want to capture its output
-- "2>&1" redirects standard error to the same place as standard out
-- - 2 stands for standard error
-- - 1 stands for standard out
-- - > is the redirect symbol
-- - & changes redirect's output from a file to a file descriptor (in this case standard out)
-- & the trailing & sends the process to the background
do shell script "cd /Users/jessefrohlich/Documents/Minecraft/" & server_name & " java -Xmx1024M -Xms1024M -jar minecraft_server.jar > /dev/null 2>&1 &"
You can add a condition to your Applescript to have it "ignore application responses", and it will then go on to whatever else is in your applescript, including quitting it.
The Apple Site has details: https://developer.apple.com/library/mac/#documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_control_statements.html
try
set logindetails to (do shell script "curl google.com")
on error
display dialog "Unable to connect."
quit
end try
When I put my network down, it gives me an error saying "unable to resolve host" and it never quits the application rather it stays in the background.
Is there a proper way to quit the application when unable to connect to the server or any other command to force quit or suppress and continue.
The correct way to end a running script is to use return:
try
set logindetails to (do shell script "curl google.com")
on error
display dialog "Unable to connect."
return
end try
See the AppleScript Language Guide’s pertinent section.
I use error number -128:
try
set logindetails to (do shell script "curl google.com")
on error
display dialog "Unable to connect."
error number -128
end try