在OSX中,当您按下窗口栏顶部最左侧的红色按钮关闭Firefox(或任何其他应用程序)时,应用程序将关闭但不会退出。它坐在码头上有一个圆点标志。
我需要一个脚本来识别Firefox的类似情况。 一种方法是计算firefox中打开的选项卡的数量。对?如果是零,则没有打开标签。
脚本可以是bash(首选)或applescript(从bash运行)osascript -e 'commands'
。
答案 0 :(得分:3)
也许这会指向正确的方向:
osascript -e 'tell application "Firefox" to count (every window whose (closeable is true))'
我尝试了窗口的属性(visible is true or miniaturized is true)
,但是当用户隐藏Firefox时,两者都无效。
没有属性 tab 可用,但我认为,永远不会有没有窗口的标签。
<强>更新强> osascript似乎激活所有目标应用程序来编译给定的AppleScript。为了防止它,我们必须使用一点点黑客让osascript不知道我们最终定位的应用程序......
osascript <<FOO
> tell application "System Events" to set fireFoxIsRunning to ((count of (name of every process where name is "Firefox")) > 0)
> if fireFoxIsRunning then
> set targetApp to "Firefox"
> tell application targetApp to count (every window whose (closeable is true))
> else
> return 0
> end if
> FOO
玩得开心,迈克尔/汉堡
答案 1 :(得分:0)
如果有其他人感兴趣的话,我会发布最终剧本...会有所帮助。 我在我的包中使用脚本作为预安装脚本。有趣的是,GUI确认工作非常好。
function offOn() {
var ballCount = 1;
var ballsCount = 1;
for (var i = 0; i < 200; i++) {
$(".grid").append("<div class='grid-inner' id='" + i + "'>sss</div>");
if (i == 45 || i == 100 || i == 82) {
$('#'+i).append("<div class='ball' id='ball" + ballCount + "'></div>" + "<div class='arrow-down' id='arrow'></div>" + "<p class='ex'><b>" + ballsCount + "</b></p>")
ballCount++;
ballsCount++;
}
}
}
正如@ShooTerKo所提到的,## 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
激活应用程序来编译脚本,并首先查询osascript
以感知它是否正在运行,这是我在Apple reference中找到的替代方案被称为强制。这将在没有系统事件帮助的情况下充当上述脚本,即如果它没有运行则不会运行Firefox。
当我在脚本编辑器中尝试代码时,它可以工作。但是当如上所述在bash脚本中运行此代码时,它会启动Firefox,然后始终返回1(打开窗口)。
"System Events"
然后,
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