5

I have a script which look for the name and search for a match with a another variable.

It's working fine however if the variable 1 is "Name Demo" and variable 2 is "Demo Name" then the script don't find a match.

set nameMatchTXT to ""
if NameOnDevice contains theName then
    set nameMatch to theName & " : Name Match"
end if

Is there anyway to change this to find the match whatever the order ? PS the script is looking for word wild name, and sometime handle dual bit characters which can be a difficulty.

vadian
  • 274,689
  • 30
  • 353
  • 361
Kevin
  • 1,076
  • 4
  • 22
  • 49

2 Answers2

10

Your request stated:

if the variable 1 is "Name Demo" and variable 2 is "Demo Name" then the script don't find a match.

This will solve that problem:

set var1 to "Name Demo"
set var2 to "Demo Name"

if (var2 contains (word 1 of var1)) and (var2 contains (word 2 of var1)) then
    -- you have a match
    display dialog "var1 and var2 match"
else
    display dialog "no match"
end if
JMichaelTX
  • 1,659
  • 14
  • 19
  • The only issue is I sometime have this error "error "Can’t make word 1 of \"\" into type Unicode text." number -1700 from word 1 of "" to Unicode text " – Kevin May 05 '17 at 05:49
1

You will have to have make a separate check for each condition. There are other ways to do it (such as a complex regex), but this is the easiest and most readable.

set nameMatch1 to "Name"
set nameMatch2 to "Demo"
if (NameOnDevice contains nameMatch1) and (NameOnDevice contains nameMatch2) then
    set nameMatch to NameOnDevice & " : Name Match"
end if

If you are adding matching criteria, you may eventually add more. Instead of adding more variables, and more conditions, you may want to put all of your words in a list and check against that. In the future, if you need to add more words, you can simply add the word to the list. I've extracted it into a separate sub-routine here for easier reading:

on name_matches(nameOnDevice)
    set match_words to {"Name", "Demo"}
    repeat with i from 1 to (count match_words)
        if nameOnDevice does not contain item i of match_words then
            return false
        end if
    end repeat
    return true
end name_matches


if name_matches(nameOnDevice) then
    set nameMatch to nameOnDevice & " : Name Match"
end if

Edit after clarification

If you have no control over the matching text (if it comes from an outside source, and is not coded by you), you can split that text into words and use those as the wordlist in the second example. For instance:

on name_matches(nameOnDevice, match_text)
    set match_words to words of match_text
    repeat with i from 1 to (count match_words)
        if nameOnDevice does not contain item i of match_words then
            return false
        end if
    end repeat
    return true
end name_matches


if name_matches(nameOnDevice, match_text_from_some_other_source) then
    set nameMatch to nameOnDevice & " : Name Match"
end if
Darrick Herwehe
  • 3,553
  • 1
  • 21
  • 30
  • Thank you for your reply, the issue is, the actual name variable is automatically taken from a another script, it's basically put the full name all together – Kevin May 04 '17 at 13:10