This article describes a method for using lasso to serve and read native files, i.e. native Word, Excel etc files, generated by Lasso.
There are many reasons you might want to serve for instance MS Word or Excel files to your users, files where the contents comes from Lasso in one way or another.
This has been the topic many times in the Lasso community forums, where various solutions have been discussed. For instance, for Excel files, one way to deal with the problem is to build and serve a .CSV file, which is nothing more than a text file with special formatting, or to build an html tabel and serve with a .xls file ending.
The problem with serving text files are several, amongst which are that you have to take care to encode the file with the right encoding according to the plattform it is being served to, and you have to make certain the cell delimiter character, such as comma, semi-colon or tab does not occur as part of the data you pour into the file.
The problems with serving an html-table file are that different versions of Excel will read the file slightly different (and some might fail), and of course, it is not a native Excel file, therefore it will not open directly in other applications that can open native Excel files, such as SPSS.
The same considerations and problems are common with all attempts to simulate one (native) file type by serving another.
This describes a method where you can read and write 100% native files to the client. This is done by using the native application as a file parser on the server side. Therefore, a few caveats exist:
The chain of events in your Lasso script is this:
Here is a code sample for an Excel file:
// set up some variables. These will vary according to your setup
Var: 'fileName' = 'myExcelFile';
Var: 'webDir' = '/Users/Library/Documents/' //from the root of the HD
Var: 'myDir'='/users/files/'; //relative to the root of the web folder
Var: 'filetype' = 'application/excel';
Var: 'fileending'='.xls';
//make sure the working directory exists
If: !(File_Exists: $myDir);
File_Create: $myDir;
/If;
// write the file, in this case an html file (which Excel can parse and open)
Inline: -username='fileuser', -password='secretpassword';
File_Create: ($myDir + $filename + '.html'), -FileOverWrite;
File_Write: ($myDir + $filename + '.html'), $filecontents, -FileOverWrite;
/Inline;
//Call Applescript through OS_process, which will have Microsoft Excel
//open the html file and then save out a native file. Notice that the argument
//for OS_process is one string, but the line breaks represent the line breaks that
//Applescript expects (a bit mangled in display, look at it in the editor to view the line breaks correctly).
Inline: -username='process_user', -password='secretpassword2';
Var: 'os' = (OS_Process: '/usr/bin/osascript', (Array: '-'));
$os->(Write: 'tell application "Microsoft Excel"
open "' + $webDir + $myDir + $filename + '.html"
save in "' $filename + $fileending'" as Excel98to2004 file format
close workbook 1
end tell');
$os->CloseWrite;
$os->Read;
$os->Close;
/Inline;
//We then delete the html file and serve the native file Excel wrote to disk
Inline: -username='fileuser', -password='secretpassword';
File_Delete: ($authorDir + $filename + '.html');
File_Stream: -File=($authorDir + $filename + $fileending), -Name=($filename + $fileending), -Type=$filetype;
/Inline;
We use the construct 'as Excel98to2004 file format' to have Excel save in a format that should be as universal across Excel Mac/Windows as possible.
Notice that we have Excel close the document, and we delete the .html file, but we cannot trash the native document after we've served it, since the [File_Stream] tag aborts the page execution. If you want to get rid of the file once it's been served, you will have to set up an Event to do so.
Also notice that while Lasso takes its file paths from the root of the website folder, Applescript needs to be told from the root of the HD.
The technique is very similar for MS Word files, except the Applescript is slightly different. This works for me:
$os->(Write: 'tell application "Microsoft Word"
open "' + $webDir + $myDir + $filename + '.html"
set default file path file path type documents path path (("' + $webDir + $myDir + '" as POSIX file) as text)
save as active document file name "' + $filename + '.doc" file format format document97
close active document
end tell');
Word is not so forgiving as Excel is when it comes to specifying paths using slashes, so we'll have to resort to some POSIX acrobatics. And the line set default file path file path type documents path path (("....
is not a typo. Go figure.
Allthough the preceeding has descibed only serving, i.e. writing, native files, to read from native files should be straightforward, using a reverse process:
Other file types should work just as well, provided that a) you have a way to get the application to parse and open a file produced with Lasso, and b) as stated, that the application is applescriptable, at least to the extent that it can open and save files.
Author: Lars A. Gundersen
Created: 23 Sep 2010
Last Modified: 24 Mar 2011
Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.
©LassoSoft Inc 2015 | Web Development by Treefrog Inc | Privacy | Legal terms and Shipping | Contact LassoSoft