Foltderitem.MoveFileTo(SpecialFolder.Trash) doesn't move the file to trash

I have a method that moves a folder item to the trash. This works without any problem, but when the trash has a folder item with the same name, it doesn’t move the file to the trash. The file is always on the original location. LastErrorCode is 0.

Sub DeleteAttachment(MyDeleteFile as string)
// Check Attachment
dim OutFolder as folderitem
dim WorkDir as FolderItem

if AttachmentFolder.Exists = false then
exit sub
end if

if ItemID < 1 then
exit sub
end if

if ItemID > 0 then
OutFolder = AttachmentFolder.child(str(ItemID))
end if

if OutFolder <> Nil then
if OutFolder.Exists then
if OutFolder.child(MyDeleteFile).Exists = true then
OutFolder.child(MyDeleteFile).MoveFileTo(SpecialFolder.Trash)
MsgBox "Error Code: " + OutFolder.LastErrorCode.ToText
if OutFolder.LastErrorCode <> 0 then
MsgBox “Can not move the attachment to the trash”
end if
ShowInfoLine(replace(langMoveToTrash, “XX”, MyDeleteFile))
end if
end if
end if

End Sub

You should verify the destination and rename if needed by adding the time like the system does.

Note that what you do will not work in a sandboxed app.

MBS has Files.MoveToTrash which takes care of the issue and works in a sandboxed app.

If you can get away with it (I don’t think that osa scripts are allowed in sandboxed apps) you could let the Finder do the heavy lifting of checking for conflicts in the trash and renaming the files appropriately.

dim sh as new shell sh.execute "osascript -e 'tell application ""Finder"" to delete (POSIX file """ + file.NativePath + """ as alias)'"

[quote=259221:@Jared Feder]If you can get away with it (I don’t think that osa scripts are allowed in sandboxed apps) you could let the Finder do the heavy lifting of checking for conflicts in the trash and renaming the files appropriately.

dim sh as new shell sh.execute "osascript -e 'tell application ""Finder"" to delete (POSIX file """ + file.NativePath + """ as alias)'"[/quote]

One thing is for sure, AppleScript gets immediately rejected in the MAS if it touches Finder or System Events.

using MBS Plugin?

than maybe this helps:
http://monkeybreadsoftware.net/faq-howtomoveafileorfoldertotrash.shtml

There a two solutions. Both works in a sandbox app for MAS:

if OutFolder <> Nil then
if OutFolder.Exists then
if OutFolder.child(MyDeleteFile).Exists = true then
if SpecialFolder.Trash.Child(MyDeleteFile).Exists then
SpecialFolder.Trash.Child(MyDeleteFile).Delete
end if

OutFolder.child(MyDeleteFile).MoveFileTo(SpecialFolder.Trash)
if OutFolder.LastErrorCode <> 0 then
MsgBox “Can not move the attachment to the trash”
end if
ShowInfoLine(replace(langMoveToTrash, “XX”, MyDeleteFile))
end if
end if
end if

if OutFolder <> Nil then
if OutFolder.Exists then
if OutFolder.child(MyDeleteFile).Exists = true then
dim r as FolderItem
dim e as integer = MacFileOperationMBS.MoveObjectToTrashSync(OutFolder.child(MyDeleteFile), r, MacFileOperationMBS.kFSFileOperationDefaultOptions)
if e = 0 then
ShowInfoLine(replace(langMoveToTrash, “XX”, MyDeleteFile))
else
MsgBox “Can not move the attachment to the trash”
end if
end if
end if
end if

The second solution rename the moved file from “MyFIle_68218.pdf” “MyFIle_68218 00-04-53-268.pdf”, which will be extended by the current remove time.