using chown recursivly on top level folder

Hello,
I am a newbie to applescript, just putting it out there so everyone knows. I have a folder structure of /users/user/desktop/hot folders/“and then about 75 sub folders within sub folders within sub folders from here”. The following script works as long as I give an absolute path (e.g /Users/user/Desktop/Hot Folders/Roland/3600_xp_3200fast) in the do shell script section and then apply the script to the folder roland.
What I need to accomplish is one of two things: use a variable in place of the absolute path (this way I can use one script and apply it too all folders that need it) or even better would be to run the script on folder “hot folders” and alter the script so anytime any file is added to any folder under the folder hot folder the chown command runs on the newly added item. Can anyone help me please? This script is a mixture of one’s I’ve found online.

on adding folder items to this_folder after receiving added_items
tell application “Finder”
set fold_name to the name of this_folder
try
repeat with i from 1 to number of items in added_items
set new_item to item i of added_items
set the item_path to the quoted form of the POSIX path of new_item
do shell script "chown -R newowner /Users/user/Desktop/Hot Folders/Roland/3600_xp_3200fast user name “user” password “password” with administrator privileges
end repeat
on error error_message number error_number
if the error_number is not -128 then
tell application “Finder”
activate
display dialog error_message buttons {“Cancel”} default button 1 giving up after 120
end tell
end if
end try
end tell
end adding folder items to

Hi,

easiest solution, the owner of every added item to the hot folder will be changed


on adding folder items to this_folder after receiving added_items
	repeat with oneItem in added_items
		do shell script "/usr/sbin/chown -R newowner " & quoted form of POSIX path of oneItem user name "user" password "password" with administrator privileges
	end repeat
end adding folder items to

what about the 75+ subfolders? People will add files to various folders under hot folder. how does your script get triggered if it’s applied to Hot folder and a file gets added to a subfolder of a subfolder of hot folder: directory tree example below:

~/hot folder
/–>roland
/------>test folder
/–>epson
/------->test2 folder
/–>HP
/-------->test3 folder

Test, test2 and test3 are all sub folders of subfolders of hot folder. In this example those 3 folders are the folders people will be adding files too. I have about another 70+ folders just like that. If someone adds a file to test3 folder how does the folder action on “hot folder” get triggered and applied to the file added to test3 folder? Just to clarify “hot folder” is the actual name of a folder not an adjective used to describe a folder.

Hello Stefan you beat me to it! :slight_smile:

Can someone please tell me why I am getting the error number −50 “confirmation failed” on this one?


on adding folder items to this_folder after receiving added_items
	tell application "System Events" to set this_user to (name of (current user))
	--	set this_user to quoted form of this_user
	set the_password to set the_password to the text returned of (display dialog "Hello " & this_user & " Enter your password" default answer "             " with hidden answer)
		repeat with i from 1 to number of items in added_items
			set new_item to item i of added_items
			set the item_path to the quoted form of the POSIX path of new_item
			try
				do shell script "sudo chown " & this_user & " " & item_path user name this_user password the_password with administrator privileges
			on error e number n
				tell me
					activate
					display dialog e & " : " & n
				end tell
			end try
		end repeat
end adding folder items to

@ rmckolay : You will have to install it for each and every subfolder. I think you could however use a launchd processes with a watchpath variable together with a folder traversal routine, to minimize the number of folder actions involved, I do not know what will be the ideal solution for you. I have a folder action that installs itself and child and parent folder actions if you are interested.

@rmckolay
the folder action script changes the owner of all added items recursively, that means it affects all folders and subfolders

@McUsr
don’t use sudo in conjunction with administrator privileges. See
Technical Note TN2065: do shell script in AppleScript

Hello Stefan
:smiley: I’d hope to avoid that. :slight_smile:

I’ll read it for me! :slight_smile:

@Stefan,
I copied the script you provided into a new apple script, compiled it, saved it and applied it via folder actions to a folder named test. I copied a file into the folder named test and nothing happened. No owner change.

It should work. Have you saved the script in (~)/Library/Scripts/Folder Action Scripts/ ?

yes, and i have added the appropriate username and password, where would error capture code go?

Hello Stefan.

@rmckolay:

Have you actually tried your chmod statements from the command line?

You could use a construct like this to get the error code returned.

set theRes to do shell script "chown rest of statments in chmod command ; echo $?"

Then you can read out the error code from theRes.

@ Stefan:
I had forgot the text returned of in the dialog for getting the password, so I got the button and the whole shebang into the password.

Thanks for your help anyway. I entered it just right now into my previous post.

Edit Corrected the command to the correct chown and not chmod.

the parameters user name and password must be of an admin account

Your script will fail anyway, if the current user has no admin status, adminstrator privileges requires the access data of an admin account

PS:

We’ re talking about chown :wink:

Hello.

I foresee that rmkcolay has a little dcl job to do, with setting up an administrative group which has the necessary rights within that subtree, or that everybody has the necessary rights.

It is a small deal with a recursive routine and some administrative rights.

I think that among the alternatives are creating a script with the setUid bit set.

An other is to add every user to the sudoers file but you wouldn’t do that.

the OP want’s just to change the owner of a specified user.
It’s a bit “oversized” to consider all possible cases :wink:

@Stefan
correction, it works on the top level folder but not subfolders.

@McUsr,
you had posted
You will have to install it for each and every subfolder. I think you could however use a launchd processes with a watchpath variable together with a folder traversal routine, to minimize the number of folder actions involved, I do not know what will be the ideal solution for you. I have a folder action that installs itself and child and parent folder actions if you are interested

can you elaborate?

i have made sure that all subfolders are owned by the admin account i am using as the owner of the new file (in the chmod cmd)

I’ve tested it by creating a test folder with a bunch of subfolders in it.
After dragging this folder into the hot folder the owner of all files and folders has changed

that makes sense because all the sub folders would be considered new items to the hot folder sense they were never in there. That’s not quite my case. i have static folders with a path like…
~/Hot Folders/Folder1/Folder2/
~/Hot Folders/Folder3/Folder4/
~/Hot Folders/Folder5/Folder6/
~/Hot Folders/Folder7/Folder8/
~/Hot Folders/Folder9/Folder10/
~/Hot Folders/Folder11/Folder12/
~/Hot Folders/Folder13/Folder14/
~/Hot Folders/Folder15/Folder16/
~/Hot Folders/Folder17/Folder18/

these are permanent paths that never change and there are a lot of them. if i drag anything into any of the even number folders i want the chmod script to run. The only way that your script seems to do that is if I apply it to the even number folders. Make sense? Sorry if i’m not explaining myself well enough or missing something you’re trying to explain to me.

Sorry, I think I missed something :wink:

attached folder actions affect only the specified folder, no subfolders.
But you can attach one script to several folders

OK, that’s what I thought. Thank you very much. :slight_smile:

@McUsr:
When you have time can you elaborate on whether your folder traversal and/or and child and parent folder actions you mentioned would help here please?