• 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: Takaaki Naganoya <email@hidden>
  • Date: Mon, 26 Oct 2015 20:39:55 +0900
  • X_v_e_cd: c109a254d0974189d056944da79e43a8
  • X_v_r_cd: df1f817766b3347f65c77550740c4899

2015/10/26 19:50、Shane Stanley <email@hidden> のメール:
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

I selected the former way and found it works very well :-)
Thanks Shane!

--
Takaaki Naganoya
Piyomaru Software
http://piyocast.com/as
email@hidden


 _______________________________________________
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: Shane Stanley <email@hidden>
References: 
 >NSFileManager problem (From: Takaaki Naganoya <email@hidden>)
 >Re: NSFileManager problem (From: Shane Stanley <email@hidden>)
 >Re: NSFileManager problem (From: Shane Stanley <email@hidden>)

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