Closing chrome incoginto window via terminal or applescript - macos

Was hoping someone could help me with code that would detect whether an incognito window (or multiple incognito windows) was open, and if so close it/them.
I found the below code which will achieve this if the only window open was incognito, but unsure how to implement a loop to check all open windows and close only those that are incognito.
tell application "Google Chrome"
if exists window 1 then
if mode of window 1 = "incognito" then
-- insert your code here
end if
end if
end tell

You don't need a repeat loop to close all the incognito windows. You can do it with an "every window whose mode is" qualifier. Like so:
tell application "Google Chrome" to close (every window whose mode is "incognito")

Related

Applescript can't access tabs in private windows

So I wrote a small script that closes all tabs with a certain URL. It works fine in every browser I tried, except that it can't access incognito tabs. This script for example works on multiple windows, multiple tabs but doesn't work with incognito mode. Is there any way to also address incognito tabs?
tell application "Safari"
close (tabs of windows whose URL contains "ecosia")
end tell
EDIT: It only works, if there also is an open tab with "ecosia" in a normal, non-private tab.
To work around the code shown in your question not working when the target URL only exists in a tab of a private window in Safari, the following example AppleScript code, albeit kludgy, will work:
tell application "Safari"
set myTabs to ¬
(tabs of windows whose URL contains "ecosia")
repeat with aTab in myTabs
try
close aTab
end try
end repeat
end tell

Applescript/Bashscript to activate incognito google chrome running in background

I know how to open google chrome in incongito mode:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --incognito
Also I found how to close incognito google chrome:
tell application "Google Chrome"
close (every window whose mode is "incognito")
end tell
Also from this link
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
Using above script I tried this:
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
tell application "Google Chrome" to activate
end if
But this did not activate the incognito google chrome, it is activating the normal mode google chrome.
I am running both normal and incognito chromes.
(incognito is playing songs, and working on normal chrome).
How can we activate incognito google chrome when also running normal mode google chrome?
Update
Borrowing idea from this link
I got this:
tell application "System Events" to tell process "Google Chrome"
perform action "AXRaise" of window 2
set frontmost to true
end tell
But this kills second google chrome, I need to activate the incognito,
even though there may be multiple normal google chromes running.
Related links:
How to check is Chrome is running in incognito mode using Applescript?
Applescript - Google Chrome activate a certain window
I'm hoping to have interpreted your situation correctly, having understood the following:
You're running a single instance of Google Chrome, which has several open windows, at least one of which is in incognito mode;
You wish to switch application focus to Google Chrome, but the methods you have tried typically bring the normal windows into the foreground, leaving the incognito windows at the back;
You want the switch to Google Chrome, essentially, to do the opposite, which would be to bring the incognito windows to the front, and leave the normal windows remaining open but at the back.
If I've interpreted you correctly, then:
tell application "Google Chrome"
activate
set index of (every window whose mode is "incognito") to 1
end tell
System info: AppleScript version: "2.7", system version: "10.13.6"
The activate command does what you would expect, and brings focus to Google Chrome's normal windows. The next line sets the index of each incognito window to 1, displacing the one before to one place behind. The normal windows, thus, end up at the rear as a natural consequence of this process.
The great thing about it is that, quite surprisingly, Google Chrome executes these two commands in a seamless fashion (under regular conditions). So there's no observable flicker or visible rearranging of windows: the incognito window that ends up frontmost simply appears at the front at the same time as Google Chrome comes into the foreground.
I'm not going to guarantee that will be the case across all systems or with a large number of open windows. But, on my pretty low-spec Macbook 12", the switch was fluid when tested with 4 normal windows plus 4 incognito windows.
I think the order of the incognito windows ends up being the reverse of what it was, but if it's important to preserve relative order, then you can simply execute the set index... command twice, which will reverse the reversed order. This, however, does produce the tiniest of visible transitions as we switch from the front incognito window to the rear one.

Apple script to activate a window that pops up within Firefox accurately

I have written this piece of code on my MAC OS X 10.10.5 to automate keystrokes on a window that pops up within Firefox.
It does not work reliably and, in many cases, performs the keystrokes on the window in the background. I have tried increasing the delay but it does not seem to be related to timing. The problem, instead, seems to be that the wrong Firefox window is activated.
This is my code. Any ideas on how I can fix it to work reliably.
set myBrowser to "/Applications/Firefox.app"
tell application myBrowser
activate window 2
end tell
tell application "System Events"
keystroke tab
delay 2
keystroke enter
end tell
If Firefox opens up a pop-up window, I believe that window now becomes window 1. The system events will be sent to that front pop-up window. Try using this code
activate application "Firefox"
tell application "System Events"
delay 2
key code 48
delay 1
key code 36
end tell

Applescript to open an application in full-screen mode?

I'm trying to program Alfred to open my Terminal, Sublime Text, and Chrome with a workflow.
I would like for my terminal to open normally as a window, but I've been trying to get Chrome and Sublime to open full screen.
I was able to get Chrome to open up in full screen mode with:
on alfred_script(q)
tell application "Google Chrome"
tell window 1 to enter presentation mode
end tell
end alfred_script
However, this did not translate to work with my Sublime Text.
What am I missing here?
Another way to do this assuming you have not changed the default keyboard shortcut for "Enter Full Screen" is simply to have System Events invoke that shortcut (⌃⌘F). As with the other approach I've seen to doing this (changing the value of AXFullScreen—see mklement0's answer here for a thorough discussion of this method), this requires making the relevant window active.
For instance, to toggle the full-screen state of the frontmost window in Safari, run:
tell application "Safari" to activate
tell application "System Events"
keystroke "f" using {command down, control down}
end tell
As found here (i need an applescript to open safari in full screen an to hide the toolbar on mavericks). The make new document line prevents the can't get window 1 error by opening a new tab if one has not previously been opened.
tell application "Safari"
make new document
activate
delay 3
tell application "System Events" to tell process "Safari"
set value of attribute "AXFullScreen" of window 1 to true
end tell
end tell

Break Active Finder Tab Into New Window with Applescript

I am trying to write an applescript to break the active Finder tab into a new window. I am able to do this with Chrome using:
on moveActiveTabToNewWindow()
tell application "Google Chrome"
set theURL to URL of active tab of window 1
close active tab of window 1
make new window
set URL of active tab of window 1 to theURL
end tell
end moveActiveTabToNewWindow
However, as far as I can tell Finder tabs are not accessible via Applescript. Is this possible? I'm on OS X Mavericks 10.9.2.
Manipulating tabs is not accessible directly but you could do it manually. In other words we can close that tab and then open a new window and replicate what you saw in that tab, just like you did in your Google Chrome example.
Here's some code. This code is basic. Just like I got the "target" property from the Finder window, you could get other properties too and replicate them in the new window to really do a more complete job of replicating the tab. You'll probably want to duplicate the bounds and view options at least.
Good luck.
-- get the target of the front tab
tell application "Finder"
activate
tell window 1 to set t to target
end tell
-- close that tab
tell application "System Events"
keystroke "w" using command down
end tell
-- make a new window and set its target
tell application "Finder"
set w to make new Finder window at front
tell w to set target to t
end tell

Resources