я выкладываю окончательный сценарий, если кто-то заинтересован ... будет полезно. Я использую сценарий в качестве предварительного скрипта в моем пакете. Интересно, что GUI Confirmation работает очень хорошо.
## Ask the user to quit firefox, in case it is running.
## fireFoxIsRunning status,
# -1 = Firefox is not running
# 0 = Running without any open (0) window
# 1 = window count return error => running with 1 or more windows. (sometime I got error: "every window whose closeable = true doesn’t understand the “count” message".
function firefox_status() {
firefoxRunning=$(osascript \
-e 'try ' \
-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 return number of (windows whose closeable is true)' \
-e 'else' \
-e 'return -1' \
-e 'end if' \
-e 'on error errorMsg number errorNumber' \
-e 'return 1' \
-e 'end try')
echo firefoxRunning = $firefoxRunning
if [ $firefoxRunning -ne -1 ]; then
if [ $firefoxRunning -eq 0 ]; then
echo 'Firefox is in the background with no window and so quitting ...'
osascript -e 'quit app "Firefox"'
else
echo "Firefox is running with $firefoxRunning windows"
quiteValue=$(osascript \
-e 'set valueReturned to display dialog "Firefox is running. Please quit Firefox to continue installation of the Your Program." with title "Your Program" buttons {"Cancel Installation", "Quit Firefox"} default button 2' \
-e 'return button returned of valueReturned')
fi
else
echo "Firefox is not running"
fi
}
firefox_status
# quiteValue will be set only when dialog is shown (firefox is running).
# So for empty value return exit code 0
if [ -z "$quiteValue" ]; then
exit 0
fi
if [ "$quiteValue" == "Quit Firefox" ]; then
osascript -e 'quit app "Firefox"'
elif [ "$quiteValue" == "Cancel Installation" ]; then
exit 1
fi
Как отметил @ShooTerKo, что osascript
активировать приложение для компиляции сценария, и первый запрос "System Events"
толку, если он работает, то альтернатива, что я нашел в Apple reference, что называется, как принуждение. Это будет действовать, как указано выше, без помощи System Events, т. Е. Не будет запускать Firefox, если он не запущен. Когда я пробовал код в редакторе скриптов, он работает. Но когда вы запускали этот код в сценарии bash, как описано выше, он запускает Firefox, а затем всегда возвращает 1 (открытое окно).
tell application "Firefox"
if it is running then
set num to count (every window whose (closeable is true))
return num
else
return -1
end if
end tell
, а затем,
if [ $firefoxRunning -ne -1 ]; then
if [ $firefoxRunning -eq 0 ]; then
echo 'Firefox is in the background with no window and so quitting ...'
osascript -e 'quit app "Firefox"'
fi
fi
Может быть, лучший способ не позволить Firefox начать, если он не работает: 'если приложение«Firefox»работает, то сказать, приложение«Firefox»считать (каждый окно которого (closeable is true)) – ShooTerKo
да. это лучше. – karim
, но это дает ошибку, osascript -e ', если приложение «Firefox» запущено, тогда скажите приложению «Firefox» подсчитать (каждое окно, чье (closeable is true)) end if', когда я запускаю bash. – karim