FolderItem.LastErrorCode 101

When I save a picture as a PNG graphics file, I got the error: FolderItem.LastErrorCode 101 (File not found)
If I save the picture again and I overwrite the file, the LastErrorCode ist 0.

// CurrentBook.Cover is the picture
Dim dlg As New SaveAsDialog
Dim f As new FolderItem
dlg.InitialDirectory = SpecialFolder.Pictures
dlg.PromptText = “Save AudioBook Cover”
dlg.SuggestedFileName = “AudioBook-” + CurrentBook.id.ToText
dlg.Title = “AudioBook Cover”
dlg.Filter = PictureFileTypes.Png // defined as a file type in FileTypes1 file type set
f = dlg.ShowModal

if f <> nil then
CurrentBook.Cover.Save(f,Picture.SaveAsPNG,100)
if f.LastErrorCode = 0 then
ShowInfoLine("AudioBook Cover was saved: " + f.NativePath)
else
Beep
ShowInfoLine("Fehler beim Speichern: " + f.LastErrorCode.ToText)
end if
else
beep
ShowInfoLine(“Das Speichern wurde abgebrochen!”)
end if

macOS 10.14.2
Xojo 2018R4

I wonder if it’s a timing thing. How large is the PNG you’re trying to write?
It could be not finished writing before you’re asking the filesystem what the status is.
If you don’t do the error test, does the PNG get written out the first time?

Also, just a heads up, the quality parameter only applies to JPEG.
http://documentation.xojo.com/api/graphics/picture.html#picture-save

The picture file is always created, but the LastErrorCode is 101. Picture size is only 492 KB.

Change to:
CurrentBook.Cover.Save(f,Picture.SaveAsPNG)
or
CurrentBook.Cover.Save(f,Picture.SaveAsJPEG,100)

Same LastErrorCode 101 when saving file.

What is inside the CurrentBook.Cover.Save(f,Picture.SaveAsPNG,100) method?

Since the file is always getting created, this is a timing issue / race condition.

Apple systems are getting more and more asynchronous. This is similar to the Open/Save dialog being stuck open if you breakpoint the line after. There is nothing you can really do as we don’t have a SaveComplete event or anything like that. You could use a timer to delay the error check, but that would be ill-advised and not reliable.

I would suggest making a permissions check before trying to write the file. There isn’t much more that can be done.

// Dialog setup

dim fDestination as FolderItem = dlg.ShowModal

if fDestination.IsWritable = false then
  MsgBox(TranslateToGerman("Write Error" + EndOfLine + EndOfLine + "Permission to write in the specified location was denied."))
  return

end

CurrentBook.Cover.Save(fDestination, Picture.SaveAsPNG)
ShowInfoLine("Das Speichern wurde abgebrochen!")