SWIG

From nswccWiki

Jump to: navigation, search

Contents

Take A_SWIG, rather than SWIG it

I have just looked at SWIG 35 (or 250) the Student Web Index Page creator valiantly! constructed in Excel by Wazza. A_SWIG 1.0 for a class, does some things that SWIG 35 does not do, and leaves out some things. Despite the seeming complexity, this is a relatively simple file rewrite function that, incidentally, ensures correct HTML syntax. A very simple in-line Cascading Style Sheet (CSS) is used to tidy up, and simplify, the markup.

How to take A_SWIG

  • Use Wazza's SWIG Excel spreadsheet to construct the list and then dump into a text file (step 1 to step 6)- you don't do the final step. The HTML generation is done using A_SWIG 1.0 from the text file.
  • Rather than use the information from a SWIG Excel workbook, construct a text file using any simple text editor, such as Text Edit. You could use any editor but ensure that the saved file is pure TEXT without any markup- not even RTF.

A_SWIG 1.0 will prompt you for appropriate configuration information when required. For example, school name, server name, class's name, teacher's name, how many columns etc. You only construct a text file containing comma separated or tab delimited entries of students. A_SWIG 1.0 requires the following format:

<student number>,<dup seq number>,<first name>,<last name>,<id name>.

In this version <dup seq number> is not used. A_SWIG automatically parses the file. The fields could be manipulated using Excel to get them into the appropriate order if you have extracted the data using, say, Passenger. The resulting spartan file can be edited using a web site editor such as Dreamweaver or iWeb.

For the more technically minded, A_SWIG could be put in the ./Library/Scripts/ folder and be available from the AppleScript menu.


-- ASWIG Vers 1.0.2 , by Ian W. Parker 
-- AppleScript version of SWIG, Another SWIG, Advanced SWIG... take your pick
-- first worked on 2008-12-27,  last worked on 2008-12-30
-- requires a comma or tab separated variable text file with each line using format..
-- <student number>,<dup seq number>,<first name>,<last name>,<id name>
-- could be exported from Excel, eg. 1, 0, Ian, Parker ,ianparker1
-- ------------------------------------------------
-- use Mac convention for end of line(eol)
property eol : (ASCII character 10)
property comment : "Auto constructed by A_SWIG " & (current date)
property school_name : "Any Ville"
property teacher_name : "Any Teacher"
property class_name : "Any Class"
property web_server : "http://10.12.13.14/" -- full server reference
global fileRef
-- table width can also be a number of pixels (eg 800)
-- table cols can be adjusted (3 to 6 best)
set {table_width, table_Cols} to {"90%", 4}
-- define student record fields
set student to {seq_id:0, seq_dup:0, first_name:"", last_name:"", short_name:""}
set {row_count, error_flagged, prolog, page} to {1, false, "", ""}
set {font_size, font_family} to {14, "Helvetica, Arial"}
-- get some  details from user
display dialog "Please enter Web Server site name." & eol & ¬
	" Eg. http://Orca/   or   http://10.12.13.14/" default answer web_server with title "Web Server"
set web_server to text returned of result
display dialog "... your school's name" default answer school_name with title "School Name"
set school_name to text returned of result
display dialog "... class's name" default answer class_name with title "Class Name"
set class_name to text returned of result
display dialog "... class teacher's name" default answer teacher_name with title "Teacher Name"
set teacher_name to text returned of result
display dialog "... desired size of text (in points)" default answer font_size with title "Font Size"
set font_size to text returned of result
display dialog "... desired names per row" default answer table_Cols with title "Columns in Table"
set table_Cols to text returned of result
-- build the page  prolog
set prolog to prolog & "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">eol
set prolog to prolog & "<html>" & eol & "<head>" & eol & "<title> " & eol

set prolog to prolog & "<!--" & comment & "-->" & eol & class_name & " Intranet " & eol & "</title>" & eol
set prolog to prolog & "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">eol
-- in-line CSS to assist with tidy layout
set prolog to prolog & "<style type=\"text/css\">eol
set prolog to prolog & ".simplestyle {" & eol
set prolog to prolog & "  font-family: Helvetica;eol
set prolog to prolog & "  font-size: " & font_size & "px;eol
set prolog to prolog & "  text-align: center;eol
set prolog to prolog & "}" & eol & "</style>eol
-- end of CSS
set prolog to prolog & "</head>" & "<body>" & eol
set prolog to prolog & "<p class=\"simplestyle\">" & school_name & "</p>" & eol
set prolog to prolog & "<p class=\"simplestyle\">Student Web Pages " & "</p>" & eol
set prolog to prolog & "<p class=\"simplestyle\">" & class_name & "</p>" & eol
set prolog to prolog & "<p class=\"simplestyle\">Click on a name to visit that student's web page " & "</p>" & eol
set prolog to prolog & "<p class=\"simplestyle\"> <a href=\"" & ¬
	web_server & "\"> Back to main school page </a></p>" & eol
