NSString doesn’t understand the “stringWithString_” message

I'm creating an AppleScript Script Library, and want to call some Cocoa methods on NSString. I created an .SCPTD file with this method:

on xtrim(sourceText)
    set the sourceString to current application's NSString's stringWithString:sourceText

    set whitespace to {space, tab, return, linefeed}
    set the adjustedString to sourceString's stringByTrimmingCharactersInSet(whitespace)

    return (adjustedString as string)
end xtrim

However, I get the error NSString doesn’t understand the “stringWithString_” message.. Most answers I find on the internet suggest that the AppleScript/Objective-C Library checkbox is not checked.

However, this checkbox has been removed with the Yosemite release.

What is wrong with the script; is there a fix?


Solution 1:

Answer from an Apple thread:

As of Yosemite, ASObjC is available everywhere, not just libraries and applications. Importing the desired terminology via the use statement is now directly supported in the Script Editor - for your example, the NSString class stuff is available by adding a use framework "Foundation" statement.