macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Variables Inside Cocoa Objects
Pages: 1, 2

Now we have created our instance variables, but we still need to add a text item to the mix. If you look at the instance variables we added, you'll notice that one is declared as an IBOutlet with the class of NSTextView. The other is an NSTextStorage object. The NSTextView object we are going to add in Interface Builder, while the NSTextStorage object is going to be created automatically for us, but we will need to assign it to this instance variable in order to use it.

  • First, on the left side, open the Resources folder and double-click on the MyDocument.nib file. This will launch Interface Builder and load your nib file. This file contains the Cocoa objects that make up your MyDocument class.
  • If you look in the MyDocument.nib window, you will see a blue cube called File's Owner. Click on this to highlight it and then switch to the Classes tab by clicking above on the Classes tab.
  • You should see the MyDocument class as an item. Control-click on the MyDocument name and you will get a contextual menu. Choose Add Outlet to MyDocument.
  • Change the name of myOutlet to textView and then change the type to NSTextView.
  • In the Cocoa panel, choose the item that looks like a text window and drag a NSTextView item to your document's window.
  • Resize it to your liking.
  • Switch back to the MyDocument.nib window and click on the Instances tab.
  • Now, control-click on the blue cube and drag the blue line up to the NSTextItem in the window above.
  • In the connections window, choose the textView item and choose connect.
  • Save you document and quit out of Interface Builder.

Back in Project Builder, we first need to hookup our textStorage instance variable and then we need to add in our scriptSuite and scriptTerminology files.

To hook up textStorage, click on the MyDocument.m file and look for a method called windowControllerDidLoadNib. This is called once the window has been loaded by the application. By this point, we know our textView item, brought in as an IBOutlet from interface builder, will be available and we can ask it for its NSTextStorage object. We do this by adding the following code after the Add Code comment:

 textStorage = [textView textStorage];
 

textStorage on the right side is a class method for NSTextView objects that returns that item's NSTextStorage item.

For the scriptSuite file it is much the same as last time, but we are going to add a section in the MyDocument section on the ToOneRelationships key. This will simply be the name of our instance variable and the type of object it is. The following snippet is what is added, but the full version follows:

Snippet for textStorage


				<key>textStorage</key>
				<dict>
					<key>AppleEventCode</key>
					<string>ctxt</string>
					<key>ReadOnly</key>
					<string>NO</string>
					<key>Type</key>
					<string>NSTextStorage</string>
				</dict>

Full ScripableDocApp.scriptSuite


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
	<key>AppleEventCode</key>
	<string>ScDA</string>
	<key>Classes</key>
	<dict>
		<key>MyDocument</key>
		<dict>
			<key>AppleEventCode</key>
			<string>docu</string>
			<key>Superclass</key>
			<string>NSCoreSuite.NSDocument</string>
			<key>ToOneRelationships</key>
			<dict>
				<key>textStorage</key>
				<dict>
					<key>AppleEventCode</key>
					<string>ctxt</string>
					<key>ReadOnly</key>
					<string>NO</string>
					<key>Type</key>
					<string>NSTextStorage</string>
				</dict>
			</dict>
		</dict>
		<key>NSApplication</key>
		<dict>
			<key>AppleEventCode</key>
			<string>capp</string>
			<key>Superclass</key>
			<string>NSCoreSuite.NSApplication</string>
			<key>ToManyRelationships</key>
			<dict>
				<key>orderedDocuments</key>
				<dict>
					<key>AppleEventCode</key>
					<string>docu</string>
					<key>Type</key>
					<string>MyDocument</string>
				</dict>
			</dict>
		</dict>
	</dict>
	<key>Name</key>
	<string>ScriptableDocApp</string>
</dict>
</plist>

The ScriptableDocApp.scriptTerminology is exactly the same as last time and looks like this:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
	<key>Classes</key>
	<dict>
		<key>MyDocument</key>
		<dict>
			<key>Description</key>
			<string>A ScriptableDocApp document.</string>
			<key>Name</key>
			<string>document</string>
			<key>PluralName</key>
			<string>documents</string>
		</dict>
		<key>NSApplication</key>
		<dict>
			<key>Description</key>
			<string>ScriptableDocApp's top level scripting object.</string>
			<key>Name</key>
			<string>application</string>
			<key>PluralName</key>
			<string>applications</string>
		</dict>
	</dict>
	<key>Description</key>
	<string>ScriptableDocApp specific classes.</string>
	<key>Name</key>
	<string>ScriptableDocApp suite</string>
</dict>
</plist>
  

Add these files like we did last time, and then build and run the project.

AppleScript

All that work, but now we can do some really cool stuff.

Try this:

 
 set out to ""

tell application "ScriptableDocApp"
	activate
	set w to make new document at the beginning of documents
	
	set w to the first document
	set the name of w to "first"
	
	set y to count of documents
	set z to get every document
	set a to exists document 1
	
	
	set the text of w to "hello"
	set the font of the text of w to "Times"
	
	set out to get the last character of the first word of the text of w
	
end tell

return out

Pretty cool, huh?! The Text Suite is available from ScriptableDocApp's scripting dictionary, so you can try out other cool things.

Next Time

Finishing up with the Core Suite and laying the foundation for custom suites. See you then.

Brad Dominy is the head of Neoki, LLC, a small web design firm located in Chicago, IL.


Return to the Mac DevCenter.