Determine If An Object is Text vs. Image

Is there a way to determine if an object is text vs. an image in SenseTalk?

For example, I have a function that can take in a text string or an image name, then do some stuff. But first, the function must determine if the object is text or an image before it knows which stuff to do.

I could split the function into someFunctionForText and someFunctionForImage (similar to how Eggplant splits many of its functions, such as ImageFound([image]) and ImageFound([text]), etc.).

However, it has long been desired to make this determination as needed. For example, could all objects ALWAYS have an “objectType” property that would be “text” for text and “image” for image (without knowing what type of object it is and setting the “objectType” property on the object)? Or similar to “is a list”, could there be a function like “is text”, “isImage” or “is an image”, etc.?

So far, I’ve been using variations of a Try/Catch to try the first method (usually attempting to use the given object as an image name), then catch (if not an image) and attempt the second method (using the given object as text).

Have I completely overlooked a better way to determine if a given object is text vs. image?

Hey @bh_eggy ,

Here is an handler that determines whether the Text provided does exist as image in the imagefolder if not it assumes its a text you want to search for on screen.

TextOrImage "MyFolder/Eggplant"
to TextOrImage TextToCheck
    if file (suiteinfo().imagesFolder&"/"&TextToCheck&".png") exists
        logsuccess "Found Image:" && TextToCheck&".png"
	else
		Log TextToCheck &&"is not an image"
    end if
end TextOrImage

Let me know in case you have further questions.

Cheers,
Karsten

1 Like

Hi @Karsten ,

Thank you for the suggestion, this is better than what we were using before, as it does not log an exception.

I adjusted your handler just a smidge to accept both a single image or an image collection:

Put objectType (“imageNameOrTextString”)

to objectType object

If file (suiteinfo().imagesFolder & “/” & object & “.png”) exists
Set objectType to “image”
Else If file (suiteinfo().imagesFolder & “/” & object) exists
Set objectType to “image”
Else
Set objectType to “text”
End If

Return objectType
end objectType

This is exactly what I needed, thank you!!!

1 Like

Hey @bh_eggy,
Great. Thanks for the feedback and contribution.
You can also use the sensetalk keyword folder for your second If.

TextOrImage "MyFolder/Eggplant"
TextOrImage "MyFolder"

to TextOrImage TextToCheck
    if file (suiteinfo().imagesFolder&"/"&TextToCheck&".png") exists 
         logsuccess "Found Image:" && TextToCheck&".png"
else if folder (suiteinfo().imagesFolder&"/"&TextToCheck) exists
        logsuccess "Found Image folder:" && TextToCheck
	else
		Log TextToCheck &&"is not an image"
    end if
end TextOrImage

Second option put it into one if. As it does not matter for sensetalk if its a folder or a file for image searches.

to TextOrImage TextToCheck
    if file (suiteinfo().imagesFolder&"/"&TextToCheck&".png") exists or folder (suiteinfo().imagesFolder&"/"&TextToCheck) exists
         logsuccess "Found Image or Collection:" && TextToCheck&".png"
	else
		Log TextToCheck &&"is not an image"
    end if
end TextOrImage

Cheers,
Karsten

2 Likes