Objective-C string replacement routine
Objective-C string replacement routine
- Subject: Objective-C string replacement routine
- From: Jim Brandt via AppleScript-Users <email@hidden>
- Date: Sat, 7 May 2022 14:13:18 -0500
I need to find an Objective-C method of doing string replacement. I’m in the
process
of replacing all my Satimage calls with something else, so I can move off High
Sierra
to Monterey and I’ve run in to a snag.
I’ve been able in most cases to use Shane Stanley’s RegexAndStuffLib for a lot
of my
text manipulation. But I’ve found a case where I can’t.
For years, I’ve used Pashua as a dialog routine. I have a library which lets me
build the
text-based dialogs for Pashua. I will be migrating to Dialog Toolkit Plus, but
for now,
I need to keep a lot of these routines.
So, on to the problem:
I make a variable that is the text I’m going to pass to Pashua. This text
starts out with
a “template” that I then use the Satimage change statement on to replace items
in the
text with substitutions before passing it to Pashua.
The items of the template that need replaced are delimited on either side by a
$.
Here is a sample of one of my templates:
$var$.type = textbox
$var$.label = $label$
$var$.x = $x$
$var$.y = $y$
$var$.width = $w$
$var$.height = $h$
$var$.default = $def$
I then use a (Satimage) change statement to replace the $var$ by some other
text, the $label$
by yet some other text, until I end up with something like:
tb.type = textbox
tb.label = Text Label:
tb.x = 0
tb.y = 25
tb.width = 300
tb.height = 5
tb.default = default text
Because the $ is the regular expression character signifying the end of a
search string,
regex is not usable here. The Satimage change command could be use with or
without regex,
so my searches were without in this case.
So, I’m trying to come up with an Objective-C replacement.
Here’s what I tried:
set V to {"$var$", "$label$", "$x$", "$y$", "$w$", "$h$", "$def$"}
set TextBox to {"tb", "Label Text:", 0, 25, 300, 10, "Default Value"}
repeat with i from 1 to count of V
set EachV to item i of V
set EachSub to item i of TextBox
set SingleBoxText to (my substituteText:SingleBoxText searchFor:EachV
replaceWith:EachSub)
end repeat
on substituteText:someText searchFor:someString replaceWith:replaceText
set theString to current application's NSString's
stringWithString:someText
set theSearch to current application's NSString's
stringWithString:someString
set theReplacement to current application's NSString's
stringWithString:replaceText
set stringLength to theString's |length|()
set newString to theString's
stringByReplacingOccurrencesOfString:theSearch withString:theReplacement
return newString as text
end substituteText:searchFor:replaceWith:
but this didn’t work.
I’m still learning Objective-C, so I’m pretty sure I didn’t get the routine
right, but don’t know where to look
to figure this out. The documentation in XCode isn’t too good at providing
examples, an the few Objective-C
books I have don’t go too deep into string manipulation.
Jim Brandt
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden