Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to modify user permissions System
Here is an easy way for changing permissions. This is useful when files are constantly being swapped around between users, or you want to access your files while logged in another administrator's account, and vice versa. Copy and paste this script into Script Editor, and save it when done.

This script makes a list of folders in the 'Users' directory of the startup disk for the user to choose from; the 'Shared' folder is included, but can easily be removed by adding it to the excluded_users property at the top of the script. The same goes for the 'Admin' option. I am willing to see improvements on the script as I am sure it could be improved.

[robg adds: I haven't tested this one ... and keep in mind that changing permissions can lead to sometimes unintended consequences, so use some caution...]
    •    
  • Currently 2.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[23,945 views]  

An AppleScript to modify user permissions | 8 comments | Create New Account
Click here to return to the 'An AppleScript to modify user permissions' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to modify user permissions
Authored by: taxi on Mar 11, '05 10:58:56PM

I used to store all of my Music in the shared directory, but found that sometimes various programs (iTunes) would set certain files to be owned by certain users.

I created a 'Shared' user, and added all users to the 'shared' group. Then I set up a crontab entry that automatically made all files in the Users/Shared/Music directory belong to user shared, and have the right permissions.

Something along the lines of:

find /Users/shared/Music -not -user "shared" -exec chown shared:shared {} \;

But I'd need to check it, so don't type this in...without checking it out first.



[ Reply to This | # ]
An AppleScript to modify user permissions
Authored by: taxi on Mar 12, '05 05:59:54AM

The command I use to change owner is:

find ~shared/Music -not -user shared -exec chown shared:admin {} \;

The command to change permissions:

find ~shared/Music -type d -not -perm 775 -exec chmod 775 {} \;



[ Reply to This | # ]
An AppleScript to modify user permissions
Authored by: jakacmar on Mar 12, '05 03:23:43AM
I haven't tested the script but just in reading it over I'm at a loss for how it actually works, or maybe I'm just confused about what it's actually trying to do. Needless to say, the script uses "sudo" in all of the shell scripts it runs but it never gets the password for an admin account so how could "sudo" actually work. Also, last I remember, "sudo" doesn't work at all using AppleScript and "do shell script". Instead you use something like:
 do shell script "whatever" with administrator privileges password "mypassword"
You could of course have a dialog pop up asking for the admin password instead of hard coding it in, but either way, it's needed at some point. Is this only for working on files that you already own? And if so, why use "sudo" at all? I'm definitely confused.

[ Reply to This | # ]
An AppleScript to modify user permissions
Authored by: qwerty denzel on Mar 15, '05 10:09:15PM
I am guessing that using sudo only works for those that have used it before (in Terminal).
do shell script "ls"
and
do shell script "sudo ls"
have different results for me like in the Terminal so I guess it works. On my computer I am of course an administrator, this script is not useful

[ Reply to This | # ]
An AppleScript to modify user permissions
Authored by: qwerty denzel on Mar 16, '05 10:15:19PM
Sorry, I cut myself off there. On my computer I am of course an administrator, this script is probably not useful to those who have a standard user account, since they can only change the permissions of their own files. Here is another script that changes the permissions of all items in your users folder.

property excluded_items : {"Public", "Sites"}

tell application "System Events"
	set currentUser to quoted form of (name of current user as string)
end tell

set this_folder to (path to home folder) as string
tell application "Finder"
	set these_files to every item of folder this_folder
end tell
repeat with i from 1 to the count of these_files
	set this_file to (item i of these_files as alias)
	set this_info to info for this_file
	set this_name to name of this_info
	if this_name is not in excluded_items and alias of this_info is false then
		set this_POSIX to quoted form of POSIX path of this_file
		try
			do shell script "sudo chown -R  " & currentUser & ":" & currentUser & " " & this_POSIX
		end try
		try
			do shell script "sudo chmod -R 775 " & this_POSIX
		end try
	end if
end repeat
Note the first 'do shell script' has:
"sudo chown -R  " & currentUser & ":" & currentUser & " " & this_POSIX
instead of:
"sudo chown -R  " & currentUser & ":admin " & this_POSIX
which is used in the original hint.

[ Reply to This | # ]
Folder Action script to change permissions
Authored by: ddldreaming on Mar 15, '05 10:31:42AM

I was inspired by this hint to address a minor annoyance: files I upload to my website (using scp from the command line or SFTP via Fugu) are assigned the same permissions as the local file, usually 0600. I generally want them to be 0644 (readable by everyone), but sometimes I forget to set them manually. So rather than change my umask, I wrote this Folder Action script and attached it to my 'uploads' folder.


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 ("/bin/chmod -R +r " & item_path)
      end repeat
    end try
  end tell
end adding folder items to

I saved it to ~/Library/Scripts/Folder Action Scripts/ and wrote another script (also saved there) to attach the first script to subfolders of a folder. This one is as follows:


on opening folder this_folder
  tell application "Finder"
    set script_file to ((path to library folder from user domain as Unicode text) ¬
      & "Scripts:Folder Action Scripts:add - Make world-readable.scpt" as Unicode text)
    repeat with each_folder in (get every folder of this_folder)
      tell application "System Events"
        attach action to (each_folder as alias) using (script_file as alias)
      end tell
    end repeat
  end tell
end opening folder

They seem to work pretty well. I'm pretty sure others have posted similar scripts before but what the heck, a few more can't hurt. :) (Can I just take this moment to say how much I despise the error reporting in AppleScript? Yecch. Thank you.)



[ Reply to This | # ]
Folder Action script to change permissions
Authored by: goodidea on Jan 08, '06 11:15:43AM

Hello!
This works pretty well and I am glad to have found it (I not very familiar with applescript).

But: it seems not to work is you *replace" an existing item instead of adding one, or am I wrong?

Is it easy to modify the script that it also works in this case???

Greetings! Patrick!



[ Reply to This | # ]
A simpler, more versatile solution...
Authored by: macfreek57 on Feb 24, '08 08:17:58PM
I know this is old, but I found this and was looking for something a bit simpler. I edited down the original script. Now you can attach it to any folder through Folder Actions, and it will instantly change the ownership on any files added to it. When it executes, it will alert you via dialog box of the folder the script was executed from and the affected files.

property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.

on adding folder items to this_folder after receiving added_items
	
	-- This is the POSIX command to be executed.
	-- You can actually change this to any command you want to perform on the files as long as the format of the command requires a file name at the end.
	set shell_script to "chmod -R -f 777 " -- Don't forget to leave a space before the end quotation mark.
	
	tell application "Finder" to set the folder_path to the POSIX path of this_folder
	set alert_message to "A Folder Action has been executed from the folder " & folder_path & ". The following shell script was performed on the files that were just added to that folder:
	" & shell_script & "

Affected files:"
	
	set i to 0
	repeat with this_item in added_items
		set i to i + 1
		set posix_path to POSIX path of this_item as string
		
		do shell script shell_script & quoted form of posix_path -- Perform command
		
		set file_name to items ((length of folder_path) + 1) thru ((length of posix_path) - 1) of text items of posix_path as string
		set alert_message to alert_message & "
		" & i & ") " & file_name
	end repeat
	
	-- Alert user that permissions have been changed on the added files.
	-- This can be deleted or commented out if desired
	display dialog alert_message with icon 1
	
end adding folder items to
I wanted the files to be read-and-writable to everyone, but you can change this by changing the number "777" to something else. Google around if you don't know what to change it to. You can also use this script to perform any command line action as long as it uses the same format (i.e. command, options, then file name). All you need to do is change the string on the line
set shell_script to "chmod -R -f 777"


[ Reply to This | # ]