FolderFeeds: Publish the contents of a folder as an RSS feed

Hi,

If you ever feel the need to publish the contents of a folder on your Mac as an RSS feed, the following two sample AppleScripts may give you some hints how to do it.

For a long time the company I am currently working for used eMail notifications to inform editors about new files being available in various folders on an Xserve. But eMails must be managed and so we searched for a better way.

With FolderFeeds the editors can now easily subscribe to certain RSS feeds and use their favourite newsreader to be notified about new files in a folder. They don’t need to delete notification eMails anymore and can directly download the files.

If you are interested I invite you to download the demo package. It requires Mac OS X 10.4 or higher, moreover you need to enable Web Sharing in the System Preferences.

Customizing the scripts to your own requirements should not be too hard if you have some experience with AppleScript. You will just need to modify some of the values in the run handler where it initializes the demo settings.

FolderFeeds 0.1 - Publish folder contents as an RSS feed (ZIP archive, 428 KB)

The ZIP file comprises of two scripts, which are described in the following:

FolderFeeds Desktop
This script will monitor your desktop folder in specified intervals (currently 60 seconds) and create an RSS feed of its contents (files only).

The RSS file will be saved in your Sites folder:
“/Users/yourname/Sites/desktop.rss”, e.g. “/Users/steve/Sites/desktop.rss”

Safari will try to open the following URL to display the RSS feed:
“feed://yourip/~yourname/desktop.rss”, e.g. “feed://192.168.0.5/~steve/desktop.rss”

The script is useful when you want to notify other people about newly created files
in a folder on your Mac.

Nevertheless you won’t be able to download any files directly from the RSS feed.
If you need this feature and fancy item icons, then please take a look at the sample script below.

FolderFeeds Sites
This demo script will monitor your Sites folder in specified intervals (currently 60 seconds) and create an RSS feed of its contents (files only).

The RSS file will be saved in your Sites folder:
“/Users/yourname/Sites/sites.rss”, e.g. “/Users/steve/Sites/sites.rss”

Safari will try to open the following URL to display the RSS feed:
“feed://yourip/~yourname/sites.rss”, e.g. “feed://192.168.0.5/~steve/sites.rss”

The script is useful when you want to notify other people about newly created files in the Sites folder on your Mac.

This version of the script will also include the actual files in the RSS feed. Clicking on an item will download it to your Mac.

Happy Scripting! :smiley:


-- author: Martin Michel
-- created: April 2010
-- modified: --/--
-- version: 0.1
-- tested on:
--     Mac OS X 10.4.11 (PowerMac G5)
--     Mac OS X 10.6.3 (MacBook Pro)

-- This demo script will monitor your Sites folder in specified intervals
-- (currently 60 seconds) and create an RSS feed of its contents (files only).

-- The RSS file will be saved in your Sites folder:
--     "/Users/yourname/Sites/sites.rss", e.g. "/Users/steve/Sites/sites.rss"

-- Safari will try to open the following URL to display the RSS feed:
--     "feed://yourip/~yourname/sites.rss", e.g. "feed://192.168.0.5/~steve/sites.rss"

-- The script is useful when you want to notify other people about newly created files
-- in the Sites folder on your Mac.

-- This version of the script will also include the actual files in the 
-- RSS feed. Clicking on an item will download it to your Mac.

-- Please note that you have to enable Web Sharing to enjoy this script.

property mytitle : "FolderFeeds Sites"
property myversion : "0.1"

-- this list stores the paths to the watched folders
property watchedfolderpaths : {}

-- this folder stores the produced *.rss files
property rssfolderpath : missing value

-- this list stores the RSS file names in a
-- 1:1 relationship to the watched folder paths above
property rssfilenames : {}

-- this list stores the links to the RSS files
-- in a 1:1 relationship to the watched folder paths above
property rsschannellinks : {}

-- this folder stores the log files
property logsfolderpath : missing value

-- triggers the watching of folders
property watching : false

-- the idle handler is called every x seconds
property updfrequency : 60

