1

I deal a lot with Lotus Notes in my company. I wrote a great application in C# to copy specific files to and from the user's Lotus Notes directory. Now I am wanting to write that application for OSX in Objective C. I have a handful of different files that need to be copied from ~/Library/Application Support/Lotus Notes Data/.

I am running into admin issues when I run a test to copy a single file. What is the best/easiest way(I am a beginner) to prompt the user for admin credentials and execute the file copy code with the newly acquired rights?

I did try implementing the BLAuthentication Class I found online, but it would not compile. I currently don't have access to my work computer to post the code.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dr-ZoidBerg
  • 41
  • 3
  • 10

2 Answers2

0

Try like this below:-

NSString *path=[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)lastObject];
NSString *testUrl =[path stringByAppendingPathComponent:@"/LotusNotesData/source.rtf"];
//
if ([[NSFileManager defaultManager]fileExistsAtPath:testUrl]) {
    NSLog(@"yes");
}
//Below destination is folder name which should be exist on your machine or else you can create programmatically as well
NSString *testUrl2 = @"/Users/home/Documents/destination";

NSLog(@"%@",testUrl);
NSLog(@"%@",testUrl2);
NSError *err=nil;

//Now we are copying the souce path to destination folder with appending file name (it can be any your name becuase file manager copy source file contents to your destination file contents)
//Here given file name is a source.rtf where you can give any your name. Also this is for copying source contents to destination contents

NSFileManager *fm=[NSFileManager defaultManager];
if ([fm copyItemAtPath:testUrl toPath:[testUrl2 stringByAppendingPathComponent:@"source.rtf"] error:&err])
{
    NSLog(@"success");
}
else
{
    NSLog(@"%@",[err localizedDescription]);
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • Also if want to enumerate the multiple files then follow this link http://stackoverflow.com/questions/18683345/cycle-through-a-directory-and-get-paths-of-all-the-files-and-folders/18688333#18688333 – Hussain Shabbir Oct 25 '13 at 04:45
  • Hussain, that worked perfectly. Thank you very much! Can you explain how this is able to copy from /Library/ vs CopyItemAtPath which fails? – Dr-ZoidBerg Oct 25 '13 at 14:25
  • I did not get your question?? you wanted to know how copyItemAtPath works right??? – Hussain Shabbir Oct 25 '13 at 14:27
  • Somewhat, as I tried CopyItemAtPath before and it would always fail because I did not have rights to copy files at the users /Library. I was just curious on how your bit of code bypasses that. – Dr-ZoidBerg Oct 25 '13 at 16:05
  • See copyItemAtPath will just copy the items of file at path, it means your source path should be accurate and when you copy to destination path you have to append the file with destination path. Then only it will copy. In your case might be the destination path was not proper. – Hussain Shabbir Oct 25 '13 at 16:28
0

Use Apple Script to copy files with privilege access.

do shell script "cp source_path destination_path" with administrator privileges

where source path is path of file to be copied.

You may call the Apple script by adding a ".scpt" file with above script in your bundle and using code below:

- (void) runEmbeddedScriptFile: (NSString*)fileName
{
    NSString* path = [[NSBundle bundleForClass:[self class]] pathForResource:fileName ofType:@"scpt"];
    NSURL* url = [NSURL fileURLWithPath:path];
    NSDictionary* errors = [NSDictionary dictionary];
    NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
    [appleScript executeAndReturnError:nil];
    [appleScript release];
}
Neha
  • 1,751
  • 14
  • 36
  • This will prompt a password request from user. – Neha Oct 25 '13 at 09:25
  • script is working in script editer but your code does not run script . – princ___y Jun 04 '16 at 07:42
  • script is working in script editer but your code does not run script . because URL is changed see path = /Users/priyadarshanjoshi/Library/Developer/Xcode/DerivedData/PMB-cxezmjfqztaqoyaedjsgubffmbso/Build/Products/Debug/PMB.app/Contents/Resources/CopyAppleScript.scpt and after conversin in URL is = file:///Users/priyadarshanjoshi/Library/Developer/Xcode/DerivedData/PMB-cxezmjfqztaqoyaedjsgubffmbso/Build/Products/Debug/PMB.app/Contents/Resources/CopyAppleScript.scpt but after execution of script file not copied and NO any error shown. – princ___y Jun 04 '16 at 07:48