-- Reopen Windows: reads file specs for windows from preference file, loads those files.
-- Works with BBEdit v6.
-- 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.0 Dec 2000 Converted an ealier script for BBEdit 5.1 to v6 (changes for ftp support).
--
-- Description:
-- This is a script to open a list of files open in BBEdit;
-- that list is stored in a script file called "BBEdit stored window list"
-- that is located in the Preferences folder;
-- it is placed there by the script "Remember open windows".

-- Only load ftp files if shift key is down;
-- and load ftp files only (i.e. not files from HD) if shift and command keys are down.

-- Needs Jon's Commands v2.0 or later (for getting state of modifier keys).


property prefFileName : "BBEdit stored window list"

global loadFTP -- if true, load from ftp server
global loadDisk -- if true, load from disk
-- no modifier keys pressed: only load files from disk
-- shift key pressed: load files from disk and via ftp
-- shift and command: only load files via ftp

set keysdown to get key pressed -- get set of keys pressed at startup; from osax "Jon's Commands"

set loadDisk to true
set loadFTP to false

if "Shift" is in keysdown then
	set loadFTP to true
	if "Command" is in keysdown then
		set loadDisk to false
	end if
end if


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

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

set winList to {}
try
	set scpt to load script file prefFile -- load script
	set my winList to scpt's winList
on error
	beep -- probably no such file yet; quit
	display dialog ¬
		"Preference file with files to re-open not found." buttons ¬
		{"OK"} default button "OK"
	error number -128
end try


set missingFiles to ""
set fileSpecString to ""
tell application "BBEdit 6.0" to tell BB
	repeat with i in winList
		try
			if i's onServer then
				-- it's an ftp file
				-- only load if shift key was down
				if loadFTP then
					set fileSpecString to (i's ftpURL)
					«event GURLGURL» fileSpecString
					-- open location fileSpecString
					-- or use Anarchie:
					(*
					with timeout of maxSeconds
						tell application "Anarchie"
							edit url (i's ftpSpecs)
						end tell
					end timeout
					*)
				end if
			else
				if loadDisk then
					set fileSpecString to (i's filespec) as text -- (i)
					open fileSpecString as alias
				end if
			end if
		on error errmsg number nb
			set missingFiles to missingFiles & (fileSpecString as text) & return
			set msg to errmsg
			set errnb to nb
		end try
	end repeat
	
	
	
end tell

-- actually, it's possible to use the list of files as parameter to open command:
-- ("open winList"); but then we can't show the following dialog box with files
-- that weren't found...

-- display missing files;
-- if more than five files were missing, only display first five entries
-- (and add: "...and others" at the end of the dialog box text);
-- also, if there were any missing files, display button that offers copying
-- a list of missing files to a ew BBEdit window

if missingFiles is not "" then
	if (count missingFiles's paragraphs) > 5 then
		set oldTextDels to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {return}
		set displayList to (paragraphs 1 thru 5 of missingFiles) as text
		set AppleScript's text item delimiters to oldTextDels
		
		set displayList to displayList & return & "...and others."
	else
		set displayList to missingFiles
	end if
	
	display dialog ¬
		"The following files where not found:" & return & displayList buttons ¬
		{"Copy to new doc", "OK"} default button "OK"
	
	if button returned of the result is "Copy to new doc" then
		tell application "BBEdit 6.0" to tell BB
			-- create new text window
			make new text window
			set selection to "The following files couldn't be loaded:" & ¬
				return & return & missingFiles
		end tell
	end if
end if