Set file ownership AppleScript script

I occasionally transfer files from WINDOWS to a Mac anonymously and upon doing so end up with a file owned by ‘nobody’. 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.

Warning: Has not been tested under Fast User Switching. In fact I only use it with the Mac user logged into the GUI.

(*
Set owner to me
http://strawhousepig.net/

This Folder Action handler is triggered whenever items are added to the attached folder. Can also be run as-is from the AppleScript menuextra.

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.
WARNING: This has not been tested for multiple user activations on a common shared folder!

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. 

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.

If you decide to store your admin password in this script DO NOT share this script without removing your password!

The file is safe (from non-administrators) if stored in ~/Library/Scripts/Folder Action Scripts
*)

-- Set this and it is ready to go. See above for instructions!
property mpassword : "prompt"
property defgroup : ":wheel" -- Anything other than "" (no group alteration) MUST be prefixed with a colon (:)
property defpriv : "755" -- See the 'chmod' man pages for more info.

on adding folder items to this_folder after receiving added_items
  delay 60 --If we continue while the file is open we die. Maybe 'lsof' could be useful here...
  justDOit(added_items)
end adding folder items to

on run
  tell application "Finder" to set _here to (choose folder) as list
  justDOit(_here)
end run

on justDOit(_these)
  try
    set whoiam to (do shell script "whoami")
    if mpassword is "prompt" then
      set 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)
    end if
    repeat with i in _these
      do shell script "_here=" & quoted form of POSIX path of i & ";chown -R " & whoiam & defgroup & " "$_here";chmod -R " & defpriv & " "$_here"" password mpassword with administrator privileges
    end repeat
  on error theErr
    do shell script "logger 'Folder Actions was unable to set ownership. Error: " & theErr & "'"
  end try
end justDOit
(*
Modified from: add - new item alert

Copyright © 2002–2007 Apple Inc.

You may incorporate this Apple sample code into your program(s) without
restriction.  This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours.  You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes.  If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)

UPDATE 2016-08-25: Changed to use a ‘hidden answer’ dialog to retrieve a password when set to “prompt”.
UPDATE 2016-08-29: Added ‘chmod’ to the ‘do shell script’ command.

Leave a Reply

Your email address will not be published. Required fields are marked *