Some Text Editing Help?

Posted on
Fri Aug 03, 2012 2:52 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Some Text Editing Help?

Hey,

Always get stuck on something in Applescript land.

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 setting up variables.

In 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.

Essentialy looking for some code like this: if the theText contains ONLY the keepThis then

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

Thanks again,

Carl

Here's the general idea:

Code: Select all
tell application "IndigoServer"
   set theText to value of variable "Weather_Condition"
   set keepThis1 to "Cloudy" as string
   set keepThis2 to "Fair" as string
   set keepThis3 to "Mist" as string
   set keepThis4 to "Fog" as string
   set keepThis5 to "Freezing" as string
   set keepThis6 to "Hail" as string
   if the theText contains the keepThis1 then
      set the value of variable "Weather_Condition" to "Cloudy"
   end if
   if the theText contains the keepThis2 then
      set the value of variable "Weather_Condition" to "Fair"
   end if
   if the theText contains the keepThis3 and theText contains the keepThis4 then
      set the value of variable "Weather_Condition" to "Fog_Mist"
   end if
   if the theText contains the keepThis4  the keepThis3 then
      set the value of variable "Weather_Condition" to "Fog"
   end if

Posted on
Fri Aug 03, 2012 4:25 pm
jay (support) offline
Site Admin
User avatar
Posts: 18224
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Some Text Editing Help?

Your script above is almost right - you just missed a couple of things. You also had lots of extra "the"s which I removed since they aren't necessary and made reading the script a little confusing. Also, in order to keep the script from continuing on through the if statements after it had already found a match, I changed each consecutive "if" to an "else if" - so either 0 or 1 of the if statements will fire and nothing else. I personally wouldn't make clearly readable strings like "Cloudy" and "Fog" into unreadable variables (keepThis*) - just use the string.

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.


I think your 3rd "if" is close. I think this, for instance, would work in that particular scenario (it's the last else if):

Code: Select all
tell application "IndigoServer"
   set theText to value of variable "Weather_Condition"
   if theText contains "Cloudy" then
      set the value of variable "Weather_Condition" to "Cloudy"
   else if theText contains "Fair" then
      set the value of variable "Weather_Condition" to "Fair"
   else if theText contains "Mist" and theText contains "Fog" 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 "Rain" and theText contains "Fog" then
      set the value of variable "Weather_Condition" to "Fog_Rain"
   end if
end tell

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Aug 03, 2012 9:38 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Some Text Editing Help?

Perfect, as always. Thanks to your help I'm able to can about 30 triggers.


Carl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 28 guests