-- I am called when the script is opened with a double click
on run
	set watching to false
	
	-- we are setting the desktop folder as a watched folder for demo purposes...
	set watchedfolderpaths to {((path to sites folder) as text)}
	-- ...and enter a corresponding RSS file name
	set rssfilename to "sites.rss"
	set rssfilenames to {rssfilename}
	set myip to my getmyip()
	tell application "System Events"
		set shortusername to name of current user
	end tell
	set rsschannellink to "feed://" & myip & "/~" & shortusername & "/" & rssfilename
	set rsschannellinks to {rsschannellink}
	-- ~/Sites/
	set rssfolderpath to (path to sites folder from user domain) as text
	set parentfolderpath to my getparentfolderpath((path to me) as text)
	set logsfolderpath to parentfolderpath & "Logs:"
	if not my itempathexists(logsfolderpath) then
		set command to "mkdir -p " & quoted form of POSIX path of logsfolderpath
		do shell script command
	end if
	
	set watching to true
	idle
end run

-- I am called every «updfrequency» seconds
on idle
	if watching is true then
		try
			-- processing each watched folder
			set countwatchedfolderpaths to length of watchedfolderpaths
			repeat with i from 1 to countwatchedfolderpaths
				set watchedfolderpath to item i of watchedfolderpaths
				set rssfilename to item i of rssfilenames
				set rsschannellink to item i of rsschannellinks
				
				-- does the watched folder exist?
				if not my itempathexists(watchedfolderpath) then
					set errmsg to "The following watched folder does not exist:" & return & tab & watchedfolderpath
					my logmsg(errmsg, "--", "error")
				else
					-- listing all files in the watched folder
					tell application "Finder"
						try
							set folderfiles to (every file in (watchedfolderpath as alias)) as alias list
						on error
							set folderfiles to (every file in (watchedfolderpath as alias)) as alias as list
						end try
						set watchedfoldername to name of (watchedfolderpath as alias)
					end tell
					
					set countfolderfiles to length of folderfiles
					set rssfilepath to rssfolderpath & rssfilename
					
					-- creating the RSS feed
					set {success, errmsg} to my RSSFeedCreator's initialize()
					if success is false then
						set errmsg to "Could not initialize the RSS feed:" & return & errmsg
						my logmsg(errmsg, "--", "error")
					else
						set curdate to current date
						
						-- setting the RSS channel elements
						my RSSFeedCreator's setchanneltitle("FolderFeed for " & watchedfoldername)
						my RSSFeedCreator's setchannellink(rsschannellink)
						my RSSFeedCreator's setchannelcategory("Folder Contents")
						my RSSFeedCreator's setchannelgenerator(mytitle & " " & myversion)
						my RSSFeedCreator's setchannelpubdate(curdate)
						my RSSFeedCreator's setchannellastbuilddate(curdate)
						
						-- getting details about the found files in the watched folder
						-- and creating the corresponding RSS items
						repeat with i from 1 to countfolderfiles
							set folderfile to item i of folderfiles
							set folderfileinfo to (info for folderfile)
							set folderfilename to (name of folderfileinfo) as text
							set folderfilemoddate to modification date of folderfileinfo
							set folderfilekind to kind of folderfileinfo
							set folderfilesize to my getstringsize(size of folderfileinfo)
							set folderfilemimetype to my getmimetype(folderfile as text)
							
							tell application "Finder"
								set folderfileurl to URL of folderfile
							end tell
							
							-- creating a new RSS item and settings its elements
							my RSSFeedCreator's createitem()
							my RSSFeedCreator's setitemtitle(folderfilename)
							my RSSFeedCreator's setitemlink(my gethttpurl(folderfileurl))
							my RSSFeedCreator's setitemdescription(folderfilekind & " (" & folderfilesize & ")")
							my RSSFeedCreator's setitempubdate(folderfilemoddate)
							-- yeah, we have a MIME type!
							if folderfilemimetype is not missing value then
								my RSSFeedCreator's setitemenclosureurl(my gethttpurl(folderfileurl))
								my RSSFeedCreator's setitemenclosurelength(((size of folderfileinfo) as integer) as text)
								my RSSFeedCreator's setitemenclosuremimetype(folderfilemimetype)
							end if
							my RSSFeedCreator's saveitem()
						end repeat
						
						-- exporting the RSS feed into a file
						set {sucess, errmsg} to my RSSFeedCreator's export(rssfilepath)
						if success is false then
							set errmsg to "Could not create RSS file:" & return & tab & rssfilepath & return & return & errmsg
							my logmsg(errmsg, "--", "error")
						else
							-- just for demo purposes
							do shell script "open -a Safari.app " & quoted form of rsschannellink
						end if
					end if
				end if
			end repeat
		on error errmsg number errnum
			my logmsg(errmsg, errnum, "error")
		end try
	end if
	return updfrequency
