Distributing files

Author: David Hood
Contents Page - Distributing files
Date Last Modified: 25th August 2003
E-mail: caversham@otago.ac.nz

Previous Page 2 of 2 Contents Page

This script contains a great deal of error checking, as should any script that involves writing files. This script is also broken into a series of smaller functions, doSomethingUseful brings all of the smaller function together, assignName as a default name for the index file, checkName checks that it is not already in use, writeIndexFile creates the index file.

property folderOfInterest : ""
property indexName : ""
property listOfFilenames : {}

set folderOfInterest to choose folder with prompt "Choose folder to create index page:" --if double-clicked

on open of finderObjects -- "open" handler triggered by drag'n'drop launches
repeat with i in (finderObjects) -- in case multiple objects dropped on applet
if folder of (info for i) is true then --
set folderOfInterest to i
end if
end repeat
end open

--Assign index file name
on assignName()
set indexQuery to display dialog ("For folder " & (name of (info for folderOfInterest)) & ", save as file named:") default answer "index.html" buttons {"Cancel", "OK"} default button 2
if button returned of indexQuery = "I give up" then
error number -128 --user canceled
set indexName to text returned of indexQuery
end if
end assignName

--Check name
on checkName()
tell application "Finder" to set listOfFilenames to (every file of folderOfInterest)
repeat with individualFile in listOfFilenames
if name of individualFile = indexName then
set warningMessage to (indexName & " already in use as a name.")
display dialog warningMessage buttons {"Exit"}
error number -128
end if
end repeat
end checkName

--build and write the index file to the folder
on writeIndexFile()
set pageHtml to "<HTML>
<TITLE>List of Files<⁄TITLE>
<H2>The following files are available<⁄H2>
repeat with individualFile in listOfFilenames
set individualFileName to name of individualFile as string
set pageHtml to pageHtml & "<P><A HREF='" & individualFileName & "'> " & individualFileName & "<⁄A><⁄P>
end repeat
set pageHtml to pageHtml & "<⁄BODY>
set myFileHere to (folderOfInterest as string) & indexName
set PickMePickMe to open for access file myFileHere with write permission
set eof of PickMePickMe to 0
write pageHtml to PickMePickMe
close access PickMePickMe
on error
close access file myFileHere
end try
end try
end writeIndexFile

-- put it together
on doSomethingUseful()
end doSomethingUseful

If this is copied and pasted into script editor and saved as an application, it creates a file which can be dragged and dropped upon to run.

Return to Contents Page