Cocoa 在应用程序支持之间复制文件

Cocoa 在应用程序支持之间复制文件,cocoa,admin,file-copying,Cocoa,Admin,File Copying,在我的公司里,我经常与Lotus Notes打交道。我用C编写了一个很好的应用程序,可以在用户的LotusNotes目录中复制特定的文件。现在我想在Objective C中为OSX编写应用程序。我有一些不同的文件需要从~/Library/application Support/Lotus Notes Data/复制 当我运行复制单个文件的测试时,我遇到了管理问题。作为初学者,提示用户输入管理员凭据并使用新获得的权限执行文件复制代码的最佳/最简单方法是什么 我确实尝试过实现我在网上找到的BLAut

在我的公司里,我经常与Lotus Notes打交道。我用C编写了一个很好的应用程序,可以在用户的LotusNotes目录中复制特定的文件。现在我想在Objective C中为OSX编写应用程序。我有一些不同的文件需要从~/Library/application Support/Lotus Notes Data/复制

当我运行复制单个文件的测试时,我遇到了管理问题。作为初学者,提示用户输入管理员凭据并使用新获得的权限执行文件复制代码的最佳/最简单方法是什么

我确实尝试过实现我在网上找到的BLAuthentication类,但它无法编译。我目前无法访问我的工作计算机发布代码

尝试以下方法:-

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]);
}

使用Apple脚本复制具有权限访问的文件

do shell script "cp source_path destination_path" with administrator privileges
其中,源路径是要复制的文件的路径

您可以通过在捆绑包中添加带有上述脚本的.scpt文件并使用以下代码来调用Apple脚本:

- (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];
}

另外,如果你想列举多个文件,然后按照这个链接侯赛因,这是完美的工作。非常感谢你!你能解释一下这是如何从/Library/vs CopyItemAtPath复制失败的吗?我不明白你的问题??您想知道copyItemAtPath是如何工作的吗???有点,因为我以前尝试过copyItemAtPath,但它总是失败,因为我没有在用户/库中复制文件的权限。我只是想知道你的代码是如何绕过这一点的。请看copyItemAtPath将只复制路径中的文件项,这意味着你的源路径应该是准确的,当你复制到目标路径时,你必须用目标路径附加文件。那么只有它会复制。在您的情况下,目标路径可能不正确。这将提示用户发出密码请求。脚本在脚本编辑器中工作,但您的代码不运行脚本。脚本在脚本编辑器中工作,但您的代码不运行脚本。由于URL已更改,请参见path=/Users/priyadarshanjoshi/Library/Developer/Xcode/DerivedData/PMB cxezmjfqtqtoyaedjsgubffmbso/Build/Products/Debug/PMB.app/Contents/Resources/CopyAppleScript.scpt,URL中的转换后为=file:///Users/priyadarshanjoshi/Library/Developer/Xcode/DerivedData/PMB-cxezmjfqztaqoyaedjsgubffmbso/Build/Products/Debug/PMB.app/Contents/Resources/CopyAppleScript.scpt 但执行脚本后,文件未被复制,且未显示任何错误。