I want to implement a handler which copies files of a directory into another, given a “Replace existing Y/N” option.
I plan to do so (pseudo code):
set theListToCopy to getTheListOfSourceFolder(inSourceFolder)
repeat with theFile in theListToCopy
and as moveItemAtPath:toPath:error: requests that its delegate confirms the copy, add this handler:
if gManager's fileExistsAtPath_(inDestFolder&theFile) and replaceCheckbox's intValue() = 0
return 0 -- do not proceed
return 1 -- copy file and eventually replace destination file
May I go for it?
I’m not sure why you need a delegate method to make the decision to move or not. But the fundamental problems is that moveItemAtPath:toPath:error: requires that its destination not yet exist.
You might be better to use moveItemAtPath:toPath:error:, and if the reply is false and your checkbox says go for it, follow up with replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: (or just delete and moveItemAtPath:toPath:error: yourself).
I made a mistake because I want the file to be copied, not mpved (the original remains where it is)
and anyway I can’t get my file copied. What is wrong here:
on importDone_(sender) -- called by the NSOpenPanel sheet
if sender â‰ missing value then
set thePath to POSIX path of (sender as text)
manager's changeCurrentDirectoryPath_(thePath) -- manager is an instance of NSFileManager
-- Copy resources into the destination directory - replace if needed
set theList to manager's contentsOfDirectoryAtPath_error_(thePath,missing value)
repeat with theFile in theList
if (gImportCtrl's iCards as integer = 1) and (theFile's pathExtension()as string = "atxt") then
manager's copyItemAtPath_toPath_error_((thePath&"/"&theFile) as string,gCurrentPath,missing value)
manager's changeCurrentDirectoryPath_(gCurrentPath) -- gCurrentPath is the destination directory
Why are you bothering with changeCurrentDirectoryPath_ for? It’s pretty irrelevant for most Mac apps. And you don’t define gCurrentPath.
No, not in this piece of code, but it’s defined sooner in the program.
You’re right about changing the directory, it was just to avoid concatenating the document’s directory before its files.
But this should not interfere with:
if (gImportCtrl’s iCards as integer = 1) and (theFile’s pathExtension()as string = “atxt”) then
manager’s copyItemAtPath_toPath_error_((thePath&“/”&theFile) as string,gCurrentPath,missing value)
thePath is the source directory;
gImportCtrl’s iCards is an outlet to the “type of files to import” checkbox.
But the toPath_ has to be the full path, including the file name.
Oops, I missed this one:
When a file is being copied, the destination path must end in a filename”there is no implicit adoption of the source filename
. and go on for the famous coercion nightmare:
set srcFile to thePath as string & "/" & theFile as string
set dstFile to gCurrentPath as string & "/" & theFile as string
manager's copyItemAtPath_toPath_error_(srcFile,dstFile,missing value)
Ok, it works – thank you Shane.
For this purpose there are convenient NSString methods
set srcFile to thePath's stringByAppendingPathComponent_(theFile)
set dstFile to gCurrentPath's stringByAppendingPathComponent_(theFile)
I also forgot NSString’s utilities. I need to sleep more.
Thank you Stefan.