user.html.macros.fileLink


File: fileLink.sit.hqx
Server: www.orange-road.com
Modified: 4/24/98; 11:00:11 AM
Size: 3K
Owner: khagler@orange-road.com
Attachments: None

The fileLink macro adds a link to a downloadable file, with information on the file. It also stuffs and binhexes the file, and sticks it in the same folder as the page it is called from. I wrote this really quickly, and it's not exactly polished, but it seemed useful enough to post. There are some limitations, though. It (probably) won't work at all if you render directly to an ftp site instead of a local volume or an Appleshare volume. Also, it uses Stuffit Deluxe to prep the file--it seems that the StuffitCMD crashes when called inside a macro during page rendering. Some of the rough edges I'll smooth out when I have a little more time.

It's called like this:
fileLink ( theFile )
Where theFile is a path to the file you want to link to.

It returns a string like this:
<a href="atombombplans.sit.hqx">Atom Bomb Plans</a>, Graphic Converter PNG picture, 57K download

Here is the source code:

on fileLink ( theFile, linkName = nil ) {
	«Wed, Feb 11, 1998 at 3:51:58 PM by KGH
		«Adds a link to a file, with info, to the web page. Also stuffs and binhexes the file and drops it in the same
		«folder as the page being rendered.
		«Drawbacks:
			«I don't know what will happen if you render directly to an ftp server, but the archive creation part
				«probably won't work.
			«There's no attempt to stick to a file name length limitation, so this wouldn't work so well if the file
				«already has a long name, or will be put on an 8.3 volume.
	«Fri, Apr 24, 1998 at 10:48:19 AM by KGH
		«Optional parameter for a link name different from the file name.
	local ( fileName, fileSize, fileKind, htmlText, archiveName );
	bundle { « get the information we need out of the Finder
		with objectModel, Finder {
			local ( fileObj = finderSpec ( theFile ) ); « make sure the file parameter is something the Finder will like
			fileName = get ( fileObj.name );
			«fileSize = number ( get ( fileObj.size ) / 1024 ) + "K"
			fileKind = get ( fileObj.kind );
			if fileKind contains "[trademark]" { « get rid of this symbol, which doesn't convert to HTML
				fileKind = fileKind - "[trademark]"}}};
	bundle { « stuff and binhex the file, and stick it in the same directory as the page being rendered
		local ( targetFolder, targetFile,  archiveObj );
		targetFolder = file.folderFromPath ( html.data.page.f ); « find the folder to put the archive in
		archiveName = html.normalizeName ( file.fileFromPath ( theFile ) ) + ".sit"; « set the archive name
		targetFile = targetFolder + archiveName; « the path for the new archive file
		if file.exists ( targetFile + ".hqx" ) { « get rid of any existing file to make room for the new one
			file.delete ( targetFile + ".hqx" )};
		with objectmodel, Stuffit { « create the archive using Stuffit Deluxe, since the UCMD doesn't work during rendering
			try { « lots of things can go wrong here, if there is already a file there by that name for instance
				launch();
				archiveObj = make ( archive, withProperties: {pathname: targetFile} ); « create a new empty archive
				stuff ( theFile ); « create the archive
				translate ( targetFile, using: "BinHex4", into: targetFile + ".hqx", encoding: true );
				quit()}
			else { « sure enough, something did go wrong
				scripterror ( "There was a problem creating the binhexed archive" )}};
		with objectModel, Finder { « get the size of the stuffed, binhexed file
			local ( fileObj = finderSpec ( targetFile + ".hqx" ) );
			fileSize = number ( get ( fileObj.size ) ) / 1024 + "K"};
		file.delete ( targetFile ) « see comments inside...
			«This may look strange, but it's necessary to get rid of the .sit file. When the file is stuffed and encoded, it creates a
				«stuffit archive and then encodes that, leave the .sit behind. We only want the .hqx, so this is done.};
	bundle { « create the text to insert in the web page
		if linkName == nil { linkName = fileName }; « set default link name
		htmlText = "<a href=\"" + archiveName + ".hqx\">" + linkName + "</a>"; « build the link to the archive
		htmlText = htmlText + ", " + fileKind + ", " + fileSize + " download"; « add the file info
		return ( htmlText )}};
fileLink ( "Work:DocServer DTD Notes" )

Site scripted with Userland Frontier
This page was last built on Mon, Jun 15, 1998 using the Sysop Suite (V1.0f1) on a PowerWave running Frontier.