Skip navigation.
 
mlRe: Running process as root from Cocoa
FROM : Kyle Sluder
DATE : Tue Jan 29 06:59:51 2008

On Jan 29, 2008 12:07 AM, Mitchell Hashimoto <<email_removed>> wrote:
>        NSString *fullPath = @"/Applications/Program.app/Contents/MacOS/Program";


This seems odd.  Is Program.app a separate application from your own
(e.g. you're building a launcher to an existing, but separate, app)?
If so, use NSWorkspace to get the path to the application you want.
The Most Correct Way(TM) to do this would be -[NSWorkspace
absolutePathForAppBundleWithIdentifier] (which you would pass the
app's bundle identifier, a string in the form
com.yourcompany.AppName), but -[NSWorkspace fullPathForApplication:]
may suit you just fine (in which case you would pass "AppName", that
is the app bundle's name without the extension).

Once you have the path to the bundle, use NSBundle to get the path to
the actual executable inside the bundle and append it to the string
you got above.  This is the path you pass to Authorization Services.

>        AuthorizationItem myItems[1];
>
>        myItems[0].name = kAuthorizationRightExecute;
>        myItems[0].valueLength = [fullPath length];
>        myItems[0].value = [fullPath cStringUsingEncoding:NSASCIIStringEncoding];
>        myItems[0].flags = 0;


Your use of -[NSString length] and -[NSString cStringUsingEncoding:]
is troublesome.  NSString has an instance method specifically for what
you're trying to do: -fileSystemRepresentation.  It returns a C string
that's appropriate to be passed to the filesystem, regardless of funky
Unicode characters in paths.  You can use the C strlen() function to
get the length of a C string (don't use -[NSString length] for this;
it may not match up).

HTH, and remember to use your powers for good, not evil.  Cheaters
just ruin the fun for the rest of us.

--Kyle Sluder

Related mailsAuthorDate
mlRunning process as root from Cocoa Mitchell Hashimoto Jan 28, 18:17
mlRe: Running process as root from Cocoa Hamish Allan Jan 28, 18:21
mlRe: Running process as root from Cocoa Nir Soffer Jan 28, 22:48
mlRe: Running process as root from Cocoa Kyle Sluder Jan 28, 22:55
mlRe: Running process as root from Cocoa Bill Bumgarner Jan 28, 23:01
mlRe: Running process as root from Cocoa Mitchell Hashimoto Jan 29, 06:07
mlRe: Running process as root from Cocoa Kyle Sluder Jan 29, 06:59
mlRe: Running process as root from Cocoa Mitchell Hashimoto Jan 29, 07:03
mlRe: Running process as root from Cocoa Kyle Sluder Jan 29, 07:17
mlRe: Running process as root from Cocoa Mitchell Hashimoto Jan 29, 07:20
mlRe: Running process as root from Cocoa Kyle Sluder Jan 29, 07:27
mlRe: Running process as root from Cocoa Mitchell Hashimoto Jan 29, 07:39
mlRe: Running process as root from Cocoa Chris Suter Jan 29, 07:54
mlRe: Running process as root from Cocoa Torsten Curdt Jan 29, 09:21