{"id":946,"date":"2015-10-28T12:27:18","date_gmt":"2015-10-28T19:27:18","guid":{"rendered":"http:\/\/blog.latenightsw.com\/?p=946"},"modified":"2015-10-28T18:01:11","modified_gmt":"2015-10-29T01:01:11","slug":"when-open-does-not-return-an-object-reference","status":"publish","type":"post","link":"http:\/\/markalldritt.com\/?p=946","title":{"rendered":"When Open Does Not Return An Object Reference"},"content":{"rendered":"<p><style type=\"text\/css\">\n    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Verdana}\n    p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Verdana; color: #321c62}\n    p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Verdana; min-height: 15.0px}\n    p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Verdana; color: #4b8c00}\n    span.s1 {color: #0433ff}\n    span.s2 {color: #000000}\n    span.s3 {color: #4b8c00}\n    span.s4 {color: #812fdc}\n    span.Apple-tab-span {white-space:pre}\n  <\/style><\/p>\n\n<p>There is a long standing bug in Cocoa Scripting, the technology Apple provides to developers to help make their applications scriptable, where the Open, Move and Copy commands do not return a reference to the newly created or moved object. This makes scripting these applications difficult.<\/p>\n\n<p>I frequently get requests from Script Debugger customers for help solving this problem.  I see people resorting to UI Scripting and many other techniques to address this problem.<\/p>\n\n<p>The code below shows one way of working around the problem of the Open command not returning a reference to the newly opened document. I use the Preview application in my code, but this technique works equally well with other applications which fail to return a document reference from their Open commands.  <em>This technique fails is the file is already open<\/em>.<\/p>\n\n<p class=\"p1\"><span class=\"s1\"><b>set<\/b> <\/span><span class=\"s2\">theFile<\/span><span class=\"s1\"> <b>to<\/b> <\/span><span class=\"s3\"><b>choose file<\/b><\/span><span class=\"s1\"> <\/span><i>&#8212; pick a file to open<\/i><\/p>\n\n<p><p class=\"p3\"><\/p>\n<p class=\"p1\"><b>tell<\/b> <span class=\"s1\"><i>application<\/i><\/span> &#8220;Preview&#8221;<\/p>\n<p class=\"p2\"><span class=\"s2\">&nbsp;&nbsp;&nbsp;<b>set<\/b> <\/span><span class=\"s3\">theOldOpenFiles<\/span><span class=\"s2\"> <b>to<\/b> <\/span><span class=\"s4\">path<\/span><span class=\"s2\"> <b>of<\/b> <\/span><span class=\"s1\"><i>documents<\/i><\/span><span class=\"s2\"> <\/span><i>&#8212; sample the existing open documents<\/i><\/p>\n<p class=\"p2\"><span class=\"s2\">&nbsp;&nbsp;&nbsp;<\/span><span class=\"s1\"><b>open<\/b><\/span><span class=\"s2\"> <\/span><span class=\"s3\">theFile<\/span><span class=\"s2\"> <\/span><i>&#8212; no new document reference returned &#8211; sigh<\/i><\/p>\n<p class=\"p2\"><span class=\"s2\">&nbsp;&nbsp;&nbsp;<b>set<\/b> <\/span><span class=\"s3\">theOpenFiles<\/span><span class=\"s2\"> <b>to<\/b> <\/span><span class=\"s4\">path<\/span><span class=\"s2\"> <b>of<\/b> <\/span><span class=\"s1\"><i>documents<\/i><\/span><span class=\"s2\"> <\/span><i>&#8212; sample the new list of open documents<\/i><\/p>\n<p class=\"p1\"><b>end<\/b> <b>tell<\/b><\/p>\n<p class=\"p3\"><br><\/p>\n<p class=\"p2\"><i>&#8212;&nbsp;&nbsp;&nbsp;Find the item in the list of new open documents that is not present in the old list of<span class=\"Apple-converted-space\">\u00a0<\/span><\/i><\/p>\n<p class=\"p2\"><i>&#8212;&nbsp;&nbsp;&nbsp;open documents.<\/i><\/p>\n<p class=\"p4\"><span class=\"s2\"><b>set<\/b> <\/span>theNewFiles<span class=\"s2\"> <b>to<\/b> <\/span>finddifference<span class=\"s2\"> <b>of<\/b> <\/span>theOpenFiles<span class=\"s2\"> <\/span><span class=\"s1\">against<\/span><span class=\"s2\"> <\/span>theOldOpenFiles<\/p>\n<p class=\"p3\"><br><\/p>\n<p class=\"p1\"><b>tell<\/b> <span class=\"s1\"><i>application<\/i><\/span> &#8220;Preview&#8221;<\/p>\n<p class=\"p2\"><span class=\"s2\">&nbsp;&nbsp;&nbsp;<\/span><i>&#8212;&nbsp;&nbsp;recover the reference to the newly opened document<\/i><\/p>\n<p class=\"p1\">&nbsp;&nbsp;&nbsp;<b>set<\/b> <span class=\"s3\">theOpenedDocument<\/span> <b>to<\/b> <b>first<\/b> <span class=\"s1\"><i>document<\/i><\/span> <b>where<\/b> <span class=\"s4\">path<\/span> <b>is<\/b> (<span class=\"s1\"><i>item<\/i><\/span> 1 <b>of<\/b> <span class=\"s3\">theNewFiles<\/span>)<\/p>\n<p class=\"p3\">&nbsp;&nbsp;&nbsp;<\/p>\n<p class=\"p4\"><span class=\"s2\">&nbsp;&nbsp;&nbsp;<b>tell<\/b> <\/span>theOpenedDocument<\/p>\n<p class=\"p2\"><span class=\"s2\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><i>&#8212;&nbsp;&nbsp;your code targetting the newly opened document goes here&#8230;<\/i><\/p>\n<p class=\"p4\"><span class=\"s2\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>set<\/b> <\/span>theDocumentName<span class=\"s2\"> <b>to<\/b> <\/span><span class=\"s4\">name<\/span><\/p>\n<p class=\"p1\">&nbsp;&nbsp;&nbsp;<b>end<\/b> <b>tell<\/b><\/p>\n<p class=\"p1\"><b>end<\/b> <b>tell<\/b><\/p>\n<p class=\"p3\"><br><\/p>\n<p class=\"p4\"><span class=\"s2\"><b>to<\/b> <\/span>finddifference<span class=\"s2\"> <b>of<\/b> <\/span>listA<span class=\"s2\"> <\/span><span class=\"s1\">against<\/span><span class=\"s2\"> <\/span>listB<\/p>\n<p class=\"p2\"><span class=\"s2\">&nbsp;&nbsp;&nbsp;<\/span><i>&#8212;&nbsp;&nbsp;This handler computes the &#8216;difference&#8217; between the contents of two lists<\/i><\/p>\n<p class=\"p1\">&nbsp;&nbsp;&nbsp;<b>local<\/b> <span class=\"s3\">newList<\/span><\/p>\n<p class=\"p3\">&nbsp;&nbsp;&nbsp;<\/p>\n<p class=\"p1\">&nbsp;&nbsp;&nbsp;<b>set<\/b> <span class=\"s3\">newList<\/span> <b>to<\/b> {}<\/p>\n<p class=\"p1\">&nbsp;&nbsp;&nbsp;<b>repeat<\/b> <b>with<\/b> <span class=\"s3\">a<\/span> <b>in<\/b> <span class=\"s3\">listA<\/span><\/p>\n<p class=\"p2\"><span class=\"s2\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>set<\/b> <\/span><span class=\"s3\">a<\/span><span class=\"s2\"> <b>to<\/b> <\/span><span class=\"s4\">contents<\/span><span class=\"s2\"> <b>of<\/b> <\/span><span class=\"s3\">a<\/span><span class=\"s2\"> <\/span><i>&#8212; dereference implicit loop reference<\/i><\/p>\n<p class=\"p1\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>if<\/b> {<span class=\"s3\">a<\/span>} <b>is not<\/b> <b>in<\/b> <span class=\"s3\">listB<\/span> <b>then<\/b> <b>set<\/b> <b>end<\/b> <b>of<\/b> <span class=\"s3\">newList<\/span> <b>to<\/b> <span class=\"s3\">a<\/span><\/p>\n<p class=\"p1\">&nbsp;&nbsp;&nbsp;<b>end<\/b> <b>repeat<\/b><\/p>\n<p class=\"p4\"><span class=\"s2\">&nbsp;&nbsp;&nbsp;<\/span>newList<\/p>\n<p class=\"p4\"><span class=\"s2\"><b>end<\/b> <\/span>finddifference<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a long standing bug in Cocoa Scripting, the technology Apple provides to developers to help make their applications scriptable, where the Open, Move&#8230;<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"http:\/\/markalldritt.com\/?p=946\">Continue Reading<span class=\"screen-reader-text\">When Open Does Not Return An Object Reference<\/span><\/a><\/div><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[6],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7AQk-fg","_links":{"self":[{"href":"http:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/posts\/946"}],"collection":[{"href":"http:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/markalldritt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=946"}],"version-history":[{"count":11,"href":"http:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/posts\/946\/revisions"}],"predecessor-version":[{"id":957,"href":"http:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/posts\/946\/revisions\/957"}],"wp:attachment":[{"href":"http:\/\/markalldritt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/markalldritt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=946"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/markalldritt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}