I would like to toggle between Enable/Disable in my applescript command. Can't seem to identify what's wrong with this script. It's not switching its states (toggling) when I run the same command again:
if {"disabled"} contains (do shell script "spctl --status") then
set theAllow to "enable"
else
set theAllow to "disable"
end if
do shell script (("sudo spctl --master-" & theAllow) as string) with administrator privileges
enter image description here
Looking at the source code for spctl, the spctl --status command will either return assessments enabled or assessments disabled. However, if it's disabled, then do shell script "spctl --status" exits with error "assessments disabled" number 1 and you'll need to account for it in some manner.
do shell script "spctl --status"
--> error "assessments disabled" number 1
So, to keep things simple, the following example AppleScript code simply toggles the state of spctl based on it status:
if (do shell script "spctl --status; exit 0") is "assessments disabled" then
do shell script "spctl --master-enable" with administrator privileges
else
do shell script "spctl --master-disable" with administrator privileges
end if
Example output when spctl is disabled while using ; exit 0 to handle error "assessments disabled" number 1:
do shell script "spctl --status; exit 0"
--> "assessments disabled"
do shell script "spctl --master-enable" with administrator privileges
As you can see, by using ; exit 0 after spctl --status, it stops error "assessments disabled" number 1 and allows the script to proceed.
There certainly is more then one way to trap the error, however, this example AppleScript code, that simply toggles the state of spctl, I've shown is simple and straight forward without the need to be more verbose with its error handling.
Otherwise you'll have to code it in a different manner, using a try statement wrapped around the do shell script "spctl --status command and an on error handler.
you could swap the 2 items
if (do shell script "spctl --status") contains "disabled"
assuming the returned string is "assessments disabled", and there's only 1 item to check.
Related
I'm trying to run a simple code from applescript.
It use blueutil, a command-line utility that can query Bluetooth’s status (on or off) / turn it on / turn it off.
I try this code from rob cottingham:
tell application “Terminal”
do shell script “/usr/local/bin/blueutil status”
set _Result tothe result
if _Result is “Status: on” then
do shell script “/usr/local/bin/blueutil off”
endif
if _Result is “Status: off” then
do shell script “/usr/local/bin/blueutil on”
endif
endtell
Without success.
If i clean all and only keep the lines about turning off or on, it works though.
Cleanest code I seems to get is:
tell application "Terminal"
do shell script "/usr/local/bin/blueutil status"
set theResult to the result
if "result" is "Status: on" then
do shell script "/usr/local/bin/blueutil off"
end if
end tell
But still doesn't work.
Maybe it's about using the result of the query as a variable?
I'm really not a professional it as you probably guessed, so any help will be appreciated !
Thanks,
Christophe.
First of all you don't need Terminal.app at all.
Second of all there is no argument status, to get the power state write:
set powerStatus to do shell script "/usr/local/bin/blueutil -p" as boolean
The result is true or false.
To toggle the power state write:
do shell script "/usr/local/bin/blueutil -p toggle"
To set the power state to on:
do shell script "/usr/local/bin/blueutil -p on"
To set the power state to off:
do shell script "/usr/local/bin/blueutil -p off"
Yes, it's just one line respectively.
And you can get the help message showing the man page.
set helpText to do shell script "/usr/local/bin/blueutil -h"
I would like to know if it is possible to ignore a shell script command
and continue running the rest of the code, for example if the following
command return me nothing in the terminal and I continue to the next command.
EDIT:
I tried: 2>/dev/null
try
do shell script "kextstat|grep -y appleintel >/tmp/Intel.txt" with administrator privileges
on error
display alert "blabla..."
end try
end showIntel:```
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
You have to excuse me if I use the wrong language here of if I'm asking an obvious but that is, after all, why I'm here.
I'm just getting to grips with shell scripting and have written a small script that is "Run as a custom command instead of my shell" to make things a little easier for the things I might want to do. Here's what I've got.
#
# Custom console startup script.
#
path_to_scripts=~/Scripts
echo "Hello $USERNAME, what would you like to do?"
echo "Options:"
echo "-l Continue in local machine"
echo "-s Connect to server"
read response
case $response in
"l") echo "Contunie within local machine.";;
"s") $path_to_scripts/connect_to_server;;
*) echo "Invalid command. Exiting.";;
esac
So my terminal starts up with this script and if I select 's' it runs the 'connect_to_server' script fine and connects then I'm in!
However when I enter an invalid command, or key in 'l' to exit and continue as normal the console says 'The child process exited normally with status 0.'
I know that it has just quit and the script has exited but what I want to do is just run the default shell so that I am then in my local machine at ~, as if id just started up console with default settings. What do I need to run in order to do this?
Run exec "$SHELL" to replace the current process with your normal shell.
I need to be able to run AppleScript in a shell script. I am using "AppleScript Runner" in order to be in interactive mode, so that dialogs etc. are supported. I've got it working, but I need to get the exit status of the AppleScript Runner app back to the shell, so I can see if there were any errors in the script.
Here is my shell script:
output=$(/usr/bin/osascript << EOT
tell application "AppleScript Runner"
do script "somescript.scpt"
end
EOT)
status=$?
Here my variable $status only ends up with the exit status of the osascript command (which will be 0 whether or not somescript.scpt actually ran successfully), and not the exit status of the app AppleScript Runner.
Does any one know how I might accomplish this?
Thanks!
The -e flag prints errors to stderr and is the default. So you just need to read stderr.
This answer might help you if you aren't familiar with that:
bash variable capture stderr and stdout separately or get exit value
EDIT: Added sample code.
error=`osascript -e 'tell app "Finder" to adtivate' 2>&1`
echo $error
The above on my system captures the error messages.