On 26 Oct 2015, at 8:01 PM, Takaaki Naganoya <
email@hidden> wrote:
Already file exists in the target folder.
OK, two options. You can just delete the existing file first, or you can replace it. The delete option:
set {theResult, theError} to (defM's moveItemAtURL:fromPath toURL:toPath |error|:(reference))
if not theResult as boolean then
if (toPath's checkResourceIsReachableAndReturnError:(missing value)) as boolean then -- it already exists, so try deleting
defM's removeItemAtURL:toPath |error|:(missing value)
-- try moving again
set {theResult, theError} to (defM's moveItemAtURL:fromPath toURL:toPath |error|:(reference))
end if
end if
The replace option is a bit more complicated, but does it in a way that there is no chance of data loss. So:
set {theResult, theError} to (defM's moveItemAtURL:fromPath toURL:toPath |error|:(reference))
if not theResult as boolean then
if theError's code() = (current application's NSFileWriteFileExistsError) then -- it already exists, so try replacing
set {theResult, theError} to defM's replaceItemAtURL:fromPath withItemAtURL:toPath backupItemName:(missing value) options:(current application's NSFileManagerItemReplacementUsingNewMetadataOnly) resultingItemURL:(missing value) |error|:(reference)
end if
end if