{"id":664,"date":"2016-08-13T11:44:45","date_gmt":"2016-08-13T18:44:45","guid":{"rendered":"http:\/\/10.0.1.201\/?p=664"},"modified":"2016-08-13T11:44:45","modified_gmt":"2016-08-13T18:44:45","slug":"set-file-ownership-applescript","status":"publish","type":"post","link":"https:\/\/strawhousepig.net\/wordpress\/2016\/08\/13\/set-file-ownership-applescript\/","title":{"rendered":"Set file ownership AppleScript script"},"content":{"rendered":"<p>I occasionally transfer files from WINDOWS to a Mac anonymously and upon doing so end up with a file owned by &#8216;nobody&#8217;. So I wrote this to set the owner to the person who runs this as a Folder Action. Can also be run from the AppleScript menuextra.<!--more--><\/p>\n<p>Warning: Has not been tested under Fast User Switching. In fact I only use it with the Mac user logged into the GUI.<\/p>\n<p><code>(*<br \/>\nSet owner to me<br \/>\nhttp:\/\/strawhousepig.net\/<\/p>\n<p>This Folder Action handler is triggered whenever items are added to the attached folder. Can also be run as-is from the AppleScript menuextra.<\/p>\n<p>This script will run a shell command (chown) to change the owner to the user who attached the action to the folder. This can be helpful when adding files over SMB to a drop share.<br \/>\nWARNING: This has not been tested for multiple user activations on a common shared folder!<\/p>\n<p>Because we don't own the files we need to use \"administrator privileges\" to complete this task. If you do not have an admin password do not bother with this. <\/p>\n<p>Setting mpassword to \"prompt\" will cause this script to prompt you for your admin password when files are added. The dialog may seem ambiguous when used as a Folder Action.<\/p>\n<p>If you decide to store your admin password in this script DO NOT share this script without removing your password!<\/p>\n<p>The file is safe (from non-administrators) if stored in ~\/Library\/Scripts\/Folder Action Scripts<br \/>\n*)<\/p>\n<p>-- Set this and it is ready to go. See above for instructions!<br \/>\nproperty mpassword : \"prompt\"<br \/>\nproperty defgroup : \":wheel\" -- Anything other than \"\" (no group alteration) MUST be prefixed with a colon (:)<br \/>\nproperty defpriv : \"755\" -- See the 'chmod' man pages for more info.<\/p>\n<p>on adding folder items to this_folder after receiving added_items<br \/>\n\tdelay 60 --If we continue while the file is open we die. Maybe 'lsof' could be useful here...<br \/>\n\tjustDOit(added_items)<br \/>\nend adding folder items to<\/p>\n<p>on run<br \/>\n\ttell application \"Finder\" to set _here to (choose folder) as list<br \/>\n\tjustDOit(_here)<br \/>\nend run<\/p>\n<p>on justDOit(_these)<br \/>\n\ttry<br \/>\n\t\tset whoiam to (do shell script \"whoami\")<br \/>\n\t\tif mpassword is \"prompt\" then<br \/>\n\t\t\tset mpassword to text returned of (display dialog \"You must enter the administrator password for user '\" & whoiam & \"' to set the owner of these files.\" default answer \"\" with title \"Set owner to me.scpt\" with hidden answer)<br \/>\n\t\tend if<br \/>\n\t\trepeat with i in _these<br \/>\n\t\t\tdo shell script \"_here=\" & quoted form of POSIX path of i & \";chown -R \" & whoiam & defgroup & \" \\\"$_here\\\";chmod -R \" & defpriv & \" \\\"$_here\\\"\" password mpassword with administrator privileges<br \/>\n\t\tend repeat<br \/>\n\ton error theErr<br \/>\n\t\tdo shell script \"logger 'Folder Actions was unable to set ownership. Error: \" & theErr & \"'\"<br \/>\n\tend try<br \/>\nend justDOit<br \/>\n(*<br \/>\nModified from: add - new item alert<\/p>\n<p>Copyright \u00a9 2002\u20132007 Apple Inc.<\/p>\n<p>You may incorporate this Apple sample code into your program(s) without<br \/>\nrestriction.  This Apple sample code has been provided \"AS IS\" and the<br \/>\nresponsibility for its operation is yours.  You are not permitted to<br \/>\nredistribute this Apple sample code as \"Apple sample code\" after having<br \/>\nmade changes.  If you're going to redistribute the code, we require<br \/>\nthat you make it clear that the code was descended from Apple sample<br \/>\ncode, but that you've made changes.<br \/>\n*)<br \/>\n<\/code><\/p>\n<p>UPDATE 2016-08-25: Changed to use a &#8216;hidden answer&#8217; dialog to retrieve a password when set to &#8220;prompt&#8221;.<br \/>\nUPDATE 2016-08-29: Added &#8216;chmod&#8217; to the &#8216;do shell script&#8217; command.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I occasionally transfer files from WINDOWS to a Mac anonymously and upon doing so end up with a file owned by &#8216;nobody&#8217;. So I wrote this to set the owner to the person who runs this as a Folder Action. Can also be run from the AppleScript menuextra.<\/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,28],"class_list":["post-664","post","type-post","status-publish","format-standard","hentry","category-code","tag-applescript-2","tag-macintosh","tag-os-x"],"_links":{"self":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/664","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=664"}],"version-history":[{"count":0,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/664\/revisions"}],"wp:attachment":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/media?parent=664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/categories?post=664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/tags?post=664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}