on textReplace(sourceText, searchText, replaceText)
set cocoaString to current application's NSMutableString's stringWithString:sourceText
cocoaString's replaceOccurrencesOfString:(searchText as string) withString:(replaceText as string) options:(current application's NSCaseInsensitiveSearch) range:{0, (cocoaString's |length|())}
return cocoaString as unicode text
end textReplace
I get this ‘error’ from Mojave 10.14.3
error “NSMutableString doesn’t understand the “stringWithString_” message.” number -1708 from NSMutableString
Shane Stanley book Everyday AppleScriptObjC on page 3 it say…
If you just want to save it as a script library for use in OS X 10.10 or later only, you can save it as either a script file (.scpt) or a script bundle (.scptd).
So I wonder why I get this error on (scpt) extension on above handler saved as Script Libraries
error “NSMutableString doesn’t understand the “stringWithString_” message.” number -1708 from NSMutableString
Take a look at this 2 files, the one with is the handle save it to your Script Libraries folder.
I name my copy as: TextLib_ASOC.scpt (this is no applescript bundle)
The other one is the script that call the function from the handler.
So I have use framework “Foundation” is both of this script.
use framework "Foundation"
use scripting additions
(*
use myLib : script "TextLib_ASOC"
myLib's textReplace("Hxllo World!", "x", "e")
*)
(*
tell script "TextLib_ASOC"
textReplace("Hxllo World!", "x", "e")
end tell
*)
(*
script "TextLib_ASOC"'s textReplace("Hxllo World!", "x", "e")
*)
-- textReplace("Hxllo World", "x", "e")
on textReplace(sourceText, searchText, replaceText)
set cocoaString to current application's NSMutableString's stringWithString:sourceText
set cocoaString to cocoaString's replaceOccurrencesOfString:(searchText as string) withString:(replaceText as string) options:(current application's NSCaseInsensitiveSearch) range:{0, (cocoaString's |length|())}
return cocoaString as Unicode text
end textReplace
#==============
use framework "Foundation"
use scripting additions
use myLib : script "TextLib_ASOC"
set resultText to myLib's textReplace("Hxllo", "x", "e") as string
Your second library script doesn’t work. It will always return “1” if the operation succeeds. You have to delete set cocoaString to in the replace line.
If you are using a Cocoa framework import also AppleScript. However Scripting Additions are not used in your library script
use AppleScript version "2.5"
use framework "Foundation"
on textReplace(sourceText, searchText, replaceText)
set cocoaString to current application's NSMutableString's stringWithString:sourceText
cocoaString's replaceOccurrencesOfString:searchText withString:replaceText options:(current application's NSCaseInsensitiveSearch) range:{0, (cocoaString's |length|())}
return cocoaString as text
end textReplace
And –as mentioned – remove the redundant coercions
In the calling script importing the library script is sufficient as there is no Scripting Addition nor Foundation terminology
use myLib : script "TextLib_ASOC"
set resultText to myLib's textReplace("Hxllo", "x", "e")
The strange thing was Applescript bundle didn’t care or complain about any error… (1st script)
So I thought for a second my code was correct or it confused me little.
The 2nd script ‘handle’ I did some change but thanks to point out for me the right direction.