• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSFileManager problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSFileManager problem


  • Subject: Re: NSFileManager problem
  • From: Shane Stanley <email@hidden>
  • Date: Mon, 26 Oct 2015 21:50:31 +1100

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

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: NSFileManager problem
      • From: Takaaki Naganoya <email@hidden>
References: 
 >NSFileManager problem (From: Takaaki Naganoya <email@hidden>)
 >Re: NSFileManager problem (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: NSFileManager problem
  • Next by Date: Scripting Bridge Question
  • Previous by thread: Re: NSFileManager problem
  • Next by thread: Re: NSFileManager problem
  • Index(es):
    • Date
    • Thread