如何使用 Applescript 检查 Chrome 是否在隐身模式下运行?

Rak*_*yal 3 macos applescript google-chrome

是否可以查明 Chrome 是否在隐身模式下运行?

if application "Google Chrome" is running then
    tell application "Finder" to display dialog "Chrome is running"
    // --> some condition here to check if it is in incognito ?
       tell application "Finder" to display dialog "Chrome is running in INCOGNITO mode"
end if
Run Code Online (Sandbox Code Playgroud)

另外,我希望这个脚本继续运行。这意味着一旦用户以隐身模式打开 Chrome,我就会显示警报。像这样:

set chromeRunning to false
repeat until application "Google Chrome" is running

    if not chromeRunning then
        tell application "Finder" to display dialog "Chrome is started in INCOGNITO mode"
        set chromeRunning to true
        #may be quit the script now..
    end if
    delay 10
end repeat
Run Code Online (Sandbox Code Playgroud)

如果这是正确的方法?

Sho*_*rKo 5

我不知道你为什么要把Q移到另一个论坛。这是一个关于使用 Applescript 的好问题。模式是每个窗口的属性!一个使用它关闭所有浏览器窗口的小例子:

tell application "Google Chrome"
    close (every window whose mode is "incognito")
end tell
Run Code Online (Sandbox Code Playgroud)

要保持脚本运行,您必须将其保存为应用程序,并选中运行处理程序后保持打开状态。在脚本中,您需要定义on idle-handler:

on idle
    -- do your stuff
    -- repeat after 10 seconds
    return 10
end idle
Run Code Online (Sandbox Code Playgroud)

把所有的东西放在一起我们得到这样的东西:

on idle
    if application "Google Chrome" is running then
        tell application "Google Chrome"
            set incognitoWindows to (every window whose mode is "incognito")
        end tell

        if (count of incognitoWindows) > 0 then
            activate
            display dialog "Chrome is running in incognito mode!"
        end if
    end if

    -- repeat after 10 seconds
    return 10
end idle
Run Code Online (Sandbox Code Playgroud)

玩得开心,迈克尔/汉堡