Saturday, February 27, 2021

#1 2021-01-26 01:11:19 am

escoolioinglesias
Member
Registered: 2021-01-15
Posts: 15

Restricted Input

Applescript:

display dialog "type whatever key you want, except for some (๑ ऀืົཽ₍₍ළ₎₎ ऀืົཽ)✧" default answer ""
   set the keyTyped to the text returned of the result

is there an easy way to create restrictions of acceptable character inputs, i.e., without making a slew of if statements for each undesired character?

is text returned from display dialog a silly way to do this?

as always, thank you for your support !

Offline

 

#2 2021-01-26 04:07:49 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1637

Re: Restricted Input

Hi,

my answer on your second question is NO. You have filter the input yourself. Here, comes the answer on your first question. One way I know is this:

Applescript:


set theChars to characters of "(๑ ऀืົཽ₍₍ළ₎₎ ऀืົཽ)✧"

repeat
   display dialog "type whatever key you want, except for some (๑ ऀืົཽ₍₍ළ₎₎ ऀืົཽ)✧" default answer ""
   set the keyTyped to the text returned of the result
   set goodInput to true
   repeat with aChar in theChars
       if keyTyped contains aChar then
           set goodInput to false
           exit repeat
       end if
   end repeat
   if goodInput then exit repeat
   display dialog "Bad input. Try again, or cancel."
end repeat

Last edited by KniazidisR (2021-01-26 04:08:54 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2021-01-27 08:26:12 pm

escoolioinglesias
Member
Registered: 2021-01-15
Posts: 15

Re: Restricted Input

KniazidisR wrote:


Applescript:


set theChars to characters of "(๑ ऀืົཽ₍₍ළ₎₎ ऀืົཽ)✧"



Awesome, thanks !

Is there a way to make the characters independent??

set theChars to character of {"a", "b", "c", "d"}
^ that didn't work, but something like that?

so you could type abcd or any combination thereof, but you can't input JUST a, or JUST b, or JUST c or JUST d??

Offline

 

#4 2021-01-27 10:58:12 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1637

Re: Restricted Input

escoolioinglesias wrote:

Is there a way to make the characters independent??

set theChars to character of {"a", "b", "c", "d"}
^ that didn't work, but something like that?

so you could type abcd or any combination thereof, but you can't input JUST a, or JUST b, or JUST c or JUST d??


Hi, again.

My suggestion is:

Applescript:


set theChars to characters of "(๑ ऀืົཽ₍₍ළ₎₎ ऀืົཽ)✧"

repeat
   display dialog "type whatever key you want, except for some (๑ ऀืົཽ₍₍ළ₎₎ ऀืົཽ)✧" default answer ""
   set the keyTyped to the text returned of the result
   set goodInput to true
   repeat with aChar in theChars
       if keyTyped contains aChar then
           set goodInput to false
           exit repeat
       end if
   end repeat
   if goodInput and (length of keyTyped > 1) then exit repeat
   display dialog "BAD INPUT FOUNDED:

Not acceptable character(s), OR
the length of the text is less than 2.

Try again, or cancel."

end repeat

OR, little more effective:

Applescript:


set theChars to characters of "(๑ ऀืົཽ₍₍ළ₎₎ ऀืົཽ)✧"

repeat
   display dialog "type whatever key you want, except for some (๑ ऀืົཽ₍₍ළ₎₎ ऀืົཽ)✧" default answer ""
   set the keyTyped to the text returned of the result
   set goodInput to true
   repeat with aChar in theChars
       if (length of keyTyped < 2) or (keyTyped contains aChar) then
           set goodInput to false
           exit repeat
       end if
   end repeat
   if goodInput then exit repeat
   display dialog "BAD INPUT FOUNDED:

Not acceptable character(s), OR
the length of the text is less than 2.

Try again, or cancel."

end repeat

NOTE: maybe, I don't understood you, and you want accept combination of not acceptable characters in the input, but not single not acceptable characters in the input. If this is true, then you should just replace contains with is (in the my first suggestion).

Last edited by KniazidisR (2021-01-28 07:05:12 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2021-01-28 10:27:56 am

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

Re: Restricted Input

KniazidisR wrote:

NOTE: maybe, I don't understood you, and you want accept combination of not acceptable characters in the input, but not single not acceptable characters in the input. If this is true, then you should just replace contains with is (in the my first suggestion).



I also wasn't sure what the OP wanted. I thought he might want to report an error when a prohibited character is a separate word (i.e. spaces before and after).

Applescript:

set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" "}

set theString to text items of "a table" -- error due to prohibited word "a"
--set theString to text items of "r table" -- no error

set theChars to {"a", "b", "c", "d"} -- prohibited characters

repeat with anItem in theString
   if (count anItem) = 1 and anItem is in theChars then
       display dialog "Erroneous character found" buttons "OK" cancel button 1 default button 1
   end if
end repeat

set AppleScript's text item delimiters to ATID

Last edited by peavine (2021-01-29 07:41:10 am)


2018 Mac mini - macOS Catalina

Offline

 

#6 2021-01-29 11:04:23 pm

escoolioinglesias
Member
Registered: 2021-01-15
Posts: 15

Re: Restricted Input

I really appreciate both of your responses!
I managed to do what I needed with a combination of your answers, plus gain some more robust knowledge of AppleScript. For that, I'm grateful and I will be more thoughtful next time in being more clear in my questions.

Much appreciated!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)