All pastes #584615 Raw Edit

complex

public text v1 · immutable
·published 2007-06-22 21:06 UTC
rendered paste body
(*Actions.scpt

Intended for use as a template for context menu script actions
*)
--and here's how you save preferences
property prefs : missing value
property prefsPath : missing value

using terms from application "Colloquy"
	on build contextual menu for item blah
		try
			my loadPrefs()
			if class of blah is member then
				return {"Slap!", {title:"Actions", alternate:1, submenu:{"Pounce!", {title:"Slap with...", submenu:slapitems of prefs & {"-", "Add...", {title:"Edit slap list...", submenu:slapitems of prefs}}}, "Poke"}}, "Add to slap list", {title:"Edit slap list...", submenu:slapitems of prefs, alternate:1}, "-"}
			else if class of blah is Unicode text then
				return {"Add to slap list"}
			else
				return {}
			end if
		on error err
			return {{title:"Error", submenu:{err}}}
		end try
	end build contextual menu for item
	
	on handle clicked contextual menu item theItem for theUser within submenus
		try
			my loadPrefs()
			tell application "Colloquy"
				if submenus is {} then
					if theItem is "Slap!" then
						tell chat room panel of theUser to send message "slaps " & name of theUser & " around a bit with " & some item of (slapitems of prefs) with action tense
					else if theItem is "Add to slap list" then
						try
							set theUser to name of theUser
						end try
						set slapitems of prefs to slapitems of prefs & {theUser}
						my savePrefs()
					end if
				else if submenus is {"Actions"} then
					if theItem is "Pounce!" then
						tell chat room panel of theUser to send message "pounces " & name of theUser & "!" with action tense
					else if theItem is "Poke" then
						tell chat room panel of theUser to send message "pokes " & name of theUser with action tense
					end if
				else if last item of submenus is "Slap with..." then
					if theItem is "Add..." then
						try
							set ans to text returned of (display dialog "Add what item?" default answer "a large trout")
						on error
							return
						end try
						if (slapitems of prefs) does not contain ans then
							set (slapitems of prefs) to (slapitems of prefs) & {ans}
							my savePrefs()
						end if
					else
						tell chat room panel of theUser to send message "slaps " & name of theUser & " around a bit with " & theItem with action tense
					end if
				else if last item of submenus is "Edit slap list..." then
					repeat with n from 1 to number of items in (slapitems of prefs)
						if item n of (slapitems of prefs) is theItem then
							exit repeat
						end if
					end repeat
					try
						if number of items of slapitems of prefs > 1 then
							set newItem to display dialog "Change to?" default answer (item n of (slapitems of prefs)) buttons {"Change", "Remove", "Cancel"} default button 1
						else
							set newItem to display dialog "Change to?" default answer (item n of (slapitems of prefs)) buttons {"Change", "Cancel"} default button 1
						end if
					on error
						return
					end try
					if button returned of newItem is "Change" then
						set item n of (slapitems of prefs) to text returned of newItem
						my savePrefs()
					else if button returned of newItem is "Remove" then
						if number of items in (slapitems of prefs) > 1 then
							set (slapitems of prefs) to my DeleteNthItem((slapitems of prefs), n)
						else
							error "Cannot delete the last item from the list"
						end if
						my savePrefs()
					end if
				end if
			end tell
		on error err
			display dialog err
		end try
	end handle clicked contextual menu item
	
	on process user command c with arguments for view
		try
			my loadPrefs()
			if c is "slap" then
				tell view to send message "slaps " & arguments & " around a bit with " & some item of (slapitems of prefs) with action tense
			end if
		on error err
			return {{title:"Error", submenu:{err}}}
		end try
	end process user command
end using terms from


on DeleteNthItem(theList, theItemNumber)
	set listCount to count of theList
	if theItemNumber = 1 or theItemNumber = -listCount then
		set theList to rest of theList
	else if theItemNumber = -1 or theItemNumber = listCount then
		set theList to items 1 thru -2 of theList
	else
		set theList to (items 1 thru (theItemNumber - 1) of theList) & (items (theItemNumber + 1) thru -1 of theList)
	end if
	return (theList)
end DeleteNthItem


script prefsTemplate
	property slapitems : {"a large trout", "a large bass"}
end script

on loadPrefs()
	if prefs is missing value then
		set prefsPath to (path to preferences folder from user domain as Unicode text) & "Colloquy Prefs - Actions Menu"
		try
			set prefs to load script alias prefsPath
		on error
			set prefs to prefsTemplate
		end try
	end if
end loadPrefs

on savePrefs()
	if prefs is not missing value then store script prefs in file prefsPath replacing yes
end savePrefs