'-------------------------------------------------------------
'-------------------------------------------------------------
'NAME:ProjectMan
'AUTHOR:Jonathan Katoe
'CONTACT:jkatoe@optonline.net
'VERSION:1.0
'DESCRIPTION:Generates an html page with links to various scene
'files, presets, or media.(Please view the ReadMe for usage.)
'-------------------------------------------------------------
'-------------------------------------------------------------
'Usage:
'	-Run script
'
'	-Enter a Title name for the files
'
'	-Uncheck "Link_to_files" if you DO NOT want to link 
'	this page to another html page
'
'	-Pick all the files you want
'
'	-Press Cancel when you are done
'
'	-At this point the window pops up again
'
'	-Now choose where you would like to save out the html 
'	page to
'
'	-The window pops up once again and you are prompted to 
'	choose other html files to link with this one
'
'	-Choose as many as you want/need and finally click 
'	cancel to end it all

Create()
sub Create()
	dim root
	dim pname
	dim title
	dim link_to_files
	dim scnpath
	dim htmlpath
	dim fb
	dim fpn()
	dim fbn()
	dim fp()
	dim fex()
	dim q
	dim vlink
	dim i, j, k
	dim fso
	dim out

	'---Find scene root
	set root = enumelements("project", true)
	set root = sifilter(Root, "scene", true )(0)
	set root = EnumElements( root , true)
	set root = SIFilter(root, "sceneobject", true)(0)
	
	'---Create Options
	set pname = AddProp ("Custom_parameter_list",root, , "ProjectMan").Value("Value")
	SIAddCustomParameter pname, "Title", siString, 0, 0, 0, 0, 0, 0.000, 0.000
	SIAddCustomParameter pname, "Link_to_files", siBool, 1.000, 0.000, 1.000, 0, 0, 0.000, 0.000

	'---Prompts user for input
	InspectObj root & "." & pname,,"Setup ProjectMan",4
	
	title = GetValue(pname & ".Title")
	link_to_files = GetValue(pname & ".Link_to_files")
	
	'---Error trap
	if err.number <> 0 then
		DeleteObj root & ".ProjectMan"
		exit sub
	end if
	
	'---Error trap
	if title = "" then
		DeleteObj root & ".ProjectMan"
		exit sub
	end if
	
	'---Get paths
	scnpath = application.installationpath(siprojectpath) & "\Scenes\"
	htmlpath = application.installationpath(siprojectpath) & "\net_temp\"

	'---Instance Filebrowser
	set fb = xsiuitoolkit.filebrowser
	i = 0		'counting variable

	'---File open dialogs for picking images
	do 
		fb.dialogtitle = "Select File #" & i
		fb.initialdirectory = scnpath
		fb.filter = "All Files(*.*)|*.*||"
		fb.showopen
		redim preserve fpn(i)
		redim preserve fbn(i)
		redim preserve fp(i)
		redim preserve fex(i)
		fpn(i) = fb.filepathname
		fbn(i) = fb.filebasename
		fp(i) = fb.filepath
		fex(i) =fb.fileextension
		i = i + 1
	loop until fb.filepathname = ""

	'---#I tried this but its flaky - if u get it let me know
	'---#PROBLEM:thumbnails are not saved properly or maybe just too quickly(???)
	'---Instance Pshop and run the Thumbnail action for images only
	'set ps = createobject("photoshop.application")
	'for j = 0 to ubound(fpn) - 1
	'	set psd = ps.open(fpn(j)) '<<<Out of mem errors track to here
	'	ps.playaction("Thumbnail")
	'	psd.saveto(fp(j) & fbn(j) & "_TN" & "." & fex(j))	'Saves out a resized thumbnail
	'	logmessage fp(j) & fbn(j) & "_TN" & "." & fex(j)
	'	psd.close
	'next
	'ps.quit

	'---Dialog to save the output HTML
	fb.dialogtitle = "Save out to an HTML file"
	fb.initialdirectory = htmlpath
	fb.filter = "Html Files (*.htm;*.html)|*.htm;*.html||"
	fb.filebasename = activeproject.activescene.name
	fb.showsave

	'---Error trap
	if fb.filepathname <> "" then
		application.statusbar = "Generating file..."
	else
		application.statusbar = "Cancelled Operation."
		DeleteObj root & ".ProjectMan"
		exit sub
	end if

	'---Strings variables for formating the html
	q = chr(34)
	vlink = q & " TEXT=black LINK=blue " & "vlink=" & q & "#DDDDDD" & q & ">"
		
	'---Start writing to the file
	set fso = createobject("scripting.filesystemobject")
	set out = fso.createtextfile(fb.filepathname, true)
	out.writeline("<HTML>" & "<BODY BGCOLOR=" & q & "#ABA8A6" & vlink)
	out.writeline("<FONT FACE=" & q & "ARIAL" & q & " COLOR=" & q & "#000000" & q & " SIZE=2>")
	out.writeline("<H1>" & title & "</H1>")
	out.writeline("<UL>")
	
	'---Item List
	for k = 0 to ubound(fpn) - 1
		out.writeline("<LI TYPE=round>")
		out.writeline("<A HREF=" & q & fpn(k) & q & ">")
		out.writeline(fbn(k) & "." & fex(k))
		out.writeline("</A>")
		out.writeline("<BR>")
	
	next
	out.writeline("</UL><HR><BR><H1>Links</H1>")
	i = 0
	dim fpn2()
	dim fbn2()

	if link_to_files = "True" then
		set fb2 = xsiuitoolkit.filebrowser
		do 
			fb2.dialogtitle = "Link to this file"
			fb2.initialdirectory = htmlpath
			fb2.filter = "Html Files (*.htm;*.html)|*.htm;*.html||"
			fb2.showopen
			redim preserve fpn2(i)
			redim preserve fbn2(i)
			fpn2(i) = fb2.filepathname
			fbn2(i) = fb2.filebasename
			i = i + 1
		loop until fb2.filepathname = ""
		
		for k = 0 to ubound(fpn2) - 1
			out.writeline("<A HREF=" & q & fpn2(k) & q & ">")
			out.writeline(fbn2(k))
			out.writeline("</A><BR>")
		next
	else
		out.writeline("No linked files.")
	end if
	DeleteObj root & ".ProjectMan"
	out.writeline("</FONT></BODY><HTML>")
	application.statusbar = "Operation Complete"
end sub
