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
Related
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.
When I run this script from Alfred (using the text "nsafari", and Safari is quit (not in the dock), two Safari windows will pop up. When I run it from the Script Editor, it will sometimes open up two windows, but sometimes not. (This also happens with my new Safari window script as well).
if application "Safari" is running then
tell application "Safari"
make new document
activate
end tell
else
tell application "Safari" to activate
end if
Why does it open two windows only from Afred? And how do I make it only create one?
I don't know why, but after two years, the problem I reported above does not seem to happen any more...
(and yes this is not a very satisfying answer)
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")
I have this code I can keep running inside Script Editor. But it causes my laptops fans to scream! So I am not sure is this right way to do this?
repeat until application "Google Chrome" is not running
if application "Google Chrome" is running then
tell application "Google Chrome"
set (URL of every tab of every window where URL is equal to "facebook.com") to "twitter.com"
end tell
end if
end repeat
Other question is that why my code isn't working? I would like to use this for redirecting from spesific urls to another. But even this implementation doesn't work.. If I open facebook.com it doesn't redirect to twitter.com.
Any help is appreciated.
NOTE: YES, I want to use applescript for this so focus on it, pls :)
When an application appears to work, but doesn’t seem to be matching its conditions, it is always helpful to look at the actual values being checked. In this case, open the URL you think should be matched, and then get that URL to look at.
tell application "Google Chrome"
get URL of tab 1 of window 1
end tell
When I entered “Facebook.com” in a new window in Chrome, and then ran the above script, what I found was https://www.facebook.com/?_rdr=p.
When I replaced “Facebook.com” in your code sample with https://www.facebook.com/?_rdr=p, the script now had an affect; it redirected to Chrome’s about page.
Taking a cue from the necessity of using the full URL in the condition, I switched “twitter.com” to “http://twitter.com/”.
The script now performs as I understand you intended it to: every tab in every window that matches https://www.facebook.com/?_rdr=p gets redirected to twitter.com.
repeat until application "Google Chrome" is not running
if application "Google Chrome" is running then
tell application "Google Chrome"
set (URL of every tab of every window where URL is equal to "https://www.facebook.com/?_rdr=p") to "http://twitter.com/"
end tell
end if
end repeat
Applescript and Google Chrome are matching your conditional’s text against the full URL, so your conditional has to use the full URL as well.
#vadian’s note about using an idle handler is a very good one. See this Hourly Pop-up Alert question for ideas on using an idle handler. Something like this should work:
on idle
if application "Google Chrome" is running then
tell application "Google Chrome"
set (URL of every tab of every window where URL is equal to "https://www.facebook.com/?_rdr=p") to "http://twitter.com/"
end tell
end if
--number of seconds to wait for the next check
return 1
end idle
Save as application, stay open after run handler.
What I have set up is a AppleScript that asks you what website you want to open with a couple options, to make this simple, I only list one option, Google. This is the current code:
if the_button = "Google" then
site = "http://www.google.com"
tell application "Google Chrome"
launch
tell window 1
make new tab with properties {URL:site}
repeat while loading of active tab
delay 0.1
end repeat
end tell
activate
end tell
It opens a new tab in Google Chrome, but doesn't put anything in the URL bar or reload/load or anything. I've tried this: Script Safari 5.1 to open a tab But, nothing's working. I'm using Mac OS X Lion 10.7.4 and Google Chrome version 21.0.1180.79. Please help!
It works for me, but not when Chrome doesn't have open windows or if all windows are minimized. You could add a reopen command to open a new default window or unminimize a window in those cases.
set site to "http://www.google.com"
tell application "Google Chrome"
reopen
activate
tell window 1
make new tab with properties {URL:site}
end tell
end tell
Another option might be to use something like do shell script "open " & quoted form of "http://google.com" & " -a Google\ Chrome". It's often closer to the default behavior of browsers.
Can't you just do this?
tell application "Google Chrome"
activate
set theSite to "http://www.google.com/"
open location theSite
end tell
The “open location” command is part of Standard Additions and is available to all Mac apps. It’s the default way to open a website in any Mac app. You might notice that Safari’s AppleScript dictionary doesn’t even include a way to open a website — it simply uses the standard “open location” command which you can find in the Standard Additions dictionary.