Edit a Sentence

Hey,

This should be simple but somehow nothing I’m trying is working.
I have this script working to replace a word in a sentence but
what I"m after is if the key text is contained in the sentence
to keep that text and remove everything else.

Example: “Fair and sunny” to just “Fair”

tell application "IndigoServer"
	set theText to value of variable "Weather_Condition"
	
	set swapOut to "Thunderstorms" as string
	set swapIn to "T-Storms" as string
	
	if the theText contains the swapOut then
		-- replace target string using delimiters
		set AppleScript's text item delimiters to the swapOut
		set the text_item_list to every text item of theText
		set AppleScript's text item delimiters to the swapIn
		set the theText to the text_item_list as string
		set AppleScript's text item delimiters to ""
	end if
	set the value of variable "Weather_Condition" to theText
end tell

The “IndigoServer” is my home automation software.

Any help greatly appreciated!

Thanks,

Carl

Your script works if what you are looking for is to replace a string with another string, i.e. replace a phrase containing “Thunderstorms” to one containing “T-Storms”. I have removed the bit about IndigoServer because I don’t have it:

(*
tell application "IndigoServer"
	set theText to value of variable "Weather_Condition"
	end tell
*)

set theText to "Thunderstorms are expected."

set swapOut to "Thunderstorms" as string
set swapIn to "T-Storms" as string

if the theText contains the swapOut then
	log theText
	-- replace target string using delimiters
	set AppleScript's text item delimiters to the swapOut
	set the text_item_list to every text item of theText
	log text_item_list
	set AppleScript's text item delimiters to the swapIn
	set the theText to the text_item_list as string
	log theText
	set AppleScript's text item delimiters to ""
end if

(* tell application "IndigoServer"
	set the value of variable "Weather_Condition" to theText
end tell
*)

If however, you want to find any instance of “Fair” in the variable and just reduce the whole phrase to “Fair”, I think it’s even easier:

set theText to "Fair and sunny"

set keepThis to "Fair" as string

if the theText contains the keepThis then
	set the theText to the keepThis as string
	log theText
end if

Perfect. Thanks a bunch!

Curious, if there is a way to test for more than one word?
Sometimes I come across two words in a sentence I need to test for. e.g.: “Fog with Light Rain” which in this
case I would need to set a variable to: “Fog_Rain”

What’s happening is I display a weather icon for tomorrows forecast based on info I download
from a weather site. The info they supply is always changing for some reason. One day it might
be, “Fog and Rain”, then later, “Rain with Fog” making it cumbersome to use.

Really appreciate the help, someday I’ll get a handle on this.

Thanks again,

Carl

Plugging along with this, I got it worked out to convert a “Fog and Rain”, or “Rain and Fog”
to what I need.
The problem now is how to limit one like, “Some Rain” to not be triggered by the above, “Fog and Rain” and only
trigger when just “Rain” is present.

Something like: if the theText contains ONLY the keepThis then

Any ideas appreciated!

Thanks,

Carl

This does it:

set theFirstText to "Fog and Rain"
set theSecondText to "Rain with Fog"


set firstWord to "Fog" as string
set secondWord to "Rain" as string

if the theFirstText contains firstWord and theFirstText contains secondWord then
	set the theFirstText to firstWord & "_" & secondWord as string
	log theFirstText
end if

if the theSecondText contains firstWord and theSecondText contains secondWord then
	set the theSecondText to firstWord & "_" & secondWord as string
	log theSecondText
end if

You should post the code you have “ don’t be shy!

Thanks a bunch. Here’s what I ended up with:

