Hi,
Unfortunately I have spent the past sixteen years writing most my code in xTalk in the Livecode IDE. A feature of xTalk is that almost every variable is a string and the engine handles type conversions. This has coloured my thinking and I am struggling to get to grips with how Applescript handles strings.
I wanted to replicate a “simple” xTalk command in AppleScript :
put " Now is the time for all good me to come to the aid of the party " into mystring
repeat until the first char of mystring is not space
delete the first char of mystring
end repeat
repeat until the last char of mystring is not space
delete the last char of mystring
end repeat
Here is my Applescript version"
set mystring to " Now is the time for all good me to come to the aid of the party "
set mystring to TrimLeading(mystring, " ")
set mystring to TrimTrailing(mystring, " ")
display dialog "-->" & mystring & "<--"
on TrimLeading(pString, pTgt)
set AppleScript's text item delimiters to ""
set tFirstChar to item 1 of pString as text
repeat while tFirstChar = " "
set pString to text 2 thru end of pString
set tFirstChar to item 1 of pString as text
end repeat
return pString
end TrimLeading
on TrimTrailing(pString, pTgt)
set AppleScript's text item delimiters to ""
set tLastChar to item -1 of pString as text
repeat while tLastChar = " "
set pString to text 1 thru -2 of pString
set tLastChar to item -1 of pString as text
end repeat
return pString
end TrimTrailing
While the Applescript version allows any character to be specified in pTgt meaning it is more powerful than my xtalk example I have a feeling that I am missing an obvious way of trimming characters in Applescript.
Also what is the difference between specifying a coercion “as text” compared with “as string”? Is text just text whereas a string is a list{} of characters?