Display Notification dialog based upon string search result

I have created several scripts which change specific string content of xml documents in TextWrangler. This effectively allows the converted document to work in an earlier version of a software application. I have achieved this in the scripts by various find and replace commands, go to line x and delete etc. The scripts are working well, but if possible I’d like to add an extra level of sophistication, which I’m struggling with.

What I want to do is, incorporate at the beginning of each script, code to search for a specific word or string in the currently open document, and;

a. if the specific word or string is found then I want a notification dialog to pop up stating something like “Rotation” is not supported in the converted version, the script will now abort". At which point the script ends without editing the document.

b. if the above word or string is not found, then the script will run to the end, converting the entire open document.

Does anyone have a suggestion, or know if this is even possible?

David

Maybe this code snippet points you in the right direction:


tell application "TextWrangler"
	tell document 1
		if contents contains "Martin" then
			display dialog "This document contains the word 'Martin'!"
			return
		else
			-- more code to follow
		end if
	end tell
end tell

Many thanks, Martin.

I am a total novice and your snippet has helped tremendously. You would not believe the combinations I had tried, only to receive script errors. Taking your snippet further, I was able to also search for different strings and return different dialog messages with each. I first guessed at using if contents contains “term 1” or “term 2” in the script, but could not get that to work. After further trial and error, I came up with a longer version of the script which does work.

Now to find a way of aborting the script if any of the search terms are found.

tell application "TextWrangler"
	tell document 1
		if contents contains "dab-type=\"BlendFlat\"" then
			display dialog "The Dab Type is only supported in Painter 11 and above"
		else
			if contents contains "BlendCamelHair" then
				display dialog "This Dab Type is only supported in Painter X and above"
			else
				if contents contains "word3" then
					display dialog "This Dab Type is only supported in Painter 8 and above"
					return
				end if
			end if
		end if
	end tell
end tell

Thank you again,

David

I had problems in persuading TextWrangler to execute the find and replace commands if a previous display dialog had not been triggered, but finally found a solution which worked for me, as demonstrated in the cat, mouse, dog example below;

tell application "TextWrangler"
	tell document 1
		if contents does not contain "cat" then
			display dialog "Text string not found" with icon stop
		else
			if contents contains "dog" then
				display dialog "Text string not found" with icon stop
			else
				if contents does not contain "dog" then
					tell application "TextWrangler"
						activate
						replace "Cat" using "Leopard" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
						replace "mouse" using "Mighty Mouse" searching in text 1 of text document 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
					end tell
				end if
			end if
		end if
	end tell
end tell

David