Tuesday, September 27, 2022

#1 2022-06-09 10:25:18 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1458

Check an array for empty items and non-integers

I have a task that I want to do with ASObjC but it has me stumped.

I need to verify that every item of an array (which is made from a string) contains a value that can be made into an integer. The following script verifies that an item is not blank, but I don't know how to verify that an item can be made into an integer. I thought I could employ the integerValue property, but that simply returns 0 if an item is a letter. I can do this with basic AppleScript but would like to stick with ASObjC in this case. Thanks for the help.

Applescript:

use framework "Foundation"
use scripting additions

set theString to current application's NSString's stringWithString:"2022/06/" -- error - OK
set theString to current application's NSString's stringWithString:"2022/06/1a" -- should error - fix
set theString to current application's NSString's stringWithString:"2022/06/01" -- no error - OK

set theArray to (theString's componentsSeparatedByString:"/")
set thePredicate to current application's NSPredicate's predicateWithFormat:"self == ''"
set blankCheck to (theArray's filteredArrayUsingPredicate:thePredicate)'s |count|()
if blankCheck > 0 then error "A blank value"
display dialog "No blank items"

Last edited by peavine (2022-06-09 10:50:08 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#2 2022-06-09 12:33:10 pm

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 163

Re: Check an array for empty items and non-integers

Here's one way with using the 'NSScanner' class.

Applescript:


use framework "Foundation"

set theString to current application's NSString's stringWithString:"2022/06/1a"
set theStringArray to theString's componentsSeparatedByString:"/"

repeat with theValue in theStringArray
   set theScanner to (current application's NSScanner's scannerWithString:theValue)
   if (theScanner's scanInt:(missing value)) and (theScanner's isAtEnd()) then
       log "A Valid Integer String = " & theValue
   else
       log "Not A Valid Integer String = " & theValue
   end if
end repeat

There is probably a way to do it with checking the strings characters in an 'NSCharacterSet', but I haven't checked that out.

Regards Mark

Offline

 

#3 2022-06-09 12:56:37 pm

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 163

Re: Check an array for empty items and non-integers

You can also use an 'NSCharacterSet' to perform the same as I previously posted.

Applescript:


use scripting additions
use framework "Foundation"

set theString to current application's NSString's stringWithString:"2022/06/1a"
set theStringArray to theString's componentsSeparatedByString:"/"

set nonNumericCharSet to (current application's NSCharacterSet's characterSetWithCharactersInString:"0123456789")'s invertedSet()

repeat with theValue in theStringArray
   if (theValue's rangeOfCharacterFromSet:nonNumericCharSet)'s |length|() = 0 then
       log "A Valid Integer String = " & theValue
   else
       log "Not A Valid Integer String = " & theValue
   end if
end repeat

I'm sure there are probably a few other ways to it as well, but these two are a good starting point.

Regards Mark

Offline

 

#4 2022-06-09 01:07:38 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5563

Re: Check an array for empty items and non-integers

Hi peavine.

This is similar to your original:

Applescript:

use framework "Foundation"
use scripting additions

set theString to current application's NSString's stringWithString:"2022/06/" -- error - OK
set theString to current application's NSString's stringWithString:"2022/06/1a" -- should error - fix
set theString to current application's NSString's stringWithString:"2022/06/01" -- no error - OK

set theArray to (theString's componentsSeparatedByString:"/")
set thePredicate to current application's NSPredicate's predicateWithFormat:"self MATCHES '[0-9]++'"
set allIntable to ((theArray's filteredArrayUsingPredicate:thePredicate)'s isEqualToArray:(theArray)) -- as boolean


NG

Offline

 

#5 2022-06-09 06:26:55 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1458

Re: Check an array for empty items and non-integers

Thanks Mark and Nigel for the great suggestions. BTW, I don't recall every using NSScanner--I'll have to spend some time with that.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#6 2022-06-10 11:19:17 pm

technomorph
Member
Registered: 2017-12-14
Posts: 279

Re: Check an array for empty items and non-integers

You could also try respondsToSelector:(SEL)aSelector
And pass “integerValue” as
The selector
aSelector = @selector(integerValue)
Or
aSelector = NSSelectorSelectorFromString(@“integerValue”)

Offline

 

#7 2022-06-11 08:41:51 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1458

Re: Check an array for empty items and non-integers

Thanks technomorph for the suggestion, but I can't seem to get it to work. What am I doing wrong?

Applescript:

use framework "Foundation"
use scripting additions

set theString to current application's NSString's stringWithString:"a" --> true
set theString to current application's NSString's stringWithString:"1" --> true
set theResult to theString's respondsToSelector:"integerValue"


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#8 2022-06-11 09:25:00 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5563

Re: Check an array for empty items and non-integers

My reading of the documentation is that theString's respondsToSelector:"integerValue" returns whether or not theString's class responds to or implements the integerValue() selector, not whether or not theString itself can actually be coerced to integer.


NG

Offline

 

#9 2022-06-12 08:52:50 pm

technomorph
Member
Registered: 2017-12-14
Posts: 279

Re: Check an array for empty items and non-integers

Yeah that makes sense.
I think Nigel’s method with the predicate using
A RegEx is way to go.
Could also use (\d+)

Another option would be NSScanner.
Which you can use to also extract the integer
Even if it’s embededd “it will Find 1234 in this string”

https://developer.apple.com/documentati … caninteger

It requires the use of a pointer value which can
Be tricky in AppleScript

Offline

 

#10 2022-06-13 11:00:01 am

robertfern
Member
Registered: 2011-11-29
Posts: 195

Re: Check an array for empty items and non-integers

This is such an easy thing to do with vanilla AppleScript.
Why do you need to use AppleScriptObjC?

Applescript:

set theString to "2022/06/1a"
set text item delimiters to "/"
set theArray to text items of theString
repeat with myitem in theArray
   try
       myitem as integer
       log "A Valid Integer String = " & myitem
   on error
       log "Not a Valid Integer String = " & myitem
   end try
end repeat

Last edited by robertfern (2022-06-13 11:02:40 am)

Offline

 

#11 2022-06-13 02:46:20 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1458

Re: Check an array for empty items and non-integers

robertfern wrote:

This is such an easy thing to do with vanilla AppleScript.
Why do you need to use AppleScriptObjC?



I am working to learn ASObjC and that's the main reason I requested an AppleScriptObjC solution. I learned from both Mark's and Nigel's suggestions, and I appreciate them taking the time to respond to my post.

In my testing, Nigel's suggestion is significantly faster than basic AppleScript if the list is quite large. In my case, the list was small and there was no compelling reason to use ASObjC.

Last edited by peavine (2022-06-13 06:47:28 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)