Split and point to items in the array

Hi all

Working on a example to calling a applescript file from VBA in Mac Office 2016.
I can only give one parameter string in this VBA call to use in the script file.

I need a few so I was thinking to make one string like (not matter what separator we use here)
“str1,str2,str3,str4”

Then add script in the applescript file that split the string in four items and be able to use something like this : Str(1) or str(3) to point to the strings in applescript.

In VBA it is easy for me but not that smart with applescript so I hope you can help me.

Thanks

Something like this?

It works only for unquoted fields, if you want to support CSV rows your need an more advanced handler.

getFieldFromRow("str1,str2,str3,str4", 3, ",") --str3

on getFieldFromRow(theRow, fieldIndex, fieldSeparator)
	tell AppleScript
		set oldTID to text item delimiters
		set text item delimiters to fieldSeparator
		set fieldValue to text item fieldIndex of theRow
		set text item delimiters to oldTID
	end tell
	return fieldValue
end getFieldFromRow

Great, that is what I needed

Thank you very much DJ and have a nice day

I have problems to add it to another script

I get the error "Expected “end” but found “on”

I add it between another on block maybe this is the problem, this is my first line

on myapplescripthandler(paramString)

paramString is the big string so I need that go first

Then DJ 's script

What are the rules for this ?

You cannot define an handler inside a handler you can only call handlers in a nested way bot not defining them.

Mmmm

What can I do to get this working

The new VBA call need this, where paramString is the string I need to split

on myapplescripthandler(paramString)

end myapplescripthandler

I need your script in between and then another script that use the text strings (fieldValue1 -4)
Where I need to replace “str1,str2,str3,str4” with paramString

SplitString(“str1,str2,str3,str4”, “,”)

on SplitString(TheBigString, fieldSeparator)
tell AppleScript
set oldTID to text item delimiters
set text item delimiters to fieldSeparator
set fieldValue1 to text item (1) of TheBigString
set fieldValue2 to text item (2) of TheBigString
set fieldValue3 to text item (3) of TheBigString
set fieldValue4 to text item (4) of TheBigString
set text item delimiters to oldTID
end tell
–return fieldValue1 & fieldValue2 & fieldValue3 & fieldValue4
end SplitString

Thanks

Something like this?

myApplescriptHandler("str1,str2,str3,str4")

on myApplescriptHandler(paramString)
	-- adding the four empty value to the end creates default values if the returned list doesn't contain 4 items
	set {fieldValue1, fieldValue2, fieldValue3, fieldValue4} to SplitString(paramString, ",") & {"", "", "", ""}
	-- insert your code here....
	return {fieldValue1, fieldValue2, fieldValue3, fieldValue4}
end myApplescriptHandler

on SplitString(TheBigString, fieldSeparator)
	tell AppleScript
		set oldTID to text item delimiters
		set text item delimiters to fieldSeparator
		set theItems to text items of TheBigString
		set text item delimiters to oldTID
	end tell
	return theItems
end SplitString

Test more tomorrow and get back to you. Got some strange results in Excel. Mac Office 2016 is not what we want on this moment.

Hi DJ

Got things working, thank you very much for your help.
Will be able to update all my mail code examples that you can run from Mac Office 16 with MacMail or Outlook.

My examples for 2011 use the VBA MacScript function and this is not working correct anymore in Office 2016 so I must do it another way.

Drop you a private mail with the examples if you like to see it when I am ready

Have a nice evening