content: 
  -- Permission is hereby granted, free of charge, 
  -- to any person obtaining a copy of this software 
  -- and associated documentation files (the "Software"), 
  -- to deal in the Software without restriction, 
  -- including without limitation the rights to use, copy, 
  -- modify, merge, publish, distribute, sublicense, 
  -- and/or sell copies of the Software, and to permit persons 
  -- to whom the Software is furnished to do so, 
  -- subject to the following conditions:
  
  -- *******
  -- The above copyright notice and this permission notice 
  -- shall be included in ALL copies 
  -- or substantial portions of the Software.
  -- *******
  
  -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  -- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
  -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
  -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
  -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
  -- OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  
  
  property pTitle : "Select theme and restart FT"
  property pVer : "0.03"
  property pAuthor : "Rob Trew"
  
  -- Ver 0.02 Fixes the behaviour when pblnBackup is set to false
  -- Ver 0.03 Adds options to reset to factory default (deactivate user theme)
  --			or restore from an earlier backup
  
  property pblnTimeStampBackups : false -- edit to false if you don't want a stream of time-stamped backups of displaced themes
  
  -- ASSUMES THAT YOU HAVE PLACED TWO OR MORE THEMES, WITH NAMES OTHER THAN USER.FTTHEME,
  -- IN THE THEME FOLDER (set pstrThemeFolder below)
  
  property pstrThemeFolder : "$HOME/Library/Containers/com.foldingtext.FoldingText/Data/Library/Application Support/FoldingText/Themes/"
  property pstrThemeExtn : ".fttheme"
  property pActiveTheme : "User"
  property pstrBackup : "Backup"
  property pstrFactory : "<Factory default - no user theme>"
  property pstrRestore : "<Restore earlier theme>"
  
  on run
  	-- GET LISTS OF:
  	-- 1. THE THEMES IN THE THEME FOLDER WHICH ARE *NOT* CALLED USER.FTTHEME
  	-- 2. ANY BACKUP THEME FOLDERS
  	set {lstThemes, lstBackup} to {{}, {}}
  	set {blnUser, blnTheme, blnBackup} to {false, false, false}
  	repeat with oTheme in (paragraphs of (do shell script "ls \"" & pstrThemeFolder & "\" | sort"))
  		if ((oTheme begins with "User.") and (oTheme ends with pstrThemeExtn)) then
  			set blnUser to true
  		else if (oTheme begins with pstrBackup) and (oTheme ends with pstrThemeExtn) then
  			set end of lstBackup to text 1 thru -9 of (contents of oTheme)
  			set blnBackup to true
  		else
  			set end of lstThemes to text 1 thru -9 of (contents of oTheme)
  			set blnTheme to true
  		end if
  	end repeat
  	
  	-- MENU LOOP
  	set blnEsc to false
  	repeat until blnEsc
  		-- IF NO NAMED OR USER OR BACKUP THEMES ARE FOUND, PROMPT USER AND EXIT
  		if not (blnUser or blnTheme or blnBackup) then
  			tell application "Finder"
  				display dialog "No user themes or backups found ..." & return & return & ¬
  					"Copy one or more " & pstrThemeExtn & " themes to:" & return & return & ¬
  					pstrThemeFolder buttons {"OK"} default button "OK" with title pTitle & "  ver. " & pVer
  				
  				do shell script "open \"" & pstrThemeFolder & "\""
  				activate
  				return
  			end tell
  		else if blnTheme then -- CHOOSE FROM ANY NAMED THEMES (+ OPTION TO CLEAR OR RESTORE)
  			set lstChoice to lstThemes
  			if blnUser then set end of lstChoice to pstrFactory
  			if blnBackup then set end of lstChoice to pstrRestore
  		else if blnBackup then -- CHOOSE FROM ANY BACKUPS (+ OPTION TO REVERT TO FACTORY THEME SETTINGS)
  			set lstChoice to lstBackup
  			if blnUser then set end of lstChoice to pstrFactory
  		end if
  		
  		-- OFFER A MENU OF NAMED THEMES OR BACKUPS (AND ANY ADDITIONAL RESET OR RESTORE CHOICES)
  		tell application id "sevs"
  			activate
  			set varChoice to choose from list lstChoice with title pTitle & tab & pVer with prompt ¬
  				"Option:" default items {item 1 of lstChoice} ¬
  				OK button name "OK" cancel button name "Cancel" with empty selection allowed without multiple selections allowed
  		end tell
  		if varChoice = false then return missing value
  		set strChoice to item 1 of varChoice
  		
  		if strChoice = pstrRestore then
  			set {blnUser, blnTheme, blnBackup} to {false, false, true} -- repeat loop choosing from backup files
  		else if strChoice = pstrFactory then
  			SwitchTo("") -- backup and de-activate User.fttheme by renaming it
  			set blnEsc to true
  		else
  			SwitchTo(strChoice) -- activate the chosen them
  			set blnEsc to true
  		end if
  	end repeat
  	
  	if strChoice ≠ pstrFactory then
  		-- ACTIVATE CHOSEN THEME
  		SwitchTo(strChoice)
  	else
  		-- OR DEACTIVATE USER THEME, RENAMING TO BACKUP OPTIONALLY FOLLOWED BY TIME-STAMP
  		SwitchTo("")
  	end if
  	
  	-- RELAUNCH FOLDINGTEXT
  	do shell script "open -a FoldingText.app"
  	tell application "FoldingText" to activate
  end run
  
  -- Remove any User.ftTheme (backing it up) to revert to default theme
  -- or in preparation for activating a new theme
  on ClearTheme()
  	set strThemeFolder to do shell script "echo \"" & pstrThemeFolder & "\""
  	
  	-- First remove any backup with a clashing name
  	set strTimeStamp to ""
  	if pblnTimeStampBackups then set strTimeStamp to TimeString()
  	set strBackup to quoted form of (strThemeFolder & pstrBackup & strTimeStamp & pstrThemeExtn)
  	if IsFolder(strBackup) then do shell script "rm -R " & strBackup
  	
  	-- Then rename existing theme 
  	set strActiveTheme to quoted form of GetThemePath(pActiveTheme)
  	do shell script "mv " & strActiveTheme & space & strBackup
  end ClearTheme
  
  on SwitchTo(strTheme)
  	try
  		tell application "FoldingText" to quit
  	end try
  	
  	-- BACK UP ANY EXISTING THEME TO A TIME-STAMPED COPY OR A SIMPLE BACKUP FILE
  	set strActiveTheme to quoted form of GetThemePath(pActiveTheme)
  	if IsFolder(strActiveTheme) then ClearTheme()
  	
  	-- MAKE AN ACTIVE COPY OF ANY CHOSEN THEME
  	if strTheme ≠ "" then
  		set strCMD to "cp -R " & quoted form of GetThemePath(strTheme) & space & strActiveTheme
  		do shell script strCMD
  	end if
  end SwitchTo
  
  on GetThemePath(strTheme)
  	do shell script "echo \"" & pstrThemeFolder & strTheme & pstrThemeExtn & "\""
  end GetThemePath
  
  on IsFolder(strPath)
  	(do shell script ("test -d " & strPath & "; echo $?")) = "0"
  end IsFolder
  
  on TimeString()
  	do shell script "date \"+%Y-%m-%d-%Hh%Mm%Ss\""
  end TimeString
  