If statement with two possible text delimiters

Dear Friends,
Folders were coming to me with names such as Bucky_Edgett or bucky_doofus_edgett, etc. I wrote a simple script to parse them into alphabetically sortable ones: Edgett, Bucky Doofus. (Capltalization and number of text items are not problems: my script accounts for those, thanks.) Now, I am getting folders named Bucky Edgett or bucky edgett, etc. In other words, the “text delimiter” has changed.

So, I thought I’d get tricky and write:

if theWorkingName contains {““} then
set AppleScript’s text item delimiters to {”
”}
else
set AppleScript’s text item delimiters to {" "}
end if

I can’t get this to work. AppleScript won’t set the delimiter to " ".

If the name is Bucky_Edgett, it works: seeing the name as two text items (or one or three; whichever is relevant depending on the name). If the name is Bucky Edgett, it is treated as one text item. If I use {" "} as the quoted character and “_” as the “else” character, it won’t parse Bucky Edgett correctly. But it WILL parse Bucky_Edgett.

So, it seems to me that the “contains” statement works both ways, but that I can’t set the delimiter to " ". Does this make sense? Have I analysed the problem correctly? And if I have, why can I not use a spacebar character as a text delimiter? Or am I just hopelessly muddled?

Any help would be greatly appreciated. Thanks for your support.

There’s no reason you shouldn’t be able to set the tid’s to a space. The following works for me…

set theWorkingName to "bucky edgett" --> or, "bucky_edgett"

if theWorkingName contains {"_"} then
	set AppleScript's text item delimiters to {"_"}
else
	set AppleScript's text item delimiters to {" "}
end if

set theOutput to text items of theWorkingName
set AppleScript's text item delimiters to {""}

return theOutput --> returns {"bucky", "edgett"} either way

I can’t see any problems with you’re code, so the problem must lie somewhere else. Make sure that the value you’re passing to it is a valid string and that it actually contains the text you’re intending to pass to it.

j

I love it when people answer their own questions!!!

First off, you can set the delimiter to " ". But in your case you don’t need to. Lets do a walk through!

You want 1)Bucky_Edgett or 2)bucky_doofus_edgett to be ‘Edgett, Bucky’ or ‘edgett, bucky doofus’ so you set your delimiter to “_”
The thing to realize is you have 2 cases to deal with: case 1 or case 2
Case 1 has 2 items so the code is


set FullName to {(text item 2 of the theworkingname) & ", " & (text item 1 of theworkingname)}

case 2 has 3 items so the code is


set FullName to {(text item 3 of the theworkingname) & ", " & (text item 1 of theworkingname) & " " & (text item 2 of theworkingname)}

error check for case 1, if its case 2 it will set case 2 as the format.
Don’t forget to set a variable to your delimiters default at the start and set back to default at the end! This might have been giving you other problems you couldn’t account for.


set old_delim to AppleScript's text item delimiters --So you can return to default delimiter
set theworkingname to "Bucky_Dodgers_Everet"
if theworkingname contains {"_"} then
	set AppleScript's text item delimiters to {"_"}
	try
		set FullName to {(text item 3 of the theworkingname) & ", " & (text item 1 of theworkingname) & " " & (text item 2 of theworkingname)}
		-- Code to reorganize for first, last, middle
		
	on error
		
		set FullName to {(text item 2 of the theworkingname) & ", " & (text item 1 of theworkingname)}
		-- Code to reorganize for first, last
	end try
end if
set AppleScript's text item delimiters to old_delim --return to default delimiter
FullName as string --returns your 'last name first, first name & middle name format

SC

Your ‘theWorkingName’ variable holds a string, right? In that case, the right-hand operand should also be a string if you ever want this expression to return ‘true’ because a string object will never contain a list object. i.e. Use:

theWorkingName contains "_"

HTH

I think he’s referring to this


set theworkingname to "Bucky_Dodgers_Everet"
if theworkingname contains {"_"} then
   set AppleScript's text item delimiters to {"_"} 

that perhaps it should be more precise


set theworkingname to "Bucky_Dodgers_Everet" as string -- Coerce to string
if theworkingname contains {"_"} then
   set AppleScript's text item delimiters to {"_"} 

But I get the same result. So make sure your source filename is coming to the handler as a string.
Comments?
SC

That won’t make any difference as you’re just coercing a string into a string. The problem is with the next line:

if theWorkingName contains {"_"} then

which should read:

if theWorkingName contains "_" then

Man, I missed that! Only because it was returning the same result. So then,


set old_delim to AppleScript's text item delimiters --So you can return to default delimiter
set theworkingname to "Bucky_Dodgers_Everet"
if theworkingname contains "_" then
   set AppleScript's text item delimiters to "_"
   try
      set FullName to {(text item 3 of the theworkingname) & ", " & (text item 1 of theworkingname) & " " & (text item 2 of theworkingname)}
      -- Code to reorganize for first, last, middle
      
   on error
      
      set FullName to {(text item 2 of the theworkingname) & ", " & (text item 1 of theworkingname)}
      -- Code to reorganize for first, last
   end try
end if
set AppleScript's text item delimiters to old_delim --return to default delimiter
FullName as string --returns your 'last name first, first name & middle name format 

SC