I am creating a command line tool for my application.
It uses pkgbuild and productbuild to create the package. Its a launchdaemon binary.
I have preinstall and postinstall scripts.
After installation, may be in postinstall script, I need to detect if Firefox is running, then a prompt to the user that (s)he need to restart Firefox.
Is it possible to do that? How?
some excerpt from the script, its part of the postinstall script.
.........
## Check if Firefox is running in the background with no tab/window open.
function restart_empty_firefox (){
echo "Restarting firefox if no tab is opened. "
firefoxRunning=$(osascript \
-e 'tell application "System Events" to set fireFoxIsRunning to ((count of (name of every process where name is "Firefox")) > 0)' \
-e 'if fireFoxIsRunning then' \
-e 'set targetApp to "Firefox"' \
-e 'tell application targetApp to count (every window whose (closeable is true))' \
-e 'else' \
-e 'return 0' \
-e 'end if')
if [ $firefoxRunning -eq 0 ]; then
echo 'Firefox is in the background with no window and so quitting ...'
osascript -e 'quit app "Firefox"'
else
##### show a dialog to the user to restart Firefox
fi
}
firefox_update # not shown here
restart_empty_firefox
.............
You can use the process status (ps) tool:
ps aux | grep '[F]irefox.app' | awk '/firefox$/ {print $2}'
Result:
82480
This tells us that Firefox.app is indeed running (process 82480).
EDIT: Since you seem to be leaning in the direction of using an osascript here's an example which asks the user to relaunch Firefox (putting full control in their hands):
#!/bin/bash
osascript <<'END'
set theApp to "Firefox"
set theIcon to "Applications:Firefox.app:Contents:Resources:firefox.icns"
tell application "System Events"
if exists process theApp then
display dialog "Warning: Mozilla " & theApp & " should be closed." buttons {"Continue"} with icon file theIcon default button 1
if the button returned of the result is "Continue" then
end if
end if
display notification "Installation complete" with title "Application Package" subtitle "Please relaunch " & theApp
delay 3
end tell
END
If your script has sufficient privileges then using quit would be recommended over blatantly killing off the process. Ultimately that's how it should be done, otherwise just ask the user to please quit and relaunch Firefox themselves — problem solved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With