end idle

-- I am returning the byte size of an item in human readable form
on getstringsize(bytesize)
	if bytesize ≥ 1.073741824E+9 then
		set stringsize to ((bytesize / 1.073741824E+7 div 1) / 100) & " GB"
	else if bytesize ≥ 1048576 then
		set stringsize to ((bytesize / 1.048576E+4 div 1) / 100) & " MB"
	else if bytesize ≥ 1024 then
		set stringsize to (bytesize div 1024) & " KB"
	else
		set stringsize to (bytesize as string) & " bytes"
	end if
	return stringsize
end getstringsize

-- I am returning the MIME type of a given item path
on getmimetype(itempath)
	set qtditempath to quoted form of POSIX path of itempath
	set command to "/usr/bin/file --mime-type " & qtditempath
	try
		set output to do shell script command
	on error
		-- Mac OS X 10.4 fallback
		set command to "/usr/bin/file -i " & qtditempath
		set output to do shell script command
	end try
	set revoutput to (reverse of (characters of output)) as text
	set colonoffset to offset of ":" in revoutput
	try
		set mimetype to (characters -(colonoffset - 2) through -1 of output) as text
	on error
		set mimetype to missing value
	end try
	return mimetype
end getmimetype

-- I am returning the HTTP representation of the file URL
on gethttpurl(fileurl)
	set httpurl to fileurl
	set httpurl to my searchnreplace("file://", "http://", httpurl)
	set searchstr to ("localhost" & (POSIX path of (path to sites folder from user domain)))
	set replstr to (my getmyip() & "/~" & my getshortusername() & "/")
	set httpurl to my searchnreplace(searchstr, replstr, httpurl)
	return httpurl
end gethttpurl

-- I am returning the short name of the current user
on getshortusername()
	tell application "System Events"
		set shortusername to name of current user
	end tell
	return shortusername
end getshortusername

-- I am writing messages to a log file
on logmsg(msg, num, type)
	set curdatestr to ((current date) as string)
	set logentry to "[" & curdatestr & "]" & tab & msg & " (" & num & ")" & return
	if not my itempathexists(logsfolderpath) then
		set logsfolderpath to (((path to desktop) as text) & "Logs:")
		set qtdlogsfolderpath to quoted form of POSIX path of logsfolderpath
		do shell script "mkdir -p " & qtdlogsfolderpath
	end if
	set logfilepath to logsfolderpath & mytitle & "_" & type & ".log"
	if my itempathexists(logfilepath) then
		set logfileinfo to info for (logfilepath as alias)
		set logfilesize to size of logfileinfo
		-- moving big log files to an archive folder
		if logfilesize is greater than 1048576 then
			try
				set timestamp to do shell script "date \"+%Y%m%d_%H%M%S\""
				do shell script "mkdir -p " & quoted form of POSIX path of (logsfolderpath & "old:")
				set oldlogfilepath to logsfolderpath & "old:" & timestamp & "_" & mytitle & "_" & type & ".log"
				set command to "mv " & quoted form of POSIX path of logfilepath & " " & quoted form of POSIX path of oldlogfilepath
				do shell script command
			end try
		end if
	end if
	try
		set openlogfile to open for access logfilepath with write permission
		write logentry to openlogfile starting at eof
		close access openlogfile
	on error
		try
			close access openlogfile
		end try
		return false
	end try
	return true
end logmsg

-- I am indicating if an item path exists or not
on itempathexists(itempath)
	try
		set itemalias to itempath as alias
		return true
	on error
		return false
	end try
end itempathexists

