{"id":35797,"date":"2022-05-07T15:43:33","date_gmt":"2022-05-07T19:43:33","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=35797"},"modified":"2022-06-10T16:25:23","modified_gmt":"2022-06-10T20:25:23","slug":"jxas-parenthesis-paradox","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/05\/07\/jxas-parenthesis-paradox\/","title":{"rendered":"JXA&rsquo;s Parenthesis Paradox"},"content":{"rendered":"<p><a href=\"https:\/\/leancrew.com\/all-this\/2022\/05\/jxas-parenthesis-paradox\/\">Dr. Drang<\/a>:<\/p>\n<blockquote cite=\"https:\/\/leancrew.com\/all-this\/2022\/05\/jxas-parenthesis-paradox\/\"><p>Because the name of the process is what we want to return, <code>name<\/code> has to go last in the chain of calls, and it&rsquo;s the one that gets the parentheses. We&rsquo;re applying the <code>whose<\/code> filter to the processes, so it has to go after <code>processes<\/code>. The properties we&rsquo;re filtering on (just <code>visible<\/code> in this case) are set up as a JavaScript object that&rsquo;s passed as an argument to <code>whose<\/code>.<\/p><p>Suppose we had two criteria for our filter? Let&rsquo;s say we want all the visible processes that start with the letter <em>M<\/em> (which will return just Mail and Messages). In AppleScript, it&rsquo;s easy to add an extra clause:<\/p><pre>tell application \"System Events\" to get the name of every process &not;\n    whose visible is true and name begins with \"M\"\n<\/pre><p>In JXA, the syntax gets nasty:<\/p><pre><code>Application('System Events').processes.whose(\n    {visible: true, name: {_beginsWith: \"M\"}}).name();<\/code><\/pre><\/blockquote>\n<p>JavaScript is nicer for basic programming and data manipulation, but Apple event stuff is much nicer in AppleScript.<\/p>\n<p>Update (2022-05-10): See also: <a href=\"https:\/\/leancrew.com\/all-this\/2022\/05\/javascript-vs-applescript\/\">JavaScript vs. AppleScript<\/a>.<\/p>\n\n<p id=\"jxas-parenthesis-paradox-update-2022-06-10\">Update (2022-06-10): <a href=\"https:\/\/leancrew.com\/all-this\/2022\/06\/jxa-and-applescript-followup\/\">Dr. Drang<\/a>:<\/p>\n<blockquote cite=\"https:\/\/leancrew.com\/all-this\/2022\/06\/jxa-and-applescript-followup\/\">\n<p>Next, we move on to whether <a href=\"https:\/\/developer.apple.com\/library\/archive\/releasenotes\/InterapplicationCommunication\/RN-JavaScriptForAutomation\/Articles\/Introduction.html#\/\/apple_ref\/doc\/uid\/TP40014508\">JavaScript for Automation<\/a> (JXA) is worth getting into at all. Do AppleScript concepts really translate well to JavaScript? The same day <a href=\"https:\/\/leancrew.com\/all-this\/2022\/05\/jxas-parenthesis-paradox\/\">my last post<\/a> was published, Rosemary and David released their <a href=\"https:\/\/www.relay.fm\/automators\/101\">Automators episode with Daniel Jalkut<\/a>. While discussing Daniel&rsquo;s <a href=\"https:\/\/redsweater.com\/fastscripts\/\">FastScripts app<\/a>, they had a little sidebar about JXA. Daniel doesn&rsquo;t think much of it. &ldquo;It just shouldn&rsquo;t be there&rdquo; was his most pithy comment, but he also mentioned an &ldquo;impedance mismatch&rdquo; between the JavaScript language and the AppleScript infrastructure underneath it. Exactly.<\/p>\n<p>[&#8230;]<\/p>\n<p>Finally, let&rsquo;s talk about the reason I&rsquo;ve used JXA for some scripts: JavaScript has a good regex engine and AppleScript has none.<\/p>\n<\/blockquote>\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/03\/14\/fastscripts-3-1\/\">FastScripts 3.1<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Dr. Drang: Because the name of the process is what we want to return, name has to go last in the chain of calls, and it&rsquo;s the one that gets the parentheses. We&rsquo;re applying the whose filter to the processes, so it has to go after processes. The properties we&rsquo;re filtering on (just visible in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2022-05-07T19:43:35Z","apple_news_api_id":"0a504e24-679e-4749-baaa-23445688d59c","apple_news_api_modified_at":"2022-06-10T20:25:25Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAg==","apple_news_api_share_url":"https:\/\/apple.news\/AClBOJGeeR0m6qiNEVojVnA","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[2],"tags":[159,1072,30,2077,71],"class_list":["post-35797","post","type-post","status-publish","format-standard","hentry","category-technology","tag-applescript","tag-javascript-for-automation","tag-mac","tag-macos-12","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=35797"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35797\/revisions"}],"predecessor-version":[{"id":36181,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35797\/revisions\/36181"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=35797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=35797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=35797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}