Skip to content

Calling Cocoa String Methods in Applescript Studio

HAS, the devleoper of appscript left a useful comment in response to my earlier posting on the offset method. He offered an example of how one might easily and usefully call Cocoa methods to manipulate strings in AppleScript Studio that aren’t available directly in the AppleScript language.

If, for example, you wanted to use the existing Cocoa method to make a string all uppercase you could write code for a button that contains something like this:

on clicked theObject
	if the name of theObject is "makeUppercase" then
		set contents of text field "inputField" of window "myWindow" to call method "uppercaseString" of (contents of text field "inputField" of window "myWindow")
	end if
end clicked

If, instead, you wanted to have a string only capitalize the first letter of each word, then you would use the Cocoa function “capitalizedString” instead.

set contents of text field "inputField" of window "myWindow" to call method "capitalizedString" of (contents of text field "inputField" of window "myWindow")

You can download an example project with this example here: Call Method Example.

A full list of the Cocoa methods for the NSString class in Cocoa can be found in Apple document NSString Class Reference. Some of these methods need to be handled differently as they may require parameters etc. There is more detail on the call method command in the AppleScript Studio documentation to be found where you can learn more about how to include parameters.

Strings are only a simple beginning, this command opens up a lot of possibilities for using Cocoa methods that may not have equivalents in AppleScript, if done with care.