Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mbrowne

macrumors newbie
Original poster
May 23, 2009
8
0
UPDATE: Works in Lion and Mountain Lion now too; see posts below.
Latest version maintained here: https://github.com/mbrowne/change-owner-osx-service

Using Snow Leopard's new Services feature, I created a service that allows me to change the ownership of files and folders from the Finder.

It lets you specify the owner (and also the group if you wish), and does the Terminal command for you (sudo chown....).

You can download the service here:
http://matthewhbrowne.com/software/change_owner_service.zip

Or if you'd rather do it yourself so you know how it works, here is the code:

Code:
on run {input, parameters}
	set newOwner to (short user name of (system info))
	tell application "Finder" to display dialog "New owner/group
(Syntax: \"newowner\" or \"newowner:newgroup\")" default answer newOwner buttons {"Cancel", "OK"} default button 2
	set newOwner to text returned of the result
	
	activate --set the focus back on this Applescript so that when the authenticate box pops up it will have the focus
	repeat with theFile in the input
		set filePath to posixPath(theFile as text)
		do shell script "sudo chown " & newOwner & " " & filePath with administrator privileges
	end repeat
end run

--posixPath function by James Sorenson
--Source: http://hints.macworld.com/article.php?story=20011030193449870
on posixPath(mac_path)
	set mac_path to (mac_path as text)
	set root to (offset of ":" in mac_path)
	set rootdisk to (characters 1 thru (root - 1) of mac_path)
	tell application "Finder"
		if (disk (rootdisk as string) is the startup disk) then
			set unixpath to "/" & (characters (root + 1) thru end of mac_path)
		else
			set unixpath to "/Volumes:" & mac_path
		end if
	end tell
	set chars to every character of unixpath
	repeat with i from 2 to length of chars
		if item i of chars as text is equal to "/" then
			set item i of chars to ":"
		else if item i of chars as text is equal to ":" then
			set item i of chars to "/"
		else if item i of chars as text is equal to "'" then
			set item i of chars to "\\'"
		else if item i of chars as text is equal to "\"" then
			set item i of chars to "\\" & "\""
		else if item i of chars as text is equal to "*" then
			set item i of chars to "\\*"
		else if item i of chars as text is equal to "?" then
			set item i of chars to "\\?"
		else if item i of chars as text is equal to " " then
			set item i of chars to "\\ "
		else if item i of chars as text is equal to "\\" then
			set item i of chars to "\\\\"
		end if
	end repeat
	return every item of chars as string
end posixPath

And this is how you would set it up in Automator:

change_owner_service.jpg


I hope this is helpful to somebody.

Also (this is unrelated), I created a service to generate HTML IMG tags for selected image files in the Finder. I tried to put it on automatoractions.com but the submission didn't take for some reason, so I've given up on that site.

I posted info on it here: http://macscripter.net/viewtopic.php?id=33267

Snow Leopard services are very cool, and if anyone knows someplace where they should be submitted so we can share and benefit from each other's services, let me know. It's too bad that it's so hard to get a submission in to automatoractions.com.
 
Last edited:
Can you specify what this will do in simpler terms? I don't use terminal or automator, and I don't know exactly how this works and what it will do to my system. How do I use it?
 
If you don't use the Terminal or do any sort of programming, changing ownership of files isn't something that you'd need to do that often... although, for example, if another user created a file and you were having trouble editing it, the service I wrote would allow you to easily take ownership of the file so that you could edit it.

In the Finder, when you do "Get Info" on an item, there's a section called "Sharing and Permissions." You can change the permissions but you can't change the ownership from there (although you can see it -- the "Name" column shows the owner [listed first], then the group [listed second]).

The easiest way to try the service I created would be to download the zip file I linked to and copy it to /Library/Services (or to [your home folder]/Library/Services).

Then, after logging out and back in (or restarting), whenever you right-click on files or folders, you will have a "Change Owner" option (probably in the "Services" submenu).

The more useful thing to understand is the overall Services infrastructure for Snow Leopard, which was completely overhauled from previous versions of Mac OS X and is much more flexible and powerful.

It is now possible to add all kinds of custom features to Finder, iTunes, and many other programs just using Automator (often not even requiring any "real" programming).

For a layman's guide to how to create your own Services, there's a great video that features one of Apple's programmers explaining how it works:

http://pixelcorps.cachefly.net/mbkv_235_540p_h264.mov
 
Necro'ing this thread :)

@mbrowne / OP
Any update to this for Lion? Seeing a few issues:

- doesn't set permissions; services icon just spins in menu bar (even after asking for auth).
- only sometimes doesn't ask for authentication; the auth dialog is not hidden, it's just not there at all.
- doesn't support more than 1 file at a time :(

Adding, OSX is a multi-user environment, so this is useful to more than just programmers (though useful for us programmers too!) :)
 
Last edited:
After a minor update to the applescript (using latest techniques for same result) and rebooting, all is well (probably was the reboot that fixed it haha).

Anyway, here is the updated applescript, if interested:
Code:
on run {input, parameters}
	set newOwner to (short user name of (system info))
	tell application "Finder" to display dialog "New owner/group
(Syntax: \"newowner\" or \"newowner:newgroup\")" default answer newOwner buttons {"Cancel", "OK"} default button 2
	set newOwner to text returned of the result
	
	activate --set the focus back on this Applescript so that when the authenticate box pops up it will have the focus
	repeat with theFile in the input
		set filePath to theFile as text
		do shell script "chown " & newOwner & " " & quoted form of POSIX path of filePath with administrator privileges
	end repeat
end run

Changes:
  1. manual posix conversion removed as it is is no longer needed, now using "POSIX path of" to get the same.
  2. added ""quoted form of" to properly encapsulate funky characters, spaces, etc. in Posix path.
  3. removed "sudo" as we're using "with administrator privileges" as using both is a no-no / security hole.
If wanting more detail, there are Apple Tech Notes on the above changes ( google for them)!
 
Thanks for your updates, Casemon! I actually hadn't been using this as much lately, so I hadn't updated it for Lion - I believe your changes were indeed necessary to make it work. And I'm glad you were the one to update it, since you seem more up-to-date on the latest changes to Applescript.

I made a new download available with your updated version:
https://github.com/mbrowne/change-owner-osx-service/archive/master.zip

Or if you just want to browse the source (currently the same as what Casemon just posted):
https://github.com/mbrowne/change-owner-osx-service
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.