To particular window of particular tab and save source file in safari

Hi Guys,

I am using Safari version 14.1.2 and macOS Mojave version 10.14.6
I am working with many windows in Safari as well as Google Chrome. I would like to routinely (certain time) refresh some particular window of some particular tab and to save it as HTML File without disturbing other windows.
The below code works up to 80% of my need. Whereas its brings the target window as frontmost window. Which disturbing while working in another window

set someurl to “Google
tell application “Safari”
–activate
set myID to id of window 1
set myTab to current tab of window 1
set thename to name of window 1
end tell

repeat
tell application “Safari”
set index of window id myID to 1
set URL of myTab to someurl
if document 1 exists then
set theDocumentTitle to the name of document 1
set theDocumentSource to the source of document 1
tell application “TextWrangler”
–activate
set theNewDocument to make new document with properties {name:theDocumentTitle, text:theDocumentSource}
set theDocumentsFolderPath to the path to desktop as text
set theSaveFilePath to theDocumentsFolderPath & theDocumentTitle & “.html”
save theNewDocument to file theSaveFilePath
close theNewDocument
end tell
end if
end tell
delay 20
end repeat
Is there any script to fulfill my need for both Safari and Google Chrome?

Any help would be appreciated

Thanks,
John

When you set the index of the window to 1, you bring it to the front. Instead, leave it where it is and do stuff like set x to name of window id wid

tell application "Safari"
	
	set w2 to window 2
	set wid2 to id of w2
	
	set d2 to document of window id wid2
	
	set docSource to source of d2
	set docTitle to name of d2
	
end tell

You might consider having safari save the document and foregoing the textwrangler part — by the way, Bare Bones has retired textwrangler and now offers BBEdit (with a few functions disabled) for free instead. Doing so would simplify your script and make it quicker and less intrusive.

Finally, if you put your code between lines of three backticks, it will be formatted properly. You should be able to edit existing posts.

```
your code here
```

Hi Mockman,
Your code does what I need. But how to target a particular tab (e.g., tab 2) of the window ID.

Thanks

To bring it to the front of that window:

set current tab of w2 to tab 2 of window id wid2

To work directly with it without bringing it to the fore:

close tab 2 of window id wid2