I wrote this script program which is to return the value of ClientName. Unfortunately the program does not return any values for the variable ClientName.
In this situation the program should be returning the ClientName Eugen and I nothing is return. Does anyone has any idea why ?
Regards!
Daniel
The input file has the following paragraphs (theInvoice, ClientName):
c5256 Mary
c5257 Paul
c5258 Eugen
c5259 Tracy
set theInvoice to "c5258"
set ClientName to ""
my ClientInformation(theInvoice, ClientName)
display dialog "--" & theInvoice & "--" & ClientName
on ClientInformation(theInvoice, ClientName)
set theOrderFile to choose file
set theOrderLine to read theOrderFile
set theOrderList to paragraphs of theOrderLine
set OrderRecordCount to count of theOrderList
display dialog (OrderRecordCount as text)
repeat with x from 1 to OrderRecordCount
set nextpar to item x of theOrderList
set _theInvoice to (text 1 thru 5 of nextpar)
set the ClientName to (text 7 thru 13 of nextpar)
if _theInvoice is equal to theInvoice then
display dialog _theInvoice & " ClientName: " & ClientName
return ClientName
end if
end repeat
end ClientInformation
It does (once Stefan’s correction has been implemented), but the variable called ClientName outside the handler is a different variable from the one inside it. (The one inside is said to be “local” to the handler.) You have to set the outside variable to the result of the handler, like this:
set ClientName to ""
set ClientName to ClientInformation(theInvoice, ClientName)
In fact, your handler doesn’t make any use of the “” value passed to it, so you might as well omit that parameter:
set ClientName to ClientInformation(theInvoice)
on ClientInformation(theInvoice)
-- etc.
Absolutely not! The former is always to be preferred (except under very unusual circumstances ;)).
The final working is a combination of all the comments received. After looking at it more closely, Nigel you got me to realize I was not using the second parameter. In fact this routine is to return a value based on what is coming in.
At another time, I will test one value coming in and three values being returned to see what appends.
BEST REGARDS to everybody!
set theInvoice to "c5258"
set ClientName to my ClientInformation(theInvoice)
display dialog "--" & theInvoice & "--" & ClientName
on ClientInformation(theInvoice)
set theOrderFile to choose file
set theOrderLine to read theOrderFile
set theOrderList to paragraphs of theOrderLine
set OrderRecordCount to count of theOrderList
repeat with x from 1 to OrderRecordCount
set nextpar to item x of theOrderList
set the _theInvoice to text 1 thru 5 of nextpar
set the ClientName to text 7 thru 13 of nextpar
if _theInvoice is equal to theInvoice then
return ClientName
exit repeat
end if
end repeat
end ClientInformation
You appear not to have noted Stefan’s point that you can’t get characters 7 thru 13 of a 10-character paragraph. Also, using a rigid index system like that only works if all your clients have 7-character names.
And I pointed out, in response to oldmanegan’s suggestion, that using the ‘(characters 1 thru 5 of nextpar) as text’ construction was not a good idea. This is because:
It creates a list containing the individual characters and then creates the required text from that. It’s therefore very much less efficient than extracting the required text directly from the source text with ‘text 1 thru 5 of nextpar’. (It doesn’t make much difference when only five characters are involved, but it’s good not to develop bad habits.)
When the list of characters is coerced to text, the current value of AppleScript’s text item delimiters is interspersed between the characters in the final result. The default value for the delimiters is {“”}, in which case nothing appears between the characters and all is well. But if the delimiters have been changed for some reason, you’ll get very different results
set AppleScript's text item delimiters to "Hello"
set nextpar to "c5256 Mary"
set _theInvoice to (characters 1 thru 5 of nextpar) as text
--> "cHello5Hello2Hello5Hello6"
set theInvoice to text 1 thru 5 of nextpar
-->" c5256"
set the ClientName to (characters 7 thru 13 of nextpar) as text
--> Misleading error message. In fact, characters 11 thru 13 don't exist.
I will change the characters to text (i.e set _theInvoice to (characters 1 thru 5 of nextpar).
I took into consideration Stefan points. In the real situation the paragraphs I am using are 13 characters-paragraphs long. This is why I did not change it to 10.