How to close safari tabs by url using applescript - macos

I'm looking for a way to close specific tabs in safari by the url.
I would probably expand on something like this:
tell application "Safari"
repeat with t in tabs of windows
tell t
if name starts with "google.com" then close
end tell
end repeat
end tell
the problem here is that i haven't found a way how to get the url value of a tab and close them based on that.

You can get the URL from Safari based on the active tab.
tell application "Safari"
set w to first window
set t to current tab of w
display dialog (URL of t as string)
end tell
And then you could iterate over every tab/page like this :
tell application "Safari"
set windowCount to number of windows
repeat with x from 1 to windowCount
set tabCount to number of tabs in window x
repeat with y from 1 to tabCount
set thistab to tab y of window x
set foo to URL of thistab
if foo is not equal to "bar" then close thistab
end repeat
end repeat
end tell

Here is another method:
set closeURLs to {"http://www.yahoo.com", "http://www.apple.com"}
repeat with theURL in closeURLs
tell application "Safari" to close (every tab of every window whose URL contains (contents of theURL))
end repeat

Related

Applescript, "A class name can't go after this identifier error", what does it mean?

Someone tried to help me set up a keyboard macro using a program called keyboard maestro on macos to control youtube, his example had safari:
set tabURL to "YouTube.com"
tell application "Safari"
set windowsList to index of every window
repeat with i in windowsList
try
tell window i
set current tab to (first tab whose URL contains tabURL)
end tell
set index of window i to 1
exit repeat
end try
end repeat
end tell
It worked, I simply changed the name safari to google chrome, I put in this code:
set tabURL to "YouTube.com"
tell application "Google Chrome"
set windowsList to index of every window
repeat with i in windowsList
try
tell window i
set current tab to (first tab whose URL contains tabURL)
end tell
set index of window i to 1
exit repeat
end try
end repeat
end tell
it gave me this error message
2020-09-10 15:02:26 Execute an AppleScript failed with script error: text-script:165:176: script error: A class name can’t go after this identifier. (-2740)
what does it mean? any help appreciated.
tell application "Google Chrome"
repeat with thisWindow in windows
set cnt to 1
repeat with thisTab in (tabs of thisWindow)
if URL of thisTab contains "youtube.com" then
set active tab index of thisWindow to cnt
set index of thisWindow to 1
exit repeat
end if
set cnt to cnt + 1
end repeat
end repeat
end tell
this is the fix if anyone cares.

Apple script find and replace text

So, right now my code clicks on a textarea in safari then it adds a word using keystroke, however I was wondering if there is a way to find and replace a word in that area and also is there a way to replace a highlighted word without keystroke?
Current code:
Sorry its very very poorly formatted since I am pretty bad at applescript
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName
to clickID(theId)
tell application "Safari"
do JavaScript "document.getElementById('" & theId & "').click();" in document 1
end tell
end clickID -- All the code up to this point did is allow me to click on IDs and classes in safari.
tell application "Safari" to activate
tell application "System Events"
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"https://en.wikipedia.org/w/index.php? title=User:USERNAMEHERE/sandbox&action=edit"})
end tell
end tell
end tell
delay 2 -- All the code up to this point from the last comment did is open a new tab in safari.
repeat 5 times
set randomlist to {"1","2","3","4"} -- List of numbers that will be randomly selected and put in the texarea
set randomword to some item of randomlist
clickClassName("tool-button wikiEditor-toolbar-spritedButton", 0) -- This clicks the bold button on the sandbox page
tell application "System Events"
tell process "Safari"
keystroke randomword -- Uses keystroke to put replace the text inside the bold formatting area
end tell
end tell
delay 1
clickID("wpSave") -- Clicks save
delay 2
tell application "Safari" to set the URL of the front document to "https://en.wikipedia.org/w/index.php? title=User:USERNAMEHERE/sandbox&action=edit" -- Goes back to sandbox and repeats the process
delay 2
end repeat
If you want to test this then put in your wikipedia username in the link and login into your account. Pretty much its a wikipedia bot that will just put stuff in my wikipedia sandbox

Applescript run Safari in background

