Integrating AppleScript and Cocoa
Pages: 1, 2, 3, 4
Back in the project window, you should now see, in the Resources folder,
the ScriptableDocApp.scriptSuite file and the
ScriptableDocApp.scriptTerminology file. If you select either one, you
should see the XML in the editor window. Now, we need to localize the
scriptTerminology file, so select the ScriptableDocApp.scriptTerminology
file and from the Project menu choose "Show Info."
The Info window for ScriptableDocApp.scriptTerminology |
In the info window
that appears, you should see the ScriptableDocApp.scriptTerminology
file listed in the Name field and on the right there is a drop-down menu that
says "Localization & Platforms." Select "Make Localized" from this menu,
and you'll see an expansion arrow appear next to the file name in the
Resources folder. From the same "Localization & Platforms" menu, choose
"Add Localized Variant..." and in the panel that appears, click on
the right arrow and select "English." You could also just type "English" into the panel.
Adding an English localized variant of our ScriptableDocApp.scriptTerminology file. |
Click on the OK button and then you can close the Info window.
Choose Build and Run from the Build menu, saving any files that need it.
Try running the script again from the Script Editor, and you should now see a new document window being created each time the script runs. If you have any trouble, you can download my ScriptableDocApp project file here.
One other interesting thing you can try is to play with the script
Terminology file. If you change "document" to be "my document," and
"documents" to be "my documents" in the
ScriptableDocApp.scriptTerminology file and then save and rebuild your
application, you will change the manner to which the MyDocument class is referred. To
see this, you will need to quit out of the Script Editor application,
because it will cache the older version of the terminology from
ScriptableDocApp until you do, and then restart Script Editor and paste
in this script:
tell application "ScriptableDocApp"
activate
set w to make new my document at the beginning of my documents
--close every document
end tell
Run it, and you should see it work the way it did before, but now with new terminology. You can also look at the ScriptAbleDocApp's script dictionary to see how the terminology changes. One thing to be careful of when changing the script Suite or Terminology files is that Script Editor will need to be restarted in order to get the latest verison of both files. You can save your scripts as text files to save them between restarts. It is a pain, but keep in mind that the syntax and terminology for your applications won't often change, so caching is generally a good idea.
One other gotcha I have run into is that if you Clean the project, the
NSAppleScriptEnabled key will be removed from the Info.plist file. You can
simply add this back in after cleaning, so if you suddenly find your
application is not responding to scripting at all, check to see if Info.plist has been changed.
Next Time
We are going to continue adding support for the Core Suite in ScriptableDocApp, including adding some text and the ability to load, save, and print our documents. This will mean jumping into ObjC and writing some code, so if you are not familiar with the basics of ObjC, check out Mike Beam's column or some of the tutorials that come with the Apple developer tools.
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.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 13 of 13.
-
Next time...
2002-12-16 15:05:52 anonymous2 [Reply | View]
I can't wait for the follow up! It has been many months now :-(
-
errors in ScriptableDocApp.scriptSuite
2002-10-01 15:51:19 anonymous2 [Reply | View]
the text for file "ScriptableDocApp.scriptSuite" as shown in your article contain errors in line 30 and 40. the "<<" and the ">>" should be single "<" and ">"
30:<key>AppleEventCode<</key>
...
40:</dict>>
now it works as it should.
-
Skip the OOP review
2002-03-05 07:21:26 snackdog [Reply | View]
I am interested in this topic but I skipped over the whole first page on basic concepts of object oriented programming (OOP) since I've been working with these concepts for years -- I suspect a lot of experienced programmers feel this way. My suggestion is that you include a link to another article on OOP and then but just the text you need to link general OOP concepts to the material at hand.
-
Script Editor execution error alert said
2002-02-26 17:31:18 psheldon [Reply | View]
"ScriptableDocApp got an error: Can't make a document."
-
PropertyList Editor can't parse ScriptableDocApp.scriptSuite
2002-02-26 17:29:42 psheldon [Reply | View]
Acrobat PDFWriter with Netscape® Communicator 4.75 took away the pretty print from the web page, so did newer IE. I put in my own indents and my eye to checking the XML syntax. Used find next to see matched number of <dict> and </dict>. Then got courage to finish parsing that aspect with pretty print indents.
I noted </dict>> at the bottom but getting rid of the >> and replacing with > didn't help with PropertyList Editor. IE did not show that syntax error. Wierd. Netscape now has gone wierd not showing the XML tags where it evidentally had when I first produced the pdf file.
Well, I'm stopping here on this one.
It looks like my applescript isn't working, perhaps as a consequence of this bad file, but that deserves a separate thread.
-
PropertyList Editor can't parse ScriptableDocApp.scriptSuite
2002-04-23 16:27:31 xyzzy-xyzzy [Reply | View]
There's also a double << in the bottom few lines somewhere. Sorry, I don't remember exactly where it was, but I had to correct it.
Didn't make the AppleScript work any better, but PropertyListEditor opened it.
-
No root class
2002-02-24 08:47:35 tib007 [Reply | View]
«This can continue over and over again, but eventually the proccess must end at the highest superclass, which is just the root object class. You can't get more general than that!»
There is no root class in Objective-C or C++. There is a root class in Cocoa because the implementation of the framework. In OOP there is usually many root classes.
-
Adding NSAppleScriptEnabled key incorrectly
2002-02-23 10:26:29 scott@nocturne.org [Reply | View]
To properly set the NSAppleScriptEnabled key you need to set it in the target, not the Info.plist in the Products folder (which is just a view of the built product).
Go to the application target and click on the 'Application Settings' tab. This will bring up UI for setting various Info.plist settings. Unfortunately, the NSAppleScriptEnabled key doesn't custom UI for it, so you'll need to switch to the 'expert' mode (click on the toggle button in the upper right) and add the key to the table.
Now when you clean, the key will be regenerated properly. -
tried what you said
2002-02-26 17:34:01 psheldon [Reply | View]
But, I think author's last applescript article had the same prescription of changing XML in a view and that did work for me, so I'm not sure you are right in what you said. More confusing is that evidentally yours worked after you did that, but mine didn't. I am clueless now. -
tried what you said
2002-03-19 18:37:32 Michael Beam |
[Reply | View]
They''re both right. The Application Settings information in the build target gets compiled into Info.plist, so its just a matter of you typing in the XML, or letting project builder write it. However, if you do a clean build of the target, Info.plist will be deleted and a new one created from PB's Application Settings, so its best to put any keys in there, and not by hand in Info.plist.






I would love to know how to send commands to a scriptable application (ie Photoshop) from cocoa, and receive back a result.
Alas, the command line app osascript does not pass or return variables.