How to check is Chrome is running in incognito mode using Applescript?

[亡魂溺海] 提交于 2019-12-06 10:48:58

问题


Is it possible to find out if the Chrome is running in incognito mode?

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

Also, I want this script to keep running. That means as soon as user opens Chrome in incognito mode I will show alert. Like this:

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

If this the correct approach?


回答1:


I don't know why you want to move the Q to another forum. It is a nice question about using Applescript. The mode is a property of each window! A little example to close all browser windows using it:

tell application "Google Chrome"
    close (every window whose mode is "incognito")
end tell

To keep a script running you have to save it as an Application with Stay open after run handler checked. Inside the script you need to define the on idle-handler:

on idle
    -- do your stuff
    -- repeat after 10 seconds
    return 10
end idle

Putting all together we get something like:

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

Have fun, Michael / Hamburg




回答2:


This will let you know if there is an Incognito window open:

tell application "Google Chrome"

    set incognitoIsRunning to the (count of (get every window whose mode is "incognito")) is greater than 0

end tell

if (incognitoIsRunning) then
    say "Shh"
end if

and to keep the scipt running (checking periodically) look into an on idle handler




回答3:


This is what I ended up coding. Check ShooTerko's answer, that is better.

set iAm to 0
set infinite to 0
repeat while (iAm = infinite)
    if application "Google Chrome" is running then

        tell application "Google Chrome"
            set totalAppWindows to count of window
            set currentWindow to 1
            repeat totalAppWindows times
                #say (mode of window currentWindow) as text
                if (mode of window currentWindow) as text = "incognito" then
                    say "incognito"
                end if
                set currentWindow to currentWindow + 1
            end repeat

        end tell


    end if
    delay 10
end repeat


来源:https://stackoverflow.com/questions/26916480/how-to-check-is-chrome-is-running-in-incognito-mode-using-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!