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
Related
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
I created a small script to control httpd/php/mysql and used XCode to create and link an interface to this script. Everything works ok. The problem is I'm trying to make the application window close after the red circle button is clicked, and is not happening. The app keeps running and only the window is closed.
I have tried using:
applicationShouldTerminateAfterLastWindowClosed_(sender)
The whole script is this:
script AppDelegate
#### PROPERTY LIST ####
property parent : class "NSObject"
property startApache : missing value
property restartApache : missing value
property stopApache : missing value
property editConfig : missing value
property editVHosts : missing value
property openDir : missing value
property resetConfig : missing value
property editPHP : missing value
property resetPHPConfig : missing value
property startMYSQL : missing value
property stopMYSQL : missing value
property restartMYSQL : missing value
property openDirMYSQL : missing value
property resetMYSQL : missing value
#### APACHE CMDs ####
on startApache_(sender)
do shell script "/usr/sbin/apachectl start" with administrator privileges
end startApache_
on restartApache_(sender)
do shell script "/usr/sbin/apachectl restart" with administrator privileges
end restartApache_
on stopApache_(sender)
do shell script "/usr/sbin/apachectl stop" with administrator privileges
end stopApache_
on editConfig_(sender)
do shell script "open -a /Applications/BBEdit.app /private/etc/apache2/httpd.conf"
end editConfig_
on editVHosts_(sender)
do shell script "open -a /Applications/BBEdit.app /private/etc/apache2/extra/httpd-vhosts.conf"
end editVHosts_
on openDir_(sender)
do shell script "open /Library/WebServer/Documents/"
end openDir_
on resetConfig_(sender)
display dialog "Are you sure you want to reset the httpd.conf to it's default settings?\n\n This cannot be undone!" with icon stop with title "Reset Configuration File"
do shell script "/usr/sbin/apachectl stop" with administrator privileges
do shell script "cp /private/etc/apache2/httpd.conf.pre-update /private/etc/apache2/httpd.conf ; cp /private/etc/apache2/extra/httpd-vhosts.conf.default /private/etc/apache2/extra/httpd-vhosts.conf" with administrator privileges
end resetConfig_
#### PHP CMDs ####
on editPHP_(sender)
do shell script "open -a /Applications/BBEdit.app /etc/php.ini"
end editPHP_
on resetPHPConfig_(sender)
display dialog "Are you sure you want to reset the php.ini to it's default settings?\n\n This cannot be undone!" with icon stop with title "Reset Configuration File"
do shell script "cp /etc/php.ini.default /etc/php.ini" with administrator privileges
end resetPHPConfig_
#### MYSQL CMDs ####
on startMYSQL_(sender)
do shell script "/usr/local/bin/mysql.server start" with administrator privileges
end startMYSQL_
on restartMYSQL_(sender)
do shell script "/usr/local/bin/mysql.server restart" with administrator privileges
end restartMYSQL_
on stopMYSQL_(sender)
do shell script "/usr/local/bin/mysql.server stop" with administrator privileges
end stopMYSQL_
on openDirMYSQL_(sender)
do shell script "open /usr/local/var/mysql/"
end openDirMYSQL_
on resetMYSQL_(sender)
display dialog "Are you sure you want to reset ALL MySQL databases to their default?\n\n This cannot be undone!" with icon stop with title "Reset All Databases"
display dialog "Type the new root password" default answer "" with hidden answer
set root_pass to text returned of result
if root_pass = "" then
display dialog "Root password cannot be blank!" buttons {"Cancel"} with icon Caution
error number -128
else
display dialog "MySQL DB will be recriated in the background.\n\nClick on reset and wait." buttons {"Cancel","Reset"}
do shell script "/usr/local/bin/mysql.server stop" with administrator privileges
do shell script "rm -rf /usr/local/var/mysql" with administrator privileges
do shell script "/usr/local/bin/mysqld --initialize-insecure"
do shell script "/usr/local/bin/mysql.server start"
do shell script "/usr/local/bin/mysqladmin -u root password '"&root_pass&"'"
display dialog "All done!\n\nDatabase reseted." buttons {"Ok"} with title "MySQL Reset Utility"
end if
end resetMYSQL_
on applicationWillFinishLaunching_(aNotification)
end applicationWillFinishLaunching_
on applicationShouldTerminateAfterLastWindowClosed_(sender)
return true
end applicationShouldTerminateAfterLastWindowClosed_
on applicationShouldTerminate_(sender)
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
As #vadian said: Have a look if the File's Owner's delegate outlet is connected to App Delegate (your script) correctly:
BTW this is the default after creating a new CocoaApplescript-App. I just copied the handler
on applicationShouldTerminateAfterLastWindowClosed_(sender)
return true
end applicationShouldTerminateAfterLastWindowClosed_
into the script and everything worked fine!
Have fun, Michael / Hamburg
I've searched around but couldn't quite find anything to fit my problem.
I want to create a script to replicate the following:
Open Terminal
Execute the following command:
sudo kextunload /System/Library/Extensions/AppleHDA.kext
Then have it enter my OSX admin password for me.
Then execute the following:
sudo kextload /System/Library/Extensions/AppleHDA.kext
I'm completely new to applescript, so hoping someone can help me out.
Thanks!
The hint in a comment on the question is correct (in [Apple]Script Editor, select File > Open Dictionary..., select StandardAdditions.osax, then search for do shell script to see the complete syntax), but it's important to note that do shell script will NOT open a Terminal window; instead, it'll run the shell command hidden and return its result - which is generally preferable:
do shell script's return value is the shell command's stdout output.
If the shell command returns a non-zero exit code, AppleScript will throw an error and the error message will contain the command's stderr output.
To run commands with administrative privileges, you have 2 options:
[Recommended] Let AppleScript display a password prompt:
set shCmds to "kextunload /System/Library/Extensions/AppleHDA.kext;
kextload /System/Library/Extensions/AppleHDA.kext"
# This will prompt for an admin password, then execute the commands
# as if they had been run with `sudo`.
do shell script shCmds with administrator privileges
[Not recommended for security reasons] Pass the password as an argument:
set shCmds to "kextunload /System/Library/Extensions/AppleHDA.kext;
kextload /System/Library/Extensions/AppleHDA.kext"
# Replace `{myPassword}` with your actual password.
# The commands will run as if they had been executed with `sudo`.
do shell script shCmds ¬
user name short user name of (system info) password "{myPassword}" ¬
with administrator privileges
As stated, if something goes wrong - whether it is because of an invalid password or a canceled password dialog or the shell commands returning a non-zero exit code - a runtime error is thrown.
Here's an example of trapping it and reporting it via display alert.
try
do shell script shCmds with administrator privileges
on error errMsg number errNo
display alert "Executing '" & shCmds & "' failed with error code " & ¬
errNo & " and the following message: " & errMsg
return
end try
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
I'm working with an Applescript that I'm trying to make, after it does a command to check back results of a command, but when I execute it, it prompts a message (in AppleScript Editor); Expected expression but found “error”..
How can I fallback and check if the command is same as The command exited with a non-zero status. so I can do something else if it's the same as the error message?
do shell script "echo \"stats\" | nc localhost 11211" password "~password~" with administrator privileges
if error = "The command exited with a non-zero status." then
display dialog "Returned zero"
else if result = "The command exited with a non-zero status." then
display dialog "Returned zero"
else if result = "" then
do shell script "memcached -d -l 127.0.0.1 -p 11211 -m 64"
display dialog "Memcached Started"
else
do shell script "killall memcached" with administrator privileges
display dialog "Memcached Stopped"
end if
EDIT: Updated version
set error to do shell script "echo \"stats\" | nc localhost 11211" password "~password~" with administrator privileges
if error = "The command exited with a non-zero status." then
do shell script "memcached -d -l 127.0.0.1 -p 11211 -m 64"
display dialog "Memcached Started"
else
do shell script "killall memcached" with administrator privileges
display dialog "Memcached Stopped"
end if
Your particular problem is in using the word "error" as a variable. Error is a special word to applescript so it can't be used as a variable.
However, even if you fix that problem your code still won't work properly. You catch error messages with a try block. Note that "theError" contains the error message...
try
do shell script "echo \"stats\" | nc localhost 11211" password "~password~" with administrator privileges
on error theError
if theError is "The command exited with a non-zero status." then
do shell script "memcached -d -l 127.0.0.1 -p 11211 -m 64"
display dialog "Memcached Started"
else
do shell script "killall memcached" with administrator privileges
display dialog "Memcached Stopped"
end if
end try