Not allowed to save/compile after attaching dialog to window

I’m trying to attach a dialog to a window but if I add "attached to window “tunisonWindow” I can’t save or compile the script (screenshot at http://idisk.patrickcate.com/xcode-error.jpg). I tried adding a barebones dialog to attach to the window (commented out below) and it worked fine. Can anyone tell me the cause of the error? I appreciate any help.

on clicked theObject
	--display dialog "Here is a Dialog" attached to window "tunisonWindow"
	
	if the name of theObject is equal to "backupButton" then
		tell application "iTunes"
			if selection is not {} then
				set selectedTracks to a reference to selection
				set numberOfTracks to (count of selectedTracks)
				repeat with counter from 1 to numberOfTracks
					set currentTrack to item counter of selectedTracks
					set trackMetaData to {played count of currentTrack, played date of currentTrack, skipped count of currentTrack, skipped date of currentTrack, rating of currentTrack}
					set metaBackup to ((item 1 of trackMetaData as string) & "$" & (item 2 of trackMetaData as string) & "$" & (item 3 of trackMetaData as string) & "$" & (item 4 of trackMetaData as string) & "$" & (item 5 of trackMetaData as string))
					
					if metaBackup is equal to (description of currentTrack) then
						--do nothing
					else
						set (description of currentTrack) to metaBackup
					end if
					
				end repeat
				display dialog "All track data has been backed up!" buttons {"Close"} default button 1 with icon 1 attached to window "tunisonWindow"
				display dialog "No tracks selected in iTunes." buttons {"Close"} default button 1 with icon 1
			end if
		end tell
		
	else if the name of theObject is equal to "restoreButton" then
		try
			set oldDelims to AppleScript's text item delimiters --Sets the current AppleScript text delimiter to the variable [oldDelims].
			set AppleScript's text item delimiters to {"$"} --Sets the AppleScript text delimiter to the "$" character.
			
			tell application "iTunes"
				if selection is not {} then
					set selectedTracks to a reference to selection
					set numberOfTracks to (count of selectedTracks)
					repeat with counter from 1 to numberOfTracks
						set currentTrack to item counter of selectedTracks
						set metaRestore to (description of currentTrack)
						set played count of currentTrack to text item 1 of metaRestore
						
						if text item 2 of metaRestore is "missing value" then
							set played date of currentTrack to missing value
						else
							set played date of currentTrack to (date (text item 2 of metaRestore))
						end if
						
						set skipped count of currentTrack to text item 3 of metaRestore
						
						if text item 4 of metaRestore is "missing value" then
							set skipped date of currentTrack to missing value
						else
							set skipped date of currentTrack to (date (text item 4 of metaRestore))
						end if
						
						set rating of currentTrack to text item 5 of metaRestore
					end repeat
				else
					display dialog "No tracks selected in iTunes." buttons {"Close"} default button 1 with icon 1
				end if
				display dialog "All track data has been restored!" buttons {"Close"} default button 1 with icon 1
			end tell
			
			set AppleScript's text item delimiters to oldDelims --Sets the old text delimiter stored in the variable [oldDelims] back to the default AppleScript text delimiter as the last thing executed by the script.
			
		on error
			set AppleScript's text item delimiters to oldDelims --Sets the old text delimiter stored in the variable [oldDelims] back to the default AppleScript text delimiter if there is a error.
			display dialog "Backup or Restore could not complete." buttons {"Close"} default button 1 with icon 1 --Error message shown when the script has an unrecoverable error in the script.
		end try
	end if
	
end clicked

on dialog ended theObject
	
	tell application Tunison
		show window tunisonWindow
	end tell
end dialog ended

Hi,

I guess, the problem is the display dialog lines in the (iTunes) application tell block.
It’s generally recommended to avoid AppleScript Studio code in any application tell block

Yep, that was the problem. I was able to get things working by moving the dialog code as a subroutine.