{"id":729,"date":"2018-07-05T23:14:52","date_gmt":"2018-07-06T06:14:52","guid":{"rendered":"http:\/\/10.0.1.201\/?p=729"},"modified":"2018-07-05T23:14:52","modified_gmt":"2018-07-06T06:14:52","slug":"applescript-folder-action-to-auto-trim-folder","status":"publish","type":"post","link":"https:\/\/strawhousepig.net\/wordpress\/2018\/07\/05\/applescript-folder-action-to-auto-trim-folder\/","title":{"rendered":"AppleScript Folder Action to auto-trim folder"},"content":{"rendered":"<p><ins datetime=\"2018-07-13T01:17:46+00:00\">[UPDATE] This does not work as expected. Doesn&#8217;t seem to run as a plain AppleScript and when re-worked into an Automator folder action workflow (word salad anyone?) it might work&#8230; It has and it also hasn&#8217;t. But, even when it has I have the sneaking suspicion iCloud does not propagate deletes from macOS. Wouldn&#8217;t be the only inexplicable behavior from that service.<\/ins><\/p>\n<p><ins datetime=\"2019-01-02T15:37:12+00:00\">[UPDATE]This has been moved to a Google Drive folder. I&#8217;ve had enough of ghost folders taking up all the space. The principle is however the same, except now it just works. Boom. Roasted.<\/ins><\/p>\n<p>I have a security camera that FTPs a new folder everyday into a local iCloud folder on my home server. FYI, the real location of your iCloud Drive is within <strong>~\/Library\/Mobile Documents<\/strong>, which is a package folder. <\/p>\n<p>This storage process has not proved ideal since iCloud doesn&#8217;t handle automation very well. It works with Finder windows and Save dialogs, but background processes not so much. The result for me has been constant reminders that my iCloud drive is &#8220;full&#8221; despite deleting the bulk of folders. Most of the space never comes back and many times folders just &#8220;deleted&#8221; reappear.<\/p>\n<p>Maybe adding another automated process to this soup is not going to help and may make things worse (will <a href=\"x-man-page:\/\/bird\" title=\"man page for an iCloud sync daemon.\">bird<\/a> even notice when items are removed this way?), but what the hell. This script will delete files oldest first until the number of items it counted when it was called reaches the &#8220;keep&#8221; count (set to &#8220;5&#8221; here).<\/p>\n<p><em>This may be attached to any folder<\/em>, and items may be sent to the Trash or deleted immediately with <strong>rm<\/strong>. <del datetime=\"2019-01-02T15:37:12+00:00\">but be aware that it does not send files to the Trash. They are removed with rm.<\/del><\/p>\n<p><code>-- http:\/\/strawhousepig.net\/<\/p>\n<p>-- Number of items to keep.<br \/>\nproperty keep : 5<\/p>\n<p>-- Number of seconds until the script may run again (incase of runaway process adding to folder).<br \/>\nproperty cycle : 10<\/p>\n<p>-- plist to store time last run.<br \/>\nproperty the_dom : \"applescript.folderaction.trimfolder\"<\/p>\n<p>-- Should items be deleted immediately (set to false) or sent to the Trash (set to true)?<br \/>\nproperty trashit : true<\/p>\n<p>on adding folder items to this_folder after receiving added_items<br \/>\n\tdo_it(this_folder)<br \/>\nend adding folder items to<\/p>\n<p>on run<br \/>\n\tset this_folder to choose folder<br \/>\n\tdo_it(this_folder)<br \/>\nend run<\/p>\n<p>on do_it(this_folder)<br \/>\nset the_key to \"epoch\"<br \/>\nset the_val to (do shell script \"date '+%s'\")<br \/>\n\tif my read_array(the_dom, the_key, the_val, cycle) is true then return<br \/>\n\tdo shell script \"defaults write \" & the_dom & \" \" & the_key & \" \" & quoted form of the_val<br \/>\n\ttell application \"Finder\"<br \/>\n\t\tset _items to items of this_folder<br \/>\n\t\tset _items to (sort _items by creation date)<br \/>\n\tend tell<br \/>\n\tif (count of _items) is greater than keep then<br \/>\n\t\ttry<br \/>\n\t\t\trepeat until (count of _items) is equal to keep<br \/>\n\t\t\t\tif trashit is true then<br \/>\n\t\t\t\t\tdelete item 1 of _items<br \/>\n\t\t\t\telse<br \/>\n\t\t\t\t\tdo shell script \"rm -R \" & quoted form of POSIX path of (item 1 of _items as alias)<br \/>\n\t\t\t\tend if<br \/>\n\t\t\t\tset _items to rest of _items<br \/>\n\t\t\tend repeat<br \/>\n\t\ton error theErr number theNum<br \/>\n\t\t\tif theNum is not -128 then display dialog theErr giving up after 60<br \/>\n\t\tend try<br \/>\n\tend if<br \/>\nend do_it<\/p>\n<p>on read_array(the_dom, the_key, the_val, cycle)<br \/>\n\ttry<br \/>\n\t\tset last_run to (do shell script \"defaults read \" & the_dom & \" \" & the_key)<br \/>\n\ton error -- Will error if there is nothing to read.<br \/>\n\t\treturn false<br \/>\n\tend try<br \/>\n\tif the_val - last_run is less than cycle then<br \/>\n\t\treturn true<br \/>\n\telse<br \/>\n\t\treturn false<br \/>\n\tend if<br \/>\nend read_array<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[UPDATE] This does not work as expected. Doesn&#8217;t seem to run as a plain AppleScript and when re-worked into an Automator folder action workflow (word salad anyone?) it might work&#8230; It has and it also hasn&#8217;t. But, even when it has I have the sneaking suspicion iCloud does not propagate deletes from macOS. Wouldn&#8217;t be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,23,24,28],"class_list":["post-729","post","type-post","status-publish","format-standard","hentry","category-code","tag-applescript-2","tag-macintosh","tag-macos","tag-os-x"],"_links":{"self":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/comments?post=729"}],"version-history":[{"count":0,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/729\/revisions"}],"wp:attachment":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/media?parent=729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/categories?post=729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/tags?post=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}