0

I have an applescript that gets a string at one point and checks if it contains another string that I inputted.

So lets says I have "Im a string" and I want to search if it contains "string". I have this working by using "if searchString is in mainString then...."

But if I pass it an empty string this should equate to true no matter what the string is, but for some reason it isnt working.

How could I check if an empty string is in another string?

2 Answers 2

2

Different languages are going to handle this differently, and what you are seeing here is how Applescript handles it [1]. Any empty string is exactly that: empty. "" has no value in Applescript (but it's neither null or missing a value; there is just nothing in it). If you were comparing two empty strings, then they would equate to each other, otherwise either a string is empty or it isn't and populated string can't contain an empty string.

You'll have to check to see if the search string is empty first and then handle that accordingly.

[1] REALbasic won't find an empty string in a populated string either.

update per comment:

Yes, but I have alwasys found it better to do the calculations of the condition outside of the if...then block with Applescript:

set condition1 to true -- "true" being a calculation of some kind
set condition2 to true

if (condition1) and (condition2) then
    -- do something
end if
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Is there a way to use the if statement in Applescript in the following way: "if [condition 1] or [condition 2] then ..." ?
1

What about first checking whether the string-to-search-for is empty, and then return 'true'?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.