Highlighted

AppleScript - Validate the Input as Number

Enthusiast ,
Aug 26, 2020

Copy link to clipboard

Copied

Dear All,

 

I am newbie to AppleScript.

 

My request is, the user has to feed the information like "number of jobs count" into the user interface.

 

Suppose, the user feed the wrong format like string, empty value etc... 

 

At that time, tool will validate and prompt accordingly.

 

I tried the below code, but it not works good

 

set myInteger to display dialog "Number of Jobs" default answer ""
set myReturn to text returned of myInteger


try
	set myOutput to myReturn as integer
end try



if myOutput is "" then
	display dialog "Input is empty"
	return
else if class of myOutput is integer then
	display dialog "Yes! It's a number!"
else
	display dialog "NO! It's a not number!"
	return
end if


display dialog "Process Done"

 

Looking forward your support!

Any other best way is highly appreciable.

 

 

Thanks

 

 

Adobe Community Professional
Correct answer by rob day | Adobe Community Professional

Try this:

 

set myInteger to display dialog "Number of Jobs" default answer ""
set myReturn to text returned of myInteger

if myReturn is "" then
	display dialog "Input is empty"
	return
end if

try
	set myReturn to myReturn as integer
on error
	display dialog "NO! It's a not number!"
	return
end try

display dialog "Yes! It's a number!"

Topics

Scripting

Views

46

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

AppleScript - Validate the Input as Number

Enthusiast ,
Aug 26, 2020

Copy link to clipboard

Copied

Dear All,

 

I am newbie to AppleScript.

 

My request is, the user has to feed the information like "number of jobs count" into the user interface.

 

Suppose, the user feed the wrong format like string, empty value etc... 

 

At that time, tool will validate and prompt accordingly.

 

I tried the below code, but it not works good

 

set myInteger to display dialog "Number of Jobs" default answer ""
set myReturn to text returned of myInteger


try
	set myOutput to myReturn as integer
end try



if myOutput is "" then
	display dialog "Input is empty"
	return
else if class of myOutput is integer then
	display dialog "Yes! It's a number!"
else
	display dialog "NO! It's a not number!"
	return
end if


display dialog "Process Done"

 

Looking forward your support!

Any other best way is highly appreciable.

 

 

Thanks

 

 

Adobe Community Professional
Correct answer by rob day | Adobe Community Professional

Try this:

 

set myInteger to display dialog "Number of Jobs" default answer ""
set myReturn to text returned of myInteger

if myReturn is "" then
	display dialog "Input is empty"
	return
end if

try
	set myReturn to myReturn as integer
on error
	display dialog "NO! It's a not number!"
	return
end try

display dialog "Yes! It's a number!"

Topics

Scripting

Views

47

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Aug 26, 2020

Copy link to clipboard

Copied

Try this:

 

set myInteger to display dialog "Number of Jobs" default answer ""
set myReturn to text returned of myInteger

if myReturn is "" then
	display dialog "Input is empty"
	return
end if

try
	set myReturn to myReturn as integer
on error
	display dialog "NO! It's a not number!"
	return
end try

display dialog "Yes! It's a number!"

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Aug 26, 2020

Copy link to clipboard

Copied

If you need a dialog for an InDesign script the InDesign dialog api has more options. This would force the user to enter a number:

 

tell application id "com.adobe.indesign"
	activate
	
	set myDialog to make dialog with properties {name:"Dialog Title"}
	tell myDialog
		
		--the enclosing column
		tell (make dialog column)
			--Integer Edit
			tell (make dialog row)
				make static text with properties {static label:"Integer Edit"}
				set myIntEdit to make integer editbox with properties ¬
					{edit value:1, maximum value:100, minimum value:0, small nudge:1, large nudge:10, min width:50}
			end tell
			
		end tell
		
		show
		set n to edit contents of myIntEdit
		--Remove the dialog box 
		destroy myDialog
	end tell
	
	display dialog "The integer entered was: " & n
end tell

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Enthusiast ,
Aug 26, 2020

Copy link to clipboard

Copied

Hi Rob,

 

Thank you for your quick response...

Approach 1:

Your provided code working like a charm..

 

Meanwhile time, I got the input from Google.. Logic using here is Shell Script.

Approach 2:
Looking this automation in Finder only. Thank you for providing approach 2.

set myInteger to display dialog "Number of Jobs in XXX for YYY" default answer ""
set input to text returned of myInteger


set validation to do shell script "egrep -x '[0-9]{1,6}' <<<" & quoted form of the input & "|| echo Invalid input"

if validation is equal to "Invalid input" then
	display dialog "Kindly Provide the Valid Input as Number" with icon caution
	return
else
	set myValue to validation
end if

display dialog myValue

 

Again thanks a lot for your timely help!

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Aug 26, 2020

Copy link to clipboard

Copied

The InDesign dialog building doesn’t kick the user out of the script on a class error, so I’ll use the ID dialog code even when I’m moving between ID and the finder. 

 

This is my reference code for dialog building:

 

Screen Shot 28.png

 

 

