Re: text manipulation in a script?
Re: text manipulation in a script?
- Subject: Re: text manipulation in a script?
- From: has <email@hidden>
- Date: Sun, 15 Sep 2002 14:23:39 +0100
John Kinsella wrote:
>
Can I manipulate a user defined variable (much like I would manipulate text
>
in a document. What I mean is, if a user gives me a variable like "left,
>
right, up, down" can I manipulate that an set it as a new variable for use
>
later in the script.
I think you've muddled your terminology here: a variable is merely a
container used to hold *values*. Your "left, right, up, down" is a value.
[It's important to be clear on the difference, as things will get really
confusing otherwise.] You can assign a value to a variable like so:
set variableName to "this string value"
Find-and-replace seems to be one of the first algorithms that new
AppleScripters meet, and the most common approach is to use AppleScript's
text item delimiters to tokenise the string (splitting it at each point
where the delimiter string occurs), then coercing the resultant list back
into a string using a different text item delimiter. [1]
I'm going to skip the bit where I show the actual algorithm, and just point
you towards a pre-existing library that contains a ready-to-use version:
<
http://www.barple.pwp.blueyonder.co.uk/libraries/index.html#stringLib>.
You can use it by loading it into a property, like so:
property stringLib : load script (alias "path to stringLib file here")
and call the appropriate F&R function (there's 3 available) from elsewhere
in your script by doing:
set oldString to "left, right, up, down" -- TEST
set newString to stringLib's simpleFindAndReplace(oldString, ", ", " - ")
return newString -- TEST
--> "left - right - up - down"
HTH
has
[1] Notes: One thing I've not seen so far is a set of F&R routines (and
other text manipulation functions) designed with Unicode in mind. Some
routines are completely neutral in the matter, but others won't work with
Unicode at all, while others will but will coerce it to string in the
process which could be undesireable. I mention this more as something for
intermediate/advanced coders to consider as a challenge, rather than as
something for the OP to worry about. But we will have to start re-thinking
how we perform text manipulations in AS, particularly if the AS team isn't
planning on bringing more comprehensive text manipulation facilities (e.g.
regexp) to AS soon.
--
http://www.barple.pwp.blueyonder.co.uk -- The Little Page of AppleScripts
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.