Gaining privileges to write a file to \ Library \ ColorSync \ Profiles \ in a mac application

In my Mac application, I need to write the file to \ Library \ ColorSync \ Profiles. For this, the application needs administrator privileges to read the write to the folder. Is it possible to get the same in the application? Any help would be appreciated.

I can open the dialog with the following code snippet

NSSavePanel *tvarNSSavePanelObj = [NSSavePanel savePanel]; [tvarNSSavePanelObj setDirectoryURL:[NSURL URLWithString:@"/Library/ColorSync/Profiles"]]; __block NSString *filePathexn = nil; [tvarNSSavePanelObj beginSheetModalForWindow:[NSApplication sharedApplication].mainWindow completionHandler:^(NSInteger tvarInt) { if(tvarInt == NSModalResponseOK){ NSURL* tvarUrl = [tvarNSSavePanelObj URL]; NSLog(@"doSaveAs filename = %@",[tvarUrl path]); NSString *filePath = [tvarUrl path]; filePathexn = [filePath stringByAppendingPathExtension:@"rtf"]; OSStatus status; if(![[NSFileManager defaultManager]isWritableFileAtPath:filePath]){ NSLog(@"Not Writable at path"); AuthorizationRef authRef; AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0}; AuthorizationRights rights = {1, &right}; status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagPreAuthorize, &authRef); AuthorizationFlags authFlags = kAuthorizationFlagDefaults | kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize; status = AuthorizationCopyRights(authRef, &rights, kAuthorizationEmptyEnvironment, authFlags, NULL); } BOOL status1 = [[NSFileManager defaultManager]createFileAtPath:filePathexn contents:nil attributes:nil]; } else if(tvarInt == NSModalResponseCancel) { return; } else { NSLog(@"doSave As tvarInt not equal 1 or zero = %3ld",(long)tvarInt); return; } }]; 

I need to know how to write a file. The file is not written to the path. Is any tool name required? Will it be possible with SMJobless ()? Please advise a solution!

+8
objective-c file-permissions macos
source share
2 answers

Another solution that I post here. I looked through the application distribution guide. It is mentioned that APIS authorization is not recommended for an application that is distributed on the side of the application store. In any case, this solution works for me, it’s a little hack, I don’t know. I am trying to run an apple script application that gives full editing for the folder (path chmod 777) and after completing my task it will return to the previous set of privileges.

 - (void)runapplescript{ NSDictionary* errorDict; NSAppleEventDescriptor* returnDescriptor = NULL; NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource: @"do shell script \"chmod 777 /Library/ColorSync/Profiles\" with administrator privileges"]; returnDescriptor = [scriptObject executeAndReturnError: &errorDict]; } 
0
source share

I can offer two approaches. One approach is quick but not recommended. The second approach is modern, but requires a bit of information for coding and reading:

  • You need to write a bash script that will do what you need and execute it with privileges that will be granted with the following: STPrivilegedTask
  • Record a privileged helper tool that will be installed once upon first access with root privileges, and you can perform any privileged operation How to create a privileged helper tool
+1
source share

All Articles