tell application "IndigoServer"
	set theText to value of variable "Weather_Condition"
	if theText contains "Clear" then
		set the value of variable "Weather_Condition" to "Fair"
	else if theText contains "Cloudy" and theText contains "Mostly" then
		set the value of variable "Weather_Condition" to "Cloudy_Mostly"
	else if theText contains "Cloudy" and theText contains "Few" then
		set the value of variable "Weather_Condition" to "Fair"
	else if theText contains "Cloudy" and theText contains "Partly" then
		set the value of variable "Weather_Condition" to "Cloudy_Partly"
	else if theText contains "Fog" and theText contains "Freezing" then
		set the value of variable "Weather_Condition" to "Fog_Freezing"
	else if theText contains "Fog" and theText contains "Mist" then
		set the value of variable "Weather_Condition" to "Fog_Mist"
	else if theText contains "Fog" then
		set the value of variable "Weather_Condition" to "Fog"
	else if theText contains "Haze" then
		set the value of variable "Weather_Condition" to "Haze"
	else if theText contains "Misty" then
		set the value of variable "Weather_Condition" to "Misty"
	else if theText contains "Snow" and theText contains "Fog" then
		set the value of variable "Weather_Condition" to "Snow_Fog"
	else if theText contains "Snow" then
		set the value of variable "Weather_Condition" to "Snow"
	else if theText contains "Sky" and theText contains "Obscure" then
		set the value of variable "Weather_Condition" to "Haze"
	else if theText contains "Sunny" then
		set the value of variable "Weather_Condition" to "Sunny"
	else if theText contains "Windy" then
		set the value of variable "Weather_Condition" to "Windy"
	else if theText contains "Smoke" then
		set the value of variable "Weather_Condition" to "Haze"
	else if theText contains "Fair" then
		set the value of variable "Weather_Condition" to "Fair"
		
	end if
end tell

The trick was to get the single entry, like “Cloudy” below the others that used it in combination with “Partly” etc.

Thanks,

Carl

There may be a problem, depending on how the original info comes in. I’ve removed the IndigoServer bit again to just test the logic. The if statement will work on the first statement that satisfies the condition. The phrase “Mostly Sunny and a bit Misty” will return “Misty”. Don’t know if this is a practical issue or just a theoretical one.

set theText to "Mostly Sunny and a bit Misty"

if theText contains "Clear" then
	set weatherCondition to "Fair"
else if theText contains "Cloudy" and theText contains "Mostly" then
	set weatherCondition to "Cloudy_Mostly"
else if theText contains "Cloudy" and theText contains "Few" then
	set weatherCondition to "Fair"
else if theText contains "Cloudy" and theText contains "Partly" then
	set weatherCondition to "Cloudy_Partly"
else if theText contains "Fog" and theText contains "Freezing" then
	set weatherCondition to "Fog_Freezing"
else if theText contains "Fog" and theText contains "Mist" then
	set weatherCondition to "Fog_Mist"
else if theText contains "Fog" then
	set weatherCondition to "Fog"
else if theText contains "Haze" then
	set weatherCondition to "Haze"
else if theText contains "Misty" then
	set weatherCondition to "Misty"
else if theText contains "Snow" and theText contains "Fog" then
	set weatherCondition to "Snow_Fog"
else if theText contains "Snow" then
	set weatherCondition to "Snow"
else if theText contains "Sky" and theText contains "Obscure" then
	set weatherCondition to "Haze"
else if theText contains "Sunny" then
	set weatherCondition to "Sunny"
else if theText contains "Windy" then
	set weatherCondition to "Windy"
else if theText contains "Smoke" then
	set weatherCondition to "Haze"
else if theText contains "Fair" then
	set weatherCondition to "Fair"
	
end if

log weatherCondition

An if-else block has also priority so the easiest thing to do first is all the multi keywords check and when done start with the highest priority words first. something like this:


if theText contains "Cloudy" and theText contains "Mostly" then
	set weatherCondition to "Cloudy_Mostly"
else if theText contains "Cloudy" and theText contains "Few" then
	set weatherCondition to "Fair"
else if theText contains "Cloudy" and theText contains "Partly" then
	set weatherCondition to "Cloudy_Partly"
else if theText contains "Fog" and theText contains "Freezing" then
	set weatherCondition to "Fog_Freezing"
else if theText contains "Fog" and theText contains "Mist" then
	set weatherCondition to "Fog_Mist"
else if theText contains "Sky" and theText contains "Obscure" then
	set weatherCondition to "Haze"
else if theText contains "Snow" and theText contains "Fog" then
	set weatherCondition to "Snow_Fog"
else if theText contains "Sunny" then
	set weatherCondition to "Sunny"
else if theText contains "Fog" then
	set weatherCondition to "Fog"
else if theText contains "Haze" then
	set weatherCondition to "Haze"
else if theText contains "Clear" then
	set weatherCondition to "Fair"
else if theText contains "Misty" then
	set weatherCondition to "Misty"
else if theText contains "Snow" then
	set weatherCondition to "Snow"
else if theText contains "Windy" then
	set weatherCondition to "Windy"
else if theText contains "Smoke" then
	set weatherCondition to "Haze"
else if theText contains "Fair" then
	set weatherCondition to "Fair"
end if

You guys are awesome. Thanks for the time and tips!

Carl