-- end building prolog
-- build table 
set page to prolog & "<table width=\"" & table_width & ¬
	"\" border=\"0\" align=\"center\" cellspacing=\"5\">" & eol
set page to page & "<tr> <td colspan=\"" & table_Cols & ¬
	"\" class=\"simplestyle\">" & teacher_name & "</td> </tr>" & eol
set page to page & "<tr>" & eol
-- ---------------- [read from desktop ] -------- 2008-12-30 
openTextFile()
-- -----------------------------------------------------------------
try -- reading a student record (one line)
	repeat -- until error or end of file
		set next_student to read fileRef until eol
		-- process, assumes field order of SWIG 
		--(studentID, Sequence Dup, first name, last name, short name)
		-- works for both tab and comma separated fields!
		set next_student to every word of next_student -- explode
		set seq_id of student to item 1 of next_student -- first field
		set seq_dup of student to item 2 of next_student -- second field etc.
		set first_name of student to item 3 of next_student
		set last_name of student to item 4 of next_student
		set short_name of student to item 5 of next_student
		-- 
		set display_name to first_name of student & " " & ¬
			last_name of student -- make display name
		set page to page & "<td class=\"simplestyle\"> <a href=\"" & ¬
			web_server & "~" & short_name of student & "/\"> "
		set page to page & display_name & " </a>"
		set page to page & "</td>" & eol
		-- terminate row, and begin a new row?
		if (row_count mod table_Cols) = 0 then
			set page to page & "</tr> <!-- student count " & ¬
				row_count & "-->" & eol & "<tr>" & eol
		end if
		-- count records processed before error
		set row_count to row_count + 1
	end repeat
on error number n
	-- this is how single pass text file handling is done
	set row_count to row_count - 1 -- to correct the count
	if n is -39 then -- normal eof reached
		display alert "Processed all students " & row_count
	else
		display alert "Exited processing at student: " & row_count as warning
	end if
end try
close access fileRef -- for reading
-- -------------------------------------------------------------
-- build page epilog
set page to page & "</tr>" & eol & "</table>" & eol & "</body>" & eol & "</html>"
-- ---------------- [write to desktop and close access ] -------- 2008-12-30
write_HTML(page)
-- -----------------------------------------------------------
-- --------------Support   Routines ---------------
-- -----------------------------------------------------------
on openTextFile()
	global fileRef
	--open text file for reading - assume is on desktop for convenience,
	set newfilePath to choose file with prompt ¬
		"SWIG source file..." default location (path to desktop) -- use a file chooser
	set fileRef to open for access newfilePath -- open the file
end openTextFile
-- ------------------------------------------
on write_HTML(page)
	global fileRef
	set newfilePath to choose file name with prompt "Writing file..." default name class_name & ¬
		" Index.htm" default location (path to desktop)
	set fileRef to (open for access newfilePath with write permission)
	write page to fileRef as string -- faster and safer than interleaving
	close access fileRef -- for writing
end write_HTML
-- --------------------------------------------------------------------
--output in result pane of Script Editor for debugging
page

Sample Input Text File

1, 0, Ian, Parker, ianparker0
2, 0, Ian, Parker, ianparker1
3, 0, Ian, Parker, ianparker2
4, 0, Ian, Parker, ianparker3
5, 0, Ian, Parker, ianparker4

Sample output

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> 
<!--Auto constructed by A_SWIG Tuesday, 30 December 2008 12:42:58 PM-->
Any Class Intranet 
</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.simplestyle {
  font-family: Helvetica;
  font-size: 14px;
  text-align: center;
}
</style>
</head><body>
<p class="simplestyle">Any Ville</p>
<p class="simplestyle">Student Web Pages </p>
<p class="simplestyle">Any Class</p>
<p class="simplestyle">Click on a name to visit that student's web page </p>
<p class="simplestyle"> <a href="http://10.12.13.14/"> Back to main school page </a></p>
<table width="90%" border="0" align="center" cellspacing="5">
<tr> <td colspan="4" class="simplestyle">Any Teacher</td> </tr>
<tr>
<td class="simplestyle"> <a href="http://10.12.13.14/~ianparker0/"> Ian Parker </a></td>
<td class="simplestyle"> <a href="http://10.12.13.14/~ianparker1/"> Ian Parker </a></td>
<td class="simplestyle"> <a href="http://10.12.13.14/~ianparker2/"> Ian Parker </a></td>
<td class="simplestyle"> <a href="http://10.12.13.14/~ianparker3/"> Ian Parker </a></td>
</tr> <!-- student count 4-->
<tr>
<td class="simplestyle"> <a href="http://10.12.13.14/~ianparker4/"> Ian Parker </a></td>
</tr>
</table>
</body>
</html>

Personal tools