content: -- Copyright (C) 2012 Robin Trew
  --
  -- 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 : "Toggle .todo mode on or off in selected heading(s)"
  property pVer : "0.03"
  property pAuthor : "Robin Trew"
  
  -- toggle a mode ON or OFF in selected node(s)
  
  property pstrMode : "todo" -- edit this line to adapt for use with other modes
  
  property pstrLineTypes : "@type=heading" -- adjust this to filter for eligible types of line
  -- e.g. property pstrLineTypes : "@type=heading or @type=unordered"
  
  on run
  	ToggleMode(pstrMode)
  end run
  
  on ToggleMode(strModeName)
  	tell application "FoldingText"
  		set lstDocs to documents
  		if lstDocs ≠ {} then
  			tell item 1 of lstDocs
  				-- GET NAME OF ANY CURRENT MODE IN *FIRST* SELECTED HEADER
  				set recSeln to read selection
  				if recSeln is missing value then return
  				set strMode to ""
  				set strPath to (nodePath of recSeln) & " intersect //" & pstrLineTypes
  				try
  					set strMode to mode of (item 1 of (read nodes at path strPath))
  				end try
  				
  				-- TOGGLE BETWEEN MODE NAME AND EMPTY STRING
  				if strMode ≠ strModeName then
  					set strMode to strModeName
  				else
  					set strMode to ""
  				end if
  				
  				-- UPDATE MODE OF *ALL* SELECTED HEADERS
  				update nodes at path strPath with changes {mode:strMode}
  			end tell
  		end if
  	end tell
  end ToggleMode
  
  