--Complex User Interface Example
tell application id "com.adobe.indesign"
	activate
	
	set myDialog to make dialog with properties {name:"Dialog Title"}
	tell myDialog
		
		--the enclosing column
		tell (make dialog column)
			
			------------------------Panel 1----------------------------
			
			--a row with title text
			tell (make dialog row)
				make static text with properties {static label:"Panel Title", static alignment:left align, min width:150}
			end tell
			
			--a panel below the title
			tell (make border panel)
				
				-- a column inside the panel
				tell (make dialog column)
					
					--Angel Combo
					tell (make dialog row)
						
						make static text with properties {static label:"Angle Combo"}
						set myAngleCombo to make angle combobox with properties ¬
							{string list:{"0", "90", "180"}, edit value:0, maximum value:360, minimum value:0, small nudge:1, large nudge:10, min width:50}
					end tell
					
					--Angel Edit
					tell (make dialog row)
						make static text with properties {static label:"Angle Edit"}
						set myAngleEdit to make angle editbox with properties ¬
							{edit value:0, maximum value:360, minimum value:0, small nudge:1, large nudge:10, min width:50}
					end tell
					
					--Checkbox control
					tell (make dialog row)
						set myMustardCB to make checkbox control with properties {static label:"Check Box ", checked state:true, min width:150}
					end tell
					
					--Dropdown
					tell (make dialog row)
						set PresetList to {"Mustard", "Relish", "Ketchup"}
						make static text with properties {static label:"Dropdown"}
						set myDropdown to make dropdown with properties ¬
							{string list:PresetList, selected index:0, min width:80}
					end tell
					
				end tell
			end tell
			
			------------------------Panel 2 (Enabling)----------------------------
			
			--title text
			tell (make dialog row)
				make static text with properties {static label:"Enabling Panel", static alignment:left align, min width:150}
			end tell
			
			--a panel below the title
			tell (make enabling group with properties {checked state:true, min width:150})
				
				-- a column inside the panel
				tell (make dialog column)
					
					--Integer Combo
					tell (make dialog row)
						make static text with properties {static label:"Integer Combo"}
						set myIntegerCombo to make integer combobox with properties ¬
							{string list:{"1", "2", "3"}, edit value:0, maximum value:100, minimum value:0, small nudge:1, large nudge:10, min width:50}
					end tell
					
					--Integer Edit
					tell (make dialog row)
						make static text with properties {static label:"Integer Edit"}
						set myIntegerEdit to make integer editbox with properties ¬
							{edit value:1, maximum value:100, minimum value:0, small nudge:1, large nudge:10, min width:50}
					end tell
					
					--Measurement Combo
					tell (make dialog row)
						make static text with properties {static label:"Measurement Combo"}
						set myMeasureCombo to make measurement combobox with properties ¬
							{string list:{"10", "20", "30"}, edit units:pixels, edit value:0, maximum value:100, minimum value:0, small nudge:1, large nudge:10, min width:90}
					end tell
					
					--Measurement Edit. Note max and min units are always points
					tell (make dialog row)
						make static text with properties {static label:"Measurement Edit"}
						set myMeasureEdit to make measurement editbox with properties ¬
							{edit units:points, edit value:0, maximum value:1000, minimum value:0, small nudge:1, large nudge:10, min width:90}
					end tell
					
					--Percent Combo
					tell (make dialog row)
						make static text with properties {static label:"Percent Combo"}
						set myPercentCombo to make percent combobox with properties ¬
							{string list:{"10", "15", "20"}, edit value:10, maximum value:100, minimum value:0, small nudge:1, large nudge:10, min width:50}
					end tell
					
					--Percent Edit
					tell (make dialog row)
						make static text with properties {static label:"Percent Edit"}
						set myPercentEdit to make percent editbox with properties ¬
							{edit value:15, maximum value:100, minimum value:0, small nudge:1, large nudge:10, min width:50}
					end tell
					
					--radiobutton group
					tell (make dialog row)
						tell (make radiobutton group)
							set myPageRadio to make radiobutton control with properties {static label:"Pages ", checked state:true}
							set mySpreadRadio to make radiobutton control with properties {static label:"Spreads "}
						end tell
					end tell
					
					--Real Combo
					tell (make dialog row)
						make static text with properties {static label:"Real Combo"}
						set myRealCombo to make real combobox with properties ¬
							{string list:{".1", ".2", ".3"}, edit value:0, maximum value:1, minimum value:0, small nudge:0.01, large nudge:0.1, min width:50}
					end tell
					
					--Real Edit
					tell (make dialog row)
						make static text with properties {static label:"Real Edit"}
						set myRealEdit to make real editbox with properties ¬
							{edit value:0, maximum value:10, minimum value:0, small nudge:0.01, large nudge:0.1, min width:50}
					end tell
					
					--Text Edit
					tell (make dialog row)
						make static text with properties {static label:"Message:"}
						set myTextEditField to make text editbox with properties ¬
							{edit contents:"Hello World!", min width:150}
					end tell
					
				end tell
			end tell
		end tell
		
		
		--get the results
		set myResult to show myDialog
		if myResult = true then
			--+1 gets the correct name because list starts at 0
			set myPreset to item ((selected index of myDropdown) + 1) of PresetList as item
			set v to edit value of myMeasureEdit
		else
			return
		end if
		destroy myDialog
		
	end tell
	display dialog v as string
	
end tell

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Enthusiast ,
Aug 26, 2020

Copy link to clipboard

Copied

Hi Rob,

 

Thank you for your valuable suggestions.

 

Will look into this and get back in case of any clarifications.

 

Thanks

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...