I have an applescript that is now doing what I want it to do: Open a specific URL, close the page and wait 2 seconds then do it again. The script works.
The problem is that when I run this on my machine, and I am trying to do something else at the same time, the Safari windows keeps popping up. I would really like to run this in the background so that I can continue to work on whatever I am doing.
The code I have is:
set theURL to "https://sites.google.com/site/whatever/"
repeat 100 times
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab --with properties {URL:theURL}
set URL of document 1 to theURL
on error
open location theURL
end try
delay 2
try
close window 1
end try
end tell
tell application "Safari"
quit
end tell
delay 1
end repeat
Anyone have a solution to this one?
Safari is coming to the front because you are activating it within the loop.
repeat 100 times
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to "https://sites.google.com/site/whatever/"
delay 2
close window 1
quit
end tell
delay 1
end repeat
EDIT
set myURLs to {"https://www.google.com/", "http://www.yahoo.com/"}
repeat with aUrl in myURLs
repeat 100 times
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to aUrl
delay 2
close window 1
quit
end tell
delay 1
end repeat
end repeat

Can't retrieve custom title of OSX terminal tabs

I'm trying to select a certain terminal tab in OSX and send keystrokes to it. But Terminal in OSX 10.8.4 doesn't seem to store anything but "Terminal" for a tab's custom title, even if you set a custom title with the inspector. Any ideas? Here is the code I'd like to use to select the right tab:
tell application "Terminal"
set allWindows to number of windows
repeat with i from 1 to allWindows
set allTabs to number of tabs of window i
repeat with j from 1 to allTabs
if custom title of tab j of window i contains "blah" then
set frontmost of window i to true
set selected of tab j of window i to true
end if
end repeat
end repeat
end tell
I'm working on an older version of OSX, so I couldn't duplicate your problem (your code works fine for me), but perhaps try:
if ((custom title of (current settings of (tab j of (window i)))) as string) contains "blah" then
Make sure you set the tab name this way:
tell "application" "Terminal"
# ...
set the_tab to tab 1 of the front window -- replace with your tab selector
tell settings set "Basic"
set custom title of the_tab to "My Tab Name"
end tell
# ...
end tell
You should be able to get retrieve the tab name:
tell "application" "Terminal"
set the_name to custom title of the_tab as string
end tell
Works for me on OSX 10.8.4

How to open a new window and multiple urls in Safari with apple script?

How do you open a new window in safari and then open multiple tabs with different urls in that window using apple script?
The way to create a new window in Safari is to use the make new document command:
make new document at end of documents with properties {URL:the_url}
This will create a new window with a single tab pointing to the_url and make that window frontmost. Note that make new window at end of windows doesn't work, and just errors out with "AppleEvent handler fails".
Similarly, to create a new tab within a window w, you can use make new tab:
make new tab at end of tabs of w with properties {URL:the_url}
This will create a new tab in window w at the end of the list of tabs; this tab will be pointing to the_url, and it won't be the current tab. Instead of explicitly saying tabs of w, you can also use a tell w block:
tell w
make new tab at end of tabs with properties {URL:the_url}
end tell
That way, tabs implicitly refers to tabs of w.
Putting this all together, we get the following script. Given a list of URLs in the_urls, it will open all of them in a new window; if the_urls is empty, it opens a window with a blank tab.
property the_urls : {¬
"http://stackoverflow.com", ¬
"http://tex.stackexchange.com", ¬
"http://apple.stackexchange.com"}
tell application "Safari"
if the_urls = {} then
-- If you don't want to open a new window for an empty list, replace the
-- following line with just "return"
set {first_url, rest_urls} to {"", {}}
else
-- `item 1 of ...` gets the first item of a list, `rest of ...` gets
-- everything after the first item of a list. We treat the two
-- differently because the first item must be placed in a new window, but
-- everything else must be placed in a new tab.
set {first_url, rest_urls} to {item 1 of the_urls, rest of the_urls}
end if
make new document at end of documents with properties {URL:first_url}
tell window 1
repeat with the_url in rest_urls
make new tab at end of tabs with properties {URL:the_url}
end repeat
end tell
end tell
tell application "Safari"
activate
set the URL of document 1 to "http://www.XXXXXXX.com"
my new_tab()
set the URL of document 1 to "http://www.XXXXXX.com"
end tell
on new_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
«event prcsclic» «class menI» "New Tab" of «class menE» "File" of «class mbar» 1
end tell
end tell
end new_tab
Replace the X's with whatever sites you want and keep repeating the code (my new_tab() and set the URL... lines) for each page you'd like to have open.
Referring to this page.
Correct me if this isn't what you were talking about.
Base on Pugmatt's answer I got the following to work...
on run {input, parameters}
tell application "Safari"
activate
make new document with properties {URL:"http://www.apple.com"}
my new_tab()
set the URL of document 1 to "http://www.example.com"
end tell
end run
on new_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
«event prcsclic» «class menI» "New Tab" of «class menE» "File" of «class mbar» 1
end tell
end tell
end new_tab
I'm not sure if this is the most efficient way of you this.

Resources