-- Remember Open Windows: script to save a list of all files open in BBEdit v6
-- to a preference file. The files can be reopened with the companion script,
-- "Reopen Windows". 
-- Author: Markus Frischknecht, with some lines coming straight from an email
-- exchange with Bare Bones' tech support (especially the ftp lines).
-- Email address: mailto:mfrischknecht@access.ch
--
-- Version history:
-- 1.01 Jan 22 2001 Doesn't remember files in "Cleanup At Startup" folder on startup disk (most likely ftp files
-- opened via an external ftp client such as Anarchie). Hopefully, this type of file might one day be supported properly.
-- 1.0 Dec 2000 Converted an ealier script for BBEdit 5.1 to v6 (changes for ftp support).
--
-- Description:
-- This is a script to create a list of files open in BBEdit (both files from a harddisk
-- or opened via the ftp client built into BBEdit; both text files and group files, but
-- not disk browsers or search result browsers);
-- the list is stored in a script file called "BBEdit stored window list"
-- that is located in the Preferences folder.

-- In BBEdit v6, the commands:
--	is FTP boolean     [r/o]  -- was this document opened from (or saved to) an FTP server?
--	FTP info FTP Info  [r/o]  -- FTP information for this document
-- only work with files opened via BBEdit's internal ftp client, not for interaction with
-- Anarchie (or other external ftp clients that support opening a file in BBEdit).


script winListProp
	property winList : {}
end script


property prefFileName : "BBEdit stored window list"


tell application "Finder"
	try
		set BB to (process 1 whose creator type is "R*ch") as «class psn »
	on error
		-- BBEdit is not running, so abort
		beep
		error number -128
	end try
end tell

set prefFile to ((path to preferences folder) as string) & prefFileName

set winList to {}
set tempFolder to (path to startup disk as text) & "Cleanup At Startup:" -- file opened via Anarchie or similar ftp client


tell application "BBEdit 6.0" to tell BB
	set nbdocs to count documents
	repeat with i from 1 to nbdocs
		try
			set currFile to document i
			set docfile to file of currFile -- file specification
			
			if not ((docfile as string) begins with tempFolder) then
				
				set ftpfile to false
				set ftpSpecs to ""
				if class of currFile is text document then
					-- ftp file in BBEdit 6: check property directly
					set ftpfile to (is FTP of currFile)
					
					if ftpfile then
						set FTPInfoRecord to (FTP Info of currFile)
						set ftpSpecs to FTPInfoRecord's URL --(FTP Info of currFile)
					end if
				end if
				
				-- create a record as list, {filespec:docfile, onServer:ftpfile, ftpURL:ftpSpecs}
				
				set winRecord to {filespec:docfile, onServer:ftpfile, ftpURL:ftpSpecs}
				copy winRecord to beginning of winList -- so last doc in list is opened first
				
			end if
			
			-- Is FTP of file also set if downloaded with e.g. Anarchie,
			-- not just via internal ftp?
			-- Can Anarchie be told to cmd-j file to BBedit?
			-- Unfortunately, no to both questions...
			
		on error errmsg number errnumber
			set msg to errmsg
			set nb to errnumber
			beep
		end try
		
	end repeat
end tell


if winList is not {} then
	-- there might have been open docs, but they might not be editable docs with a file path,
	-- i.e. the list of docs might be empty; if files are in the list, open existing
	-- preference list and overwrite its value with the current list
	try
		set scpt to load script file prefFile -- load script
		set scpt's winList to my winList
	on error
		beep
		set scpt to winListProp
		set scpt's winList to my winList
		
	end try
	
	-- save it
	try
		store script scpt in file prefFile with replacing
	on error errmsg number errnumber
		beep
		display dialog errmsg & " Couldn't write pref file into Preferences folder. (" & errnumber & ")." buttons ¬
			{"OK"} default button "OK"
		
	end try
	
else
	display dialog "Couldn't find any open documents that represent files." buttons ¬
		{"Cancel"} default button "Cancel"
	
end if