I'm trying to set up Macs for extreme novice computer users so everything is ready out of the box with the exception of their user account.
The problem with this is there are very hard things to install without access to the user account such as Safari extensions. I have Chrome and Firefox set up - I want to install a couple of basics for them and even clicking the install button when they first load up is going to confuse people, I want it totally hands off.
I'm trying to get Adblocker installed. So far I get it so that the script works perfect when run from terminal but when run as a LaunchAgent it gets the install extension window on Safari requiring user input and stops.
It shouldn't stop as originally I had AppleScript click the menu and then I decided to enable full tab control on keyboard and have cliclick press tab and space (as the user can) which installs too.
As I mentioned, run as a script this works perfectly but for some reason the script stalls when Launchagent runs it on login as soon as the extension install window appears.
Is this a bug, is it an extreme security feature? Do i have to adapt my plist to tell Launchagent not to change the way the script works if windows pop up, do I need more in my script for it to behave differently when run by launchd?
Here is the main part, adapted from bits online.
# HARDCODED VALUE FOR "PATHTOEXTENSION" IS SET HERE
pathToExtension="/ext/safari.safariextz"
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 4 AND, IF SO, ASSIGN TO "PATHTOEXTENSION"
if [ "$4" != "" ] && [ "$pathToExtension" == "" ];then
pathToExtension=$4
fi
# Error if variable appName is empty
if [ "$pathToExtension" == "" ]; then
echo "Error: No value was specified for the pathToExtension variable..."
exit 1
fi
# Turn on full tab control for keyboard
defaults write NSGlobalDomain AppleKeyboardUIMode -int 3
### Launch Safari if not open
osascript -e 'tell application "Safari"
activate
end tell'
### Prompt the user to install the extension given @ $4
osascript -e 'tell application "Safari"
open "'"$pathToExtension"'"
tell application "System Events"
tell process "Safari"
set frontmost to true
end tell
end tell
end tell'
#Run cliclick to press tab and space on the keyboard to install extension
sleep 0.7s
cliclick kp:tab kp:space
### Close Safari
osascript -e 'tell application "System Events"
if ((name of processes) contains "Safari") then
tell application "Safari" to quit
end if
end tell'
### Open Getting Started PDF
osascript -e 'tell application "Preview"
open "/Applications/Tutorials/Quick Start Guide.pdf"
end tell'
### Cleanup - delete launch agent so it doesn't run on next boot
rm ~/Library/LaunchAgents/com.ext.install.plist
### Exit silently, as will error if exists & if times out for whatever reason
exit 0
And the plist to run the script is this...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.ext.install</string>
<key>Nice</key>
<integer>20</integer>
<key>ProcessType</key>
<string>Interactive</string>
<key>ProgramArguments</key>
<array>
<string>/Users/Shared/install-ext.command</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/com.ext.install.err</string>
<key>StandardOutPath</key>
<string>/tmp/com.rxt.install.out</string>
</dict>
</plist>
No errors reported in the logs by the way. Hopefully someone can offer some insight.
Saving this:
install-ext.command
# HARDCODED VALUE FOR "PATHTOEXTENSION" IS SET HERE
pathToExtension="/Users/Shared/ext/wpa.safariextz"
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 4 AND, IF SO, ASSIGN TO "PATHTOEXTENSION"
if [ "$4" != "" ] && [ "$pathToExtension" == "" ];then
pathToExtension=$4
fi
# Error if variable appName is empty
if [ "$pathToExtension" == "" ]; then
echo "Error: No value was specified for the pathToExtension variable..."
exit 1
fi
### Prompt the user to install the extension given @ $4
osascript -e 'tell application "Safari"
activate
open "'"$pathToExtension"'"
end tell
delay 2
tell application "System Events"
tell process "Safari"
click button "Install" of window 1
end tell
end tell'
### Exit silently, as will error if exists & if times out for whatever reason
exit 0
I tell Safari to click the Install button. Rather than use a third party app.
Chmod'd the file to allow it to be an executable.
Used your LaunchAgent as is.
Manually loading the LaunchAgent ran the code. But as expected I also got the prompt to authorise install-ext.command to control the computer.
So I had to auth. That first.
Then any subsequent load of the LaunchAgent worked as desired including on login.