-- I am returning the parent folder path of a given item path
on getparentfolderpath(itempath)
	set olddelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set itemcount to (count text items of itempath)
	set lastitem to the last text item of itempath
	if lastitem = "" then
		set itemcount to itemcount - 2 -- folder path
	else
		set itemcount to itemcount - 1 -- file path
	end if
	set parentfolderpath to text 1 thru text item itemcount of itempath & ":"
	set AppleScript's text item delimiters to olddelims
	return parentfolderpath
end getparentfolderpath

-- I am returning the internal IP of the Mac
on getmyip()
	set myip to do shell script "ifconfig | grep broadcast | cut -f2 -d' '"
	return myip
end getmyip

-- I am a very old search & replace function...
on searchnreplace(searchstr, replacestr, txt)
	considering case, diacriticals and punctuation
		if txt contains searchstr then
			set olddelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {searchstr}
			set txtitems to text items of txt
			set AppleScript's text item delimiters to {replacestr}
			set txt to txtitems as Unicode text
			set AppleScript's text item delimiters to olddelims
		end if
	end considering
	return txt
end searchnreplace

-- I am responsible for creating a RSS feed (2.0)
script RSSFeedCreator
	property newline : ASCII character 10
	property rssitemsdb : {}
	
	-- I am the RSS feed object
	script RSSFeed
		property content : ""
		
		-- I am assembling the complete RSS feed
		on assemble()
			set content to ""
			
			-- filling the RSS channel template
			set channeltemplate to RSSFeedCreator's RSSChannel's template
			set channelelements to RSSFeedCreator's RSSChannel's getelements()
			set channeltags to RSSFeedCreator's RSSChannel's gettags()
			set countchannelelements to length of channelelements
			
			repeat with i from 1 to countchannelelements
				set channelelement to item i of channelelements
				set channeltag to item i of channeltags
				if channelelement is not missing value then
					set channeltemplate to RSSFeedCreator's searchnreplace(channeltag, channelelement, channeltemplate)
				end if
			end repeat
			
			-- creating the string representations of the RSS items
			set rssitems to ""
			set itemtags to RSSFeedCreator's RSSItem's gettags()
			set countrecords to length of rssitemsdb
			repeat with i from 1 to countrecords
				set itemelements to item i of rssitemsdb
				set itemtemplate to RSSFeedCreator's RSSItem's template
				set countitemelements to length of itemelements
				repeat with i from 1 to countitemelements
					set itemelement to item i of itemelements
					set itemtag to item i of itemtags
					if itemelement is not missing value then
						set itemtemplate to RSSFeedCreator's searchnreplace(itemtag, itemelement, itemtemplate)
					end if
				end repeat
				if i is not equal to countrecords then
					set rssitems to rssitems & itemtemplate & RSSFeedCreator's newline
				else
					set rssitems to rssitems & itemtemplate
				end if
			end repeat
			
			-- adding the RSS items to the RSS channel
			set content to RSSFeedCreator's searchnreplace("$rssitems$", rssitems, channeltemplate)
			-- removing empty elements from the RSS feed
			set content to my rememptyelements(content)
		end assemble
		
		-- I am removing lines with empty elements* from the RSS feed
		-- * e.g. "<title>$itemtitle$</title>"
		on rememptyelements(txt)
			set tmpcontent to ""
			set tmplines to paragraphs of txt
			set counttmplines to length of tmplines
			repeat with i from 1 to counttmplines
				set tmpline to item i of tmplines
				if tmpline does not contain ">$" and tmpline does not contain "$<" then
					if tmpline does not contain "\"$" and tmpline does not contain "$\"" then
						if i is not equal to counttmplines then
							set tmpcontent to tmpcontent & tmpline & RSSFeedCreator's newline
						else
							set tmpcontent to tmpcontent & tmpline
						end if
					end if
				end if
			end repeat
			return tmpcontent
		end rememptyelements
	end script
	
	-- I am the RSS channel object
	script RSSChannel
		property templatefilename : "rsschannel.tpl"
		property template : missing value
		-- required RSS channel elements
		property channeltitle : missing value
		property channellink : missing value
		property channeldescription : missing value
		-- optional RSS channel elements
		property channellanguage : missing value
		property channelcopyright : missing value
		property channelpubdate : missing value
		property channellastbuilddate : missing value
		property channeldocs : missing value
		property channelgenerator : missing value
		property channelcategory : missing value
		property channelmanagingeditor : missing value
		property channelwebmaster : missing value
		property channelttl : missing value
		property channelimgurl : missing value
		property channelimgtitle : missing value
		property channelimglink : missing value
		
		-- I am returning all elements
		on getelements()
			return {channeltitle, channellink, channeldescription, channellanguage, channelcopyright, channelpubdate, channellastbuilddate, channeldocs, channelgenerator, channelcategory, channelmanagingeditor, channelwebmaster, channelttl, channelimgurl, channelimgtitle, channelimglink}
		end getelements
		
		-- I am returning all tags
		on gettags()
			return {"$channeltitle$", "$channellink$", "$channeldescription$", "$channellanguage$", "$channelcopyright$", "$channelpubdate$", "$channellastbuilddate$", "$channeldocs$", "$channelgenerator$", "$channelcategory$", "$channelmanagingeditor$", "$channelwebmaster$", "$channelttl$", "$channelimgurl$", "$channelimgtitle$", "$channelimglink$"}
		end gettags
		
		-- I am resetting all elements to their default value
		on resetelements()
			set channeltitle to missing value
			set channellink to missing value
			set channeldescription to missing value
			set channellanguage to missing value
			set channelcopyright to missing value
			set channelpubdate to missing value
			set channellastbuilddate to missing value
			set channeldocs to missing value
			set channelgenerator to missing value
			set channelcategory to missing value
			set channelmanagingeditor to missing value
			set channelwebmaster to missing value
			set channelttl to missing value
			set channelimgurl to missing value
			set channelimgtitle to missing value
			set channelimglink to missing value
		end resetelements
	end script
	
	-- I am the RSS item object
	script RSSItem
		property template : missing value
		-- RSS item elements
		property itemtitle : missing value
		property itemlink : missing value
		property itemdescription : missing value
		property itemauthor : missing value
		property itemcategory : missing value
		property itemcomments : missing value
		property itempubdate : missing value
		property itemenclosureurl : missing value
		property itemenclosurelength : missing value
		property itemenclosuremimetype : missing value
		property itemguid : missing value
		
		-- I am returning all elements
		on getelements()
			return {itemtitle, itemlink, itemdescription, itemauthor, itemcategory, itemcomments, itempubdate, itemenclosureurl, itemenclosurelength, itemenclosuremimetype, itemguid}
		end getelements
		
		-- I am returning all tags
		on gettags()
			return {"$itemtitle$", "$itemlink$", "$itemdescription$", "$itemauthor$", "$itemcategory$", "$itemcomments $", "$itempubdate$", "$itemenclosureurl$", "$itemenclosurelength$", "$itemenclosuremimetype$", "$itemguid$"}
		end gettags
		
		-- I am resetting all elements to their default value
		on resetelements()
			set itemtitle to missing value
			set itemlink to missing value
			set itemdescription to missing value
			set itemauthor to missing value
			set itemcategory to missing value
			set itemcomments to missing value
			set itempubdate to missing value
			set itemenclosureurl to missing value
			set itemenclosurelength to missing value
			set itemenclosuremimetype to missing value
			set itemguid to missing value
		end resetelements
	end script
	
	-- I am initializing the RSSFeedCreator object
	on initialize()
		try
			-- loading the templates for the RSS channel and RSS item
			set mypath to (path to me) as text
			set rsschanneltplpath to mypath & "Contents:Resources:rsschannel.tpl"
			set rssitemtplpath to mypath & "Contents:Resources:rssitem.tpl"
			
			repeat with tplfilepath in {rsschanneltplpath, rssitemtplpath}
				if not my itempathexists(tplfilepath) then
					set errmsg to "The template file does not exist:" & return & return & tplfilepath
					return {false, errmsg}
				end if
			end repeat
			
			set RSSChannel's template to read (rsschanneltplpath as alias)
			set RSSItem's template to read (rssitemtplpath as alias)
			
			-- resetting the channel and item elements
			RSSChannel's resetelements()
			RSSItem's resetelements()
			set rssitemsdb to {}
			
			return {true, missing value}
		on error errmsg number errnum
			return {false, errmsg}
		end try
	end initialize
	
	-- I am exporting the RSS feed to a given file path
	on export(filepath)
		-- assembling the RSS feed...
		RSSFeed's assemble()
		
		-- ...and writing it to the given file
		try
			set openfile to open for access filepath with write permission
			set eof of openfile to 0
			write RSSFeed's content to openfile as «class utf8»
			close access openfile
			return {true, missing value}
		on error errmsg number errnum
			try
				close access openfile
			end try
			return {false, errmsg}
		end try
	end export
	
	-- various RSS channel setters
	on setchanneltitle(channeltitle)
		set RSSChannel's channeltitle to my escape(channeltitle)
	end setchanneltitle
	
	on setchannellink(channellink)
		set RSSChannel's channellink to my escape(channellink)
	end setchannellink
	
	on setchanneldescription(channeldescription)
		set RSSChannel's channeldescription to my escape(channeldescription)
	end setchanneldescription
	
	on setchannellanguage(channellanguage)
		set RSSChannel's channellanguage to channellanguage
	end setchannellanguage
	
	on setchannelcopyright(channelcopyright)
		set RSSChannel's channelcopyright to my escape(channelcopyright)
	end setchannelcopyright
	
	on setchannelpubdate(channelpubdate)
		set RSSChannel's channelpubdate to my getdatestr(channelpubdate)
	end setchannelpubdate
	
	on setchannellastbuilddate(channellastbuilddate)
		set RSSChannel's channellastbuilddate to my getdatestr(channellastbuilddate)
	end setchannellastbuilddate
	
	on setchanneldocs(channeldocs)
		set RSSChannel's channeldocs to my escape(channeldocs)
	end setchanneldocs
	
	on setchannelgenerator(channelgenerator)
		set RSSChannel's channelgenerator to my escape(channelgenerator)
	end setchannelgenerator
	
	on setchannelcategory(channelcategory)
		set RSSChannel's channelcategory to my escape(channelcategory)
	end setchannelcategory
	
	on setchannelmanagingeditor(channelmanagingeditor)
		set RSSChannel's channelmanagingeditor to my escape(channelmanagingeditor)
	end setchannelmanagingeditor
	
	on setchannelwebmaster(channelwebmaster)
		set RSSChannel's channelwebmaster to my escape(channelwebmaster)
	end setchannelwebmaster
	
	on setchannelttl(channelttl)
		set RSSChannel's channelttl to channelttl
	end setchannelttl
	
	on setchannelimgurl(channelimgurl)
		set RSSChannel's channelimgurl to my escape(channelimgurl)
	end setchannelimgurl
	
	on setchannelimgtitle(channelimgtitle)
		set RSSChannel's channelimgtitle to my escape(channelimgtitle)
	end setchannelimgtitle
	
	on setchannelimglink(channelimglink)
		set RSSChannel's channelimglink to my escape(channelimglink)
	end setchannelimglink
	
	on createitem()
		RSSItem's resetelements()
	end createitem
	
	on saveitem()
		set rssitemsdb to rssitemsdb & {my RSSItem's getelements()}
	end saveitem
	
	-- various RSS item setters
	on setitemtitle(itemtitle)
		set RSSItem's itemtitle to my escape(itemtitle)
	end setitemtitle
	
	on setitemlink(itemlink)
		set RSSItem's itemlink to my escape(itemlink)
	end setitemlink
	
	on setitemdescription(itemdescription)
		set RSSItem's itemdescription to my escape(itemdescription)
	end setitemdescription
	
	on setitemauthor(itemauthor)
		set RSSItem's itemauthor to my escape(itemauthor)
	end setitemauthor
	
	on setitemcategory(itemcategory)
		set RSSItem's itemcategory to my escape(itemcategory)
	end setitemcategory
	
	on setitempubdate(itempubdate)
		set RSSItem's itempubdate to my getdatestr(itempubdate)
	end setitempubdate
	
	on setitemenclosureurl(itemenclosureurl)
		set RSSItem's itemenclosureurl to my escape(itemenclosureurl)
	end setitemenclosureurl
	
	on setitemenclosurelength(itemenclosurelength)
		set RSSItem's itemenclosurelength to itemenclosurelength
	end setitemenclosurelength
	
	on setitemenclosuremimetype(itemenclosuremimetype)
		set RSSItem's itemenclosuremimetype to my escape(itemenclosuremimetype)
	end setitemenclosuremimetype
	
	on setitemguid(itemguid)
		set RSSItem's itemguid to my escape(itemguid)
	end setitemguid
	
	on setitemcomments(itemcomments)
		set RSSItem's itemcomments to my escape(itemcomments)
	end setitemcomments
	
	-- I am converting a date object into the following string format:
	-- Tue, 06 Apr 2010 14:23:24 +0200
	on getdatestr(dateobj)
		set dateobj to dateobj
		
		set shortweekdaystr to (characters 1 through 3 of ((weekday of dateobj) as text)) as text
		set daynumstr to (day of dateobj) as text
		if length of daynumstr is 1 then
			set daynumstr to "0" & daynumstr
		end if
		set shortmonthstr to (characters 1 through 3 of ((month of dateobj) as text)) as text
		set yearstr to (year of dateobj) as text
		set hourstr to (hours of dateobj) as text
		if length of hourstr is 1 then
			set hourstr to "0" & hourstr
		end if
		set minstr to (minutes of dateobj) as text
		if length of minstr is 1 then
			set minstr to "0" & minstr
		end if
		set secstr to (seconds of dateobj) as text
		if length of secstr is 1 then
			set secstr to "0" & secstr
		end if
		
		set timestr to (hourstr & ":" & minstr & ":" & secstr)
		
		set gmt to (time to GMT) / 60 / 60
		
		set timezones to {"-1100", "-1000", "-0930", "-0900", "-0800", "-0700", "-0600", "-0500", "-0430", "-0400", "-0300", "-0230", "-0200", "-0100", "+0000", "+0100", "+0200", "+0300", "+0400", "+0430", "+0500", "+0530", "+0545", "+0600", "+0630", "+0700", "+0800", "+0845", "+0900", "+0930", "+1000", "+1030", "+1100", "+1130", "+1200", "+1245", "+1300", "+1400"}
		set gmts to {-11.0, -10.0, -9.5, -9.0, -8.0, -7.0, -6.0, -5.0, -4.5, -4.0, -3.0, -2.5, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 4.5, 5.0, 5.5, 545, 6.0, 6.5, 7.0, 8.0, 845, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 1245, 13.0, 14.0}
		
		set countgmts to length of gmts
		repeat with i from 1 to countgmts
			if item i of gmts is equal to gmt then
				set timezone to item i of timezones
				exit repeat
			end if
		end repeat
		
		set datestr to shortweekdaystr & ", " & daynumstr & " " & shortmonthstr & " " & yearstr & " " & timestr & " " & timezone
		return datestr
	end getdatestr
	
	-- I am a very old search & replace function...
	on searchnreplace(searchstr, replacestr, txt)
		considering case, diacriticals and punctuation
			if txt contains searchstr then
				set olddelims to AppleScript's text item delimiters
				set AppleScript's text item delimiters to {searchstr}
				set txtitems to text items of txt
				set AppleScript's text item delimiters to {replacestr}
				set txt to txtitems as Unicode text
				set AppleScript's text item delimiters to olddelims
			end if
		end considering
		return txt
	end searchnreplace
	
	-- I am indicating if an item path exists or not
	on itempathexists(itempath)
		try
			set itemalias to itempath as alias
			return true
		on error
			return false
		end try
	end itempathexists
	
	-- I am converting the <, &, and > characters to the corresponding entity reference
	on escape(txt)
		set searchstrings to {"&", "<", ">"}
		set replacestrings to {"&", "<", ">"}
		repeat with i from 1 to length of searchstrings
			set searchstr to item i of searchstrings
			set replacestr to item i of replacestrings
			set txt to my searchnreplace(searchstr, replacestr, txt)
		end repeat
		return txt
	end escape
end script