Economy-x-Talk - Sample Scripts
Change Permissions
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.
See http://www.macosxhints.com/dlfiles/permissions_change_scpt.txt for the original
property excluded_users : {"Deleted Users"}
-- Feel free to add any user you want excluded. e.g {"Deleted Users", "Admin","Shared","Steve"}
-- Remember this only applies to users that have their home folder in "startup disk:Users:"
-- note: "Deleted Users" is where users deleted from the accounts prefpane go when archived.
-- Get folders in the "Users" folder
tell application "Finder"
set these_folders to folders of folder "Users" of startup disk
end tell
-- Add 'Admin' item if not in excluded_users
if "Admin" is in excluded_users then
set these_names to {}
else
set these_names to {"Admin"}
end if
-- Get the names of the folders for use in our list
repeat with this_folder in these_folders
set this_name to name of this_folder
-- Excluded the names in excluded_users
if this_name is not in excluded_users then
set end of these_names to this_name
end if
end repeat
-- Choose from list of names
set current_user to choose from list these_names with prompt "Choose a UserÉ"
if current_user is false then -- User canceled
beep
else -- User chose a name
-- Make current_user a string instead of a list item sice it is easier to read. {"Admin"} > "Admin".
-- Not set earlier because there might be a user named 'false'.
set current_user to current_user as string
if current_user is "Admin" then
-- Set default location to the startup disk not '/private/var/root/' for obvious reasons
-- Can be set to Library folder: replace with 'path to (library folder)'
set default_location to path to startup disk
else
-- Get path to the chosen user's folder for use in choose folder's default location
set default_location to (path to users folder) & current_user as string as alias
end if
set choose_format to (choose from list {"File", "Folder"} with prompt "Choose File or Folder") as string
if choose_format is "false" then -- User canceled
beep
else
if choose_format is "File" then
-- Choose files(s) to change permissions to chosen user
set item_selection to (choose file with prompt "Choose file(s) to change permissions to " & quoted form of current_user & " with:" default location default_location with multiple selections allowed)
else -- if choose_format is "Folder" then
-- Choose folder(s) to change permissions to chosen user
set item_selection to (choose folder with prompt "Choose folder(s) to change permissions to " & quoted form of current_user & " with:" default location default_location with multiple selections allowed)
end if
if current_user is "Admin" then
-- Change group of folder to admin
set shell_script_chown to "sudo chown -R root:admin "
-- Change mode of folder to rwxrwxr-x
set shell_script_chmod to "sudo chmod -R 775 "
else if current_user is "Shared" then
-- Change owner and group of folder to root and wheel
set shell_script_chown to "sudo chown -R root:wheel "
-- Change mode of folder to rwxrwxrwx
set shell_script_chmod to "sudo chmod -R -f 777 "
else
-- I use the following since the I want to be able to access my files when I'm in another administrators account.
-- Feel free to post other permission settings that you use.
-- Change owner and group of folder to chosen user and admin
set shell_script_chown to "sudo chown -R " & current_user & ":admin "
-- Change mode of folder to rwxrwxr-x
set shell_script_chmod to "sudo chmod -R 775 "
end if
-- Repeat for every item in item_selection
repeat with i from 1 to (count of item_selection)
-- Format path for use in shell script
set selected_folder to quoted form of POSIX path of (item i of item_selection)
-- Perform both shell scripts
repeat with this_shell_script in {shell_script_chown, shell_script_chmod}
try
do shell script this_shell_script & selected_folder as string
display dialog ("item " & i & " of " & (count of item_selection) & return & this_shell_script & selected_folder as string) giving up after 1
on error error_message number error_number
-- If error occurs beep user and bring up why there was an error
beep
display dialog ("Error number: " & error_number & return & error_message as string) giving up after 5
end try
end repeat
end repeat
beep 2
display dialog "Permission fixup complete." buttons "OK" default button 1 giving up after 1
end if
end if
Keep this extensive resource of tutorials and samples on-line! Support RunRev.info!
| Yes, this script is useful. I make a donation and choose freely how much I want to donate on-line (PayPal). Click here if you want to make a donation in dollars. | This script has not (completely) solved my problem and I want Economy-x-Talk to take care of it. Let me contact you. |
Copyright©2002-2008 by Mark Schonewille, Nijmegen, the Netherlands. This web site is in no way affiliated with Runtime Revolution Ltd., but we do recommend the Revolution development platform.