• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: if clauses: multiple values in one test?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: if clauses: multiple values in one test?


  • Subject: Re: if clauses: multiple values in one test?
  • From: Christopher Stone <email@hidden>
  • Date: Sun, 21 Sep 2003 04:18:17 -0500

At 00:34 +0200 09/21/2003, Mikael Bystrvm wrought:

How can I shorten a long line of nearly identical tests, except for the value it tests against, in an if clause? Say I have "if (FileName ends with "X") or (FileName ends with "Y") or (FileName ends with ".Z")....... (*plus 26 more or so*) then"
______________________________________________________________________

Requires the Satimage osax:
http://www.satimage.fr/software/en/softx.html


I like the flexibility of regular expressions for this sort of test:

set fileName to "SomeFileName"

try
find text "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)$" in fileName with regexp without case sensitive
set findIt to true
on error
set findIt to false
end try


If you need a more complex if structure you can do something like this:

--
=============================================================================
-->> Find Handler using Satimage osax - returns boolean AND result
--
=============================================================================
on fnd(findStr, usingStr, theText, regexFlags, stringResultFlag, allOcFlag)
try
find text findStr in theText using usingStr regexpflag regexFlags string result stringResultFlag all occurrences allOcFlag with regexp
return true
on error
return false
end try
end fnd


set theText to "How 'bout that!"

if fnd("(how)", "\\1", theText, {"EXTENDED", "NEWLINE", "ICASE"}, true, false) of me then
set theValue to result
else if fnd("('bout)", "\\1", theText, {"EXTENDED", "NEWLINE", "ICASE"}, true, false) of me then
set theValue to result
end if


The cool thing about this hander is that it will evaluate to true or false, *and* if true it will return the found text in the result variable.


Chris
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

References: 
 >if clauses: multiple values in one test? (From: Mikael Byström <email@hidden>)

  • Prev by Date: Quark 6 question?
  • Next by Date: Re: if clauses: multiple values in one test?
  • Previous by thread: Quark 6 question?
  • Next by thread: Re: if clauses: multiple values in one test?
  • Index(es):
    • Date
    • Thread