{"id":150,"date":"2006-06-26T10:33:18","date_gmt":"2006-06-26T17:33:18","guid":{"rendered":"http:\/\/www.red-sweater.com\/blog\/150\/minimal-scriptability"},"modified":"2006-06-26T10:33:21","modified_gmt":"2006-06-26T17:33:21","slug":"minimal-scriptability","status":"publish","type":"post","link":"https:\/\/redsweater.com\/blog\/150\/minimal-scriptability","title":{"rendered":"Minimal Scriptability"},"content":{"rendered":"<p>The Cocoa Scripting layer makes it extremely easy to expose manipulation of your application&#8217;s model to users via AppleScript. That said, for most developers the learning curve associated with this can be far from &#8220;easy.&#8221; The psychological burden of tackling scripting support seems to prevent many developers, even those within Apple, from providing even the barest of scripting support for their applications.<\/p>\n<p>\nThe promise of Cocoa Scripting is essentially that some robust AppleEvent parsing code in the Cocoa frameworks will translate the &#8220;yucky, raw events&#8221; into method dispatches that &#8220;just work&#8221; with your application&#8217;s attribute-based data model. Then, a different chunk of robust code will take the Cocoa objects that your application works with and automatically translate them into suitable AppleScript representations. For instance if you expose an NSString attribute from your application&#8217;s data model, the text contents are automatically converted to an AppleScript-native format suitable to the scripter&#8217;s needs.  Similar bridging is done for objects as complex as windows and documents.\n<\/p>\n<p>\nThe power of this adaptive layer can be witnessed by taking an otherwise unscriptable application and &#8220;throwing the switch&#8221; that gives Cocoa permission to take over scripting. If you&#8217;ve got an unscriptable Cocoa-based application, why not use this as an opportunity to get your feet wet? In fact, Apple ships a good case study for this in Preview, so I&#8217;ll use that as a guinea pig while you follow along with whatever app you like. (thanks to <a href=\"http:\/\/www.daringfireball.net\/\">John Gruber<\/a> for pointing out this shortcoming and inspiring this article). Take the following script and paste it into Script Editor:<\/p>\n<div class=\"caption\">\n<pre>\ntell application \"Preview\"\n\tget name of document 1\nend tell\n<\/pre>\n<\/div>\n<p>\nWhen you run the script, even if you can see an open document in Preview, you&#8217;ll be met with a rather rude reply: &#8220;Can&#8217;t get name of document 1.&#8221; Please! You&#8217;re telling me you can&#8217;t even offer me that tiny glimpse into the application&#8217;s personal details? Unfortunately Apple didn&#8217;t &#8220;flip the switch&#8221; on Preview, so none of the easy freebies from Cocoa Scripting are exposed through it.\n<\/p>\n<p>\nSo let&#8217;s flip the switch for them. Let&#8217;s pretend we work for Apple in the Preview group and our manager has <em>finally<\/em> given us the go ahead to investigate AppleScript support for Preview. Heck, let&#8217;s assume we&#8217;re the new intern, it&#8217;s day one, and we haven&#8217;t even been given source code access yet!\n<\/p>\n<\/p>\n<p>\nBy navigating into the Preview.app package and opening the Info.plist document in Property List Editor (or an editor of your choice), we can add the requisite flag that instructs Cocoa Scripting to take over:\n<\/p>\n<p>\n<img decoding=\"async\" src=\"http:\/\/www.red-sweater.com\/blog\/images\/ScriptingInfoPlist.jpg\"\/>\n<\/p>\n<p>\nNow quit and relaunch both Preview and the Script Editor. Paste your example &#8220;name of document 1&#8221; script again and observe that the frontmost document&#8217;s name is returned!\n<\/p>\n<p>\n<img decoding=\"async\" src=\"http:\/\/www.red-sweater.com\/blog\/images\/SimplePreviewScript.jpg\"\/>\n<\/p>\n<p>\nNow you may be thinking, &#8220;Who cares? It&#8217;s just the title of the document, that&#8217;s not very useful.&#8221; But that&#8217;s not for you to decide! Let the script author decide. They&#8217;re the ones who have to use your program to get something done. The <em>big deal<\/em> here is that we turned an unscriptable application into a &#8220;scriptable beta&#8221; with the addition of a single Info.plist attribute. We didn&#8217;t even have to add a scripting dictionary!\n<\/p>\n<p>\nLest you think that the standard Cocoa Scripting functionality is only the name of the document, I will share a slightly more complex (albeit still contrived) example. Let&#8217;s suppose we have a hundred documents to review by hand with Preview. Based on some human-observable trait we want to open 25% of those documents later in Safari for printing or whatever. One option would be to go through and save a copy of each such document to a separate directory. When we&#8217;re done with the grueling day of work, we&#8217;ll drag the contents of that separate directory to Safari (or whatever application).\n<\/p>\n<p>\nUsing AppleScript and the &#8220;newly scriptable&#8221; Preview application, we can simplify this workflow by making the assumption that &#8220;any document with a tiny window&#8221; should be opened in Safari. Now as we go through the open documents one by one, we simply drag-resize the window to be less than 400 pixels wide.  Later on, we run the following script, which makes a list of all such windows and opens the corresponding documents in Safari:\n<\/p>\n<p>\n<img decoding=\"async\" src=\"http:\/\/www.red-sweater.com\/blog\/images\/ComplexPreviewScript.jpg\"\/>\n<\/p>\n<p>\nAll of this for free! We make a kick-ass intern! And all on our first day with no source code.\n<\/p>\n<p>\nBy no means am I suggesting that developers should just flip the switch and ship. It&#8217;s very frustrating to see unscriptable apps, but even worse when a product claims scriptability but for all practical purposes is not.  The point of this entry is merely to point out that Cocoa gives us a huge head start towards a decent scripting implementation, and to choose not to take advantage of that is cruel and unusual punishment to your users.\n<\/p>\n<p>\nJust think how cool Preview will be when we add a dictionary that describes its document object model&#8217;s custom attributes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Cocoa Scripting layer makes it extremely easy to expose manipulation of your application&#8217;s model to users via AppleScript. That said, for most developers the learning curve associated with this can be far from &#8220;easy.&#8221; The psychological burden of tackling scripting support seems to prevent many developers, even those within Apple, from providing even the [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,11,15],"tags":[],"class_list":["post-150","post","type-post","status-publish","format-standard","hentry","category-applescript","category-cocoa","category-programming"],"_links":{"self":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/comments?post=150"}],"version-history":[{"count":0,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"wp:attachment":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}