How to Extend Browser Filing Script - with - close each window processed?

Hi,

First of all thanks for posting these nice Browser Filing Scripts.
I’ve started with “File Safari Tabs Extended”.

FYI, I’m currently using an Alfred workflow with the Veritrope scripts to save browser tabs.
But I would like to extend the “File Safari Tabs Extended” script to also close each window while processing.

I’ve tried adding close currentWindow, but this does not work. (currentWindow is not defined)
Before getting into more trouble, I prefer to ask some help and advice :wink:

Thanks for helping out!

tell application id "DNtp"
	set {database:db, location:destLoc, name:destName} to (preferred import destination)
	
	-- If the Inbox or the root of the database is chosen, don't repeat the database's name in the created group
	if (db's id is 1) or (destLoc is "/" and (destName is (name of db))) then set {destName, destLoc} to {"", ""}
	
	set dest to create location (destLoc & destName & "/" & appName & ts as string) in db
	
	if not groupByWindow then set urlGroup to create record with {name:(localized string "All Windows"), type:group} in dest
	
	repeat with windowURLs in items of URLList
		if groupByWindow then
			set theName to (item incr of windowNames)
			if theName contains nameDelimiter then
				set AppleScript's text item delimiters to nameDelimiter
				set theName to (text item 1 of theName) as string
			else
				set theName to ((localized string "Window ") & incr) as string
			end if
			set urlGroup to create record with {name:theName, type:group} in dest
		end if
		
		--- Close the window---PanE---2026-05-14---
		close currentWindow
		repeat with theURL in windowURLs
			create record with {type:bookmark, URL:theURL} in urlGroup
		end repeat
		set incr to incr + 1
	end repeat
end tell

Posting the complete script would’ve been helpful. And explaining a bit more – like that you’re (probably) trying to close a browser window, not a DT window. Or are you?

But that does not matter, as neiter DT nor Safari seem to provide anything that is even remotely named currentWindow. Nor does the original script for Safari contain a variable of that name. So – no, you cannot close something that is not there.

If you want to close a browser window, this may help in the case of Safari.
And since you’re using AppleScript with its weird context switching tell stuff: You must send the command to the appropriate application. Your code is telling DT to do something…

Probably easiest to add the close thingy to the appropriate tell block that you did not post.

Thanks for replying;

My intention was to avoid posting on the forum what is available on the DEVONthink blog. (The used script being clearly mentioned by name and a link to the pertaining blogpost)
But, hey, no problem there, here’s the complete script. I’ve uncommented “my” trials so that at least the script works the way it is supposed to work.

Your assumption is indeed correct that the script should:

  • step through each browser (Safari in this case) window,
  • do it’s work and save to DTPO (window per window)
  • close each processed window before proceeding to the next window

** in bold italics is the additional step that I would like to add.

Attachment - the script

File Safari Tabs Extended.scptd.zip (29.3 KB)

It is a lot easier to read a complete script in one place, compared to downloading the code and opening it in Script Editor to answer the questions that appear in the brower.

I’ve already posted a link :wink: to the relevant code. And if you look at the code in the script’s Safari tell block, you’ll notice that it saves everything in two global lists that are then processed in the next tell block by DT. When DT does its work, it no longer even looks at Safari, it just loops over the two lists created before.

You could just as easily turn off Safari as the last action in its tell block. That will close all windows.

1 Like

My coding skills are insufficient to remedy my question;
But if I understand (you) correctly, the script creates or sets
In part a) - Safari

  1. URLList
  2. windowNames

Then in part b) - DNtp
Does the rest of the script

Is that correct?

Yes. And that is a very clean way to do it. Why do you even want to close all the windows?

Long story, but mainly I tend to have way to many windows and tabs open, until they finally grow out of hand (after a couple of days / weeks).

For starters, I almost always keep my browsing history in Devonthink. So that is a starting point: all browser activity gets registered into Devonthink.
I would like to change or extend that, additionally, in a way that, if I can get the script to work, preferably including the sequentially closing of the window(s), so that I can set up a cron job to just run a couple of scripts at a specified time, perhaps even shutting down the mac, then all will be closed and a clean working environment is ready the next morning.

It’s a matter of enforcing browsing hygiene while maintaining a browser activity log.

It sounds like over engineering to me. But if it serves your purpose… Still don’t understand why you have to “sequentially close the windows” instead of just shutting down the browser. But at least you should now know how to do it.