Thursday, February 25, 2021

#1 2020-12-14 08:57:17 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

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. smile

Here is the code.

Applescript:

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:

Last edited by Fredrik71 (2020-12-14 08:59:21 pm)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#2 2020-12-14 11:57:14 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6565

Re: Looking for explanations how this method works.

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2020-12-15 09:15:02 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

Re: Looking for explanations how this method works.

Shane Stanley wrote:

Probably 99% of the time it's used for atomic replacement.


This was what google also told me... (in my world its new) smile

Shane Stanley wrote:

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


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.


The purpose to study someone else art is not to add, its to make less more.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)