{"id":1587,"date":"2011-11-16T11:53:51","date_gmt":"2011-11-16T19:53:51","guid":{"rendered":"http:\/\/manytricks.com\/blog\/?p=1587"},"modified":"2024-04-29T18:35:14","modified_gmt":"2024-04-30T01:35:14","slug":"how-to-use-butler-and-applescript-to-open-a-folder","status":"publish","type":"post","link":"https:\/\/manytricks.com\/blog\/?p=1587","title":{"rendered":"How-to: Use Butler and AppleScript to open a folder"},"content":{"rendered":"<p><!--RGSupportContent-->I know what you&#8217;re thinking&hellip;how hard can it be to open a folder? Not very hard at all. But what I wanted was a way to open a certain folder with a certain view and sort order, even though I may then change that folder&#8217;s view and sort order, thereby messing up future openings of that same folder. In my case, I wanted my Downloads folder to open, at a given size and location, sorted in reverse date order. <a href=\"\/moom\">Moom<\/a> can do 90% of this using saved window layouts, but it can&#8217;t do anything with the actual Finder window, such as setting the view and sort column.<\/p>\n<p>After a bit of work with AppleScript, I came up with a solution that got me everything I wanted. By storing the AppleScript in <a href=\"\/butler\">Butler<\/a>, I can now open my folder with the press of a hot key, at any time from any application. Here&#8217;s how I did it.<\/p>\n<p><!--more--><\/p>\n<p>First, open Butler&#8217;s settings window, and click on the Configuration tab. Click the plus sign at the bottom left corner of the window, select Smart Item > AppleScript > AppleScript:<\/p>\n<p class=\"centercontainer\"><img decoding=\"async\" src=\"\/images\/blogimages\/bas_newscript.png\" class=\"nonzoomingscreenshot\"><\/p>\n<p>Once this item has been added to Butler&#8217;s configuration, you&#8217;ll need to decide where you want it: visible within one of the Menu Bar areas, or as a Hidden item that you&#8217;ll activate only via the keyboard and\/or mouse. I keep mine in the Hidden section, but you can drag the entry wherever you&#8217;d like to use it.<\/p>\n<p>Next, create the AppleScript. Click on your newly-added action in the left-hand pane of Butler&#8217;s configuration window, and then click on the Source Code tab on the right. While you&#8217;re here, click on the box at the top and name your action (Open Downloads Folder or whatever); make sure you press Tab when done so the change is saved. In the Source Code area, paste the following code:<\/p>\n<p style=\"text-align:center; background-color: #ccc; line-height:1.5em; margin-bottom: 0px;\">Click anywhere in the box to copy the code<\/p>\n<div id=\"MoomButlerCode\" class=\"rg_scrollbox applescript\" style=\"height:150px\">\n<span class=\"asbb\">tell<\/span> <span class=\"asibu\">application<\/span> &quot;Finder&quot;<br \/>\n  <span class=\"asbb\">set<\/span> <span class=\"asgr\">myWindow<\/span> <span class=\"asbb\">to<\/span> <span class=\"asbub\">make<\/span> <span class=\"asibu\">Finder window<\/span> <span class=\"asbu\">to<\/span> <span class=\"asibu\">folder<\/span> &quot;path:to:folder:to:open&quot;<br \/>\n  <span class=\"asbb\">set<\/span> <span class=\"aspu\">current view<\/span> <span class=\"asbb\">of<\/span> <span class=\"asgr\">myWindow<\/span> <span class=\"asbb\">to<\/span> <span class=\"aspu\">list view<\/span><br \/>\n  <span class=\"asbb\">set the<\/span> <span class=\"aspu\">sort column<\/span> <span class=\"asbb\">of the<\/span> <span class=\"asibu\">list view options<\/span> <span class=\"asbb\">of<\/span> <span class=\"asgr\">myWindow<\/span> <span class=\"asbb\">to the<\/span> <span class=\"aspu\">modification date column<\/span><br \/>\n  <span class=\"asbub\">activate<\/span><br \/>\n<span class=\"asbb\">end tell<\/span>\n<\/div>\n<p>You&#8217;ll need to replace <tt>path:to:folder:to:open<\/tt> with the path to the folder you wish to open. The path starts at the root of the hard drive for local folders; if the folder is on a networked volume, start with the name of the mounted drive. You can also modify the view settings; you don&#8217;t have to use list view sorted by date modified. Here are the options available for each:<\/p>\n<ul>\n<li>Current view: <span style=\"font-family:menlo,monaco,monospace; font-size:80%\"><span class=\"aspu\">icon view<\/span>, <span class=\"aspu\">list view<\/span>, <span class=\"aspu\">column view<\/span>, <span class=\"aspu\">group view<\/span>, <span class=\"aspu\">flow view<\/span><\/span><\/li>\n<li>Sort column: <span style=\"font-family:menlo,monaco,monospace; font-size:80%\"><span class=\"aspu\">name column<\/span>, <span class=\"aspu\">modification date column<\/span>, <span class=\"aspu\">creation date column<\/span>, <span class=\"aspu\">size column<\/span>, <span class=\"aspu\">kind column<\/span>, <span class=\"aspu\">label column<\/span>, <span class=\"aspu\">version column<\/span>, <span class=\"aspu\">comment column<\/span><\/span><\/li>\n<\/ul>\n<p>You should probably test your code in AppleScript editor prior to pasting into Butler; it&#8217;s much easier to test there. Once it&#8217;s all working, paste it into Butler&#8217;s Source Code tab, then click over to the Triggers tab. Assign a Hot Key of your choice, and\/or a hot corner to use to activate your AppleScript.<\/p>\n<p>Once you&#8217;ve done that, you&#8217;re good to go. Press your trigger or move to your hot corner, and your script will trigger. In my case, it takes a couple seconds to run, as I&#8217;m opening a folder on a networked drive. But even with this delay, pressing a hot key beats navigating to Finder and then opening and customizing the window again.<!--\/RGSupportContent--><\/p>\n<p><script>\n    document.getElementById('MoomButlerCode').addEventListener('click', function() {\n        var codeToCopy = this.textContent;\n        var tempTextArea = document.createElement('textarea');\n        tempTextArea.value = codeToCopy;\n        document.body.appendChild(tempTextArea);\n        tempTextArea.select();\n        document.execCommand('copy');\n        document.body.removeChild(tempTextArea);\n        alert('AppleScript code copied to clipboard.');\n    });\n    <\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I know what you&#8217;re thinking&hellip;how hard can it be to open a folder? Not very hard at all. But what I wanted was a way to open a certain folder with a certain view and sort order, even though I may then change that folder&#8217;s view and sort order, thereby messing up future openings of [&hellip;]<\/p>\n","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_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5,26,4],"tags":[],"coauthors":[21],"class_list":["post-1587","post","type-post","status-publish","format-standard","hentry","category-butler","category-how-to","category-products"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1587"}],"version-history":[{"count":29,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1587\/revisions"}],"predecessor-version":[{"id":5991,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1587\/revisions\/5991"}],"wp:attachment":[{"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1587"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/manytricks.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcoauthors&post=1587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}