Split function works - only when not embedded in a larger script

Hi Folks-

I am trying to split up a string (1.456789.100) and pull the middle 6 digits out, by changing the text item delimter to “.” This works in a test scenario:



to split(fullitemcode, vdelimiter)
	set AppleScript's text item delimiters to vdelimiter
	set fullitemcode to fullitemcode's text items --turns foundtext into a list
	set AppleScript's text item delimiters to {""} --returns delimiter to apple's default
	return fullitemcode
end split

set foundText to {"1.343455.100", 3333}

set fullitemcode to first item of foundText
display dialog fullitemcode
set itemcode to second item of split(fullitemcode, ".")
display dialog itemcode


but when I place it into the growing actual script, the split fails, and returns the whole string again. It doesn’t seem to matter if I put the split function at the beginning or just before the actual function.



to split(fullitemcode, vdelimiter)
	set AppleScript's text item delimiters to vdelimiter
	set fullitemcode to fullitemcode's text items --turns foundtext into a list
	set AppleScript's text item delimiters to {""} --returns delimiter to apple's default
	return fullitemcode
end split

tell application "Adobe InDesign CS2"
	set foundText to {}
	tell document 1
		set searchResults to search text frames for "1.^9^9^9^9^9^9.^9^9^9"
		repeat with x from 1 to count of searchResults
			repeat with y from 1 to count of item x of searchResults
				try
					set selection to item y of item x of searchResults
				end try
				try
					set end of foundText to text of selection
				end try
			end repeat
		end repeat
	end tell
end tell
if (count of foundText) is 0 then
	display dialog "A code was not found in the current document (1.xxxxxx.xxx)" buttons {"Cancel"} with icon stop
end if

set fullitemcode to first item of foundText --pulls first instance of itemcode from returned list
display dialog fullitemcode
fullitemcode as string
set itemcode to first item of split(fullitemcode, ".")
display dialog itemcode


thanks for any help in advance,

Ralph

Hi Ralph,

the problem is in the last part of your script.


...
set fullitemcode to first item of foundText --pulls first instance of itemcode from returned list
display dialog fullitemcode
fullitemcode as string
set itemcode to first item of split(fullitemcode, ".")
display dialog itemcode

fullitemcode is a list, the line fullitemcode as string is useless, because the variable won’t be changed.

this works:

set fullitemcode to first item of foundText --pulls first instance of itemcode from returned list
display dialog fullitemcode
set itemcode to first item of split(fullitemcode as string, ".") -- you mean really FIRST item?
display dialog itemcode

Aha.

So it seems one can coerce a variable (in brackets) for a one-time use when it can’t be coerced “actually”?

You are correct- I do want the 2nd item. I had it set to 1st item because it was at least generating a response, instead of erroring out. When I eventually got the result “1”, I was going to change it over.

thanks Stephan!

-Ralph

some notes:

myvariable as string

is a temporary coercion, myvariable won’t be changed

set myvariable to myvariable as string

coerces and saves the result into myvariable

passing the parameter thru the parentheses sets the local handler variable fullitemcode to the coerced value

Another way to skin the same cat.

set stringValue to "1.343455.100"
set offsetValue to offset of "." in stringValue
set itemcode to text (offsetValue + 1) through (offsetValue + 6) of stringValue

or with one less line

set stringValue to "1.343455.100"
tell (offset of "." in stringValue) to set itemcode to text (it + 1) through (it + 6) of stringValue

Side note: You shouldn’t assume anything about AppleScript’s text item delimiters; The best way to handle them is:

  • Save the current delimited
  • Change to what you need
  • Do what you need to do
  • Change to value saved earlier

I often use something like this:

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
-- do whatever
set AppleScript's text item delimiters to ASTID

See also: explode()