AppleScript delimiters, error -1728

My script uses display dialog to get a text string from the user. The string will contain several values separated by commas (“,”). Then, I attempt to simply parse the answer using AppleScript’s text item delimiters. Here is a simplified example:

display dialog “Enter Fname,Lname,City,State”
copy the result as list to {the Action, the Answer}
set AppleScript’s text item delimiters to {“,”}
set newFields to the text items of Answer --AppleScript Execution Error

Can’t get every text item of “Tom,Jones,Syracuse,NY”
-1728

Sorry if a dumb syntax error but can’t get past this.

If you run only your first two lines, you should see the problem more clearly. The result is not what you’re after.

Dialogs can return either the button or the text. Also, you can assign the results directly to a variable, which may be easier to work with.

Try something like this:

set dialInput to display dialog "Enter Fname,Lname,City,State" default answer "Tom,Jones,Syracuse,NY"
set tr to text returned of dialInput
--> "Tom,Jones,Syracuse,NY"

set text item delimiters to ","
set newFields to text items of tr
--> {"Tom", "Jones", "Syracuse", "NY"}

Change the default answer to a pair of empty quotes to give your dialogue a text input with nothing in it.

That worked. Thank you!

1 Like

An alternative without delimiters can be written as:

use scripting additions

set {Fname, Lname, City, State} to words of text returned of (display dialog "Enter Fname,Lname,City,State" default answer "Tom,Jones,Syracuse,NY")

which assigns each item from the display into its own resepective variable.

This could produce unreliable results you live in places with multi-word names, like Great Neck or White Plains, or if either of your names is complex.

Agreed. A little too hasty in the response. For those that favor AppleScript delimiters, then using your dialInput string with Great Neck instead of Syracuse:

set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
set newFields to text items of tr
--> {"Tom", "Jones", "Great Neck", "NY"}

set AppleScript's text item delimiters to TID

It can also be resolved with slightly more code using ASOC and Foundation’s NSString methods.

1 Like