Looking for explanations how this method works.

I have lost the count how many times I have read the ‘reference guide’ of this method.

Could someone explain in words of a child in mind, I’m lost. :slight_smile:

Here is the code.

use framework "Foundation"
use scripting additions

(**
* [Class]: NSFileManager
* 	A convenient interface to the contents of the file system, 
* 	and the primary means of interacting with it.
**
* [Instance Method]: replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error:
* 	Replaces the contents of the item at the specified URL in a manner 
* 	that ensures no data loss occurs.
**
* - (BOOL)replaceItemAtURL:NSURL withItemAtURL:NSURL backupItemName:NSString
* 		options:NSFileManagerItemReplacementOptions resultingItemURL:NSURL
* 		error:NSError
*)

set thePath to POSIX path of (path to desktop) & "123.tiff"
set theDest to POSIX path of (path to desktop) & "123 copy.tiff"
set theBackupName to "123.bak"

my replaceItemAtURL:thePath withItemURL:theDest backupItemName:theBackupName resultingItemURL:theDest

on replaceItemAtURL:_originalItemURL withItemURL:_newItemURL backupItemName:_backupItemName resultingItemURL:_resultingURL
	set theSource to current application's |NSURL|'s fileURLWithPath:_originalItemURL
	set theDestination to current application's |NSURL|'s fileURLWithPath:_newItemURL
	set theBackupName to current application's NSString's stringWithString:_backupItemName
	set theResultingItem to current application's |NSURL|'s fileURLWithPath:_resultingURL
	set filemanager to current application's NSFileManager's defaultManager
	set {theResult, theError} to filemanager's replaceItemAtURL:theSource withItemAtURL:theDestination backupItemName:theBackupName options:0 resultingItemURL:theResultingItem |error|:(reference)
	return {theResult, theError} as list
end replaceItemAtURL:withItemURL:backupItemName:resultingItemURL:

Probably 99% of the time it’s used for atomic replacement of an old version of a file with a new one of the same name: instead of deleting the destination file and moving the source, this does it more safely so that if something goes wrong, the original file is still there.

It’s something apps will use when saving files: they will typically use URLForDirectory:inDomain:appropriateForURL:create:error: with NSItemReplacementDirectory to create a temporary directory, save the new file to it, replace the old version with the new one, then delete the temporary directory. But it’s also just a a more bullet-proof way of moving while replacing in any circumstances.

If you look in my FileManagerLib, you’ll see an example.

This was what google also told me… (in my world its new) :slight_smile:

I will, and you cover most of Class NSFileManager, I guess its cheating to look before trying to
understand and make the case.

Ps. Most of the case when I share code here on macscripter.net is my research to understand
AppleScript, ASObjC and become a better coder. I also know any person who do that kind of
research also discover something new they didn’t know before. If we share that knowledge
someone else could learn from it (if they want to).

Thanks again for your knowledge and also for your approach in life to be able to share it.