Where to put the code to drag to Finder an image ?

Until minutes ago, I have set the code to drag a Canvas Backdrop to macOS Finder. But, I have occasional bugs with that:

a. the image file is created elsewhere, sometimes (not in the open folder), *

b. the image file still exists in the Temporary folder (these saw the light after a reboot in the Trash.

Supportable “bugs” (call them features, please), but I wanted to get rid of them.

So, after the drag, I added a TmpFI.Delete line: the image file is created (silently), but deleted in the target folder :wink:
Then I add this if block:

[code] If TmpFI.Exists Then
// Delete the newly created Temporary file
TmpFI.Delete

// Display the Temporary folder contents
SpecialFolder.Temporary.Launch

End If[/code]

TmpFI is a FolderItem that refers to macOS TemporaryFolder.

At the first run, I saw the file created in the TemporaryFolder before I release the mouse, but at my target folder, it is also deleted.

When I put my code into the DragExit Event, no drag at all.
I tried MouseDrag (that sounded good), and it seems to be the correct location. (Of course, in MouseDown, I added Return True.)
EDIT: Wrong, the files are deleted from my target location AND from the Temporary folder. So, I commented out the If block above to keep the generated image file. :frowning:

Is this the correct event to do that ?

  • macOS have some preferred folders and I think the file is created in one of them (I got the file in a folder that was not open at the drop time, once a long ago). Sometimes, the folder just behind the target folder (who is also the Parent of the target folder) scrolls to the bottom and stay there even if I move its contents vertically. This stops when I close it and reopen it.

I cant speak for anyone else, but I do not understand what you are trying to say or ask.

You want to be able to drag a picture and allow it to be dropped in the finder?
You want a picture to be created in the location where you drop it?
What code do you use to create the dragitem?
Why do you delete something?

In OSX, your dragitem has a .destination property… this is where to save your file, is it not?

Canvas.Bacdrop have an image
User drag the Image from the Canvas into the Finder / Explorer
Works fine on macOS and Windows,

On macOS, I got a second copy in the Temporary folder (and that is the file I want to delete).

Used code:

[code] // Drag outside this application (to a web browser ?)
Dim ExportDI As DragItem
Dim TmpFI As FolderItem // Added on [2017-01-02]
Dim SQLDate As New Date // Added on [2017-01-02]
Dim SuffixName As String

// Anti crash test
If Me.Backdrop = Nil Then Return

If Self.Focus <> Nil And (Self.Focus.Name = Me.Name) Then
// Clears the Focus
Self.Focus = Nil
Else
// Setthe Focus to … Me !
Me.SetFocus
End If

// To set the file name unique
SuffixName = ReplaceAll(SQLDate.SQLDateTime,":","-") // Added on [2017-01-02]

// Save the image into Temporary
TmpFI = SpecialFolder.Temporary.Child("Windows 10 Custom Icon - " + SuffixName + “.png”) // Added on [2017-01-02]

// Save the image in the Temporary Folder (Directory)
Me.Backdrop.Save(TmpFI,Picture.SaveAsPNG) // Added on [2017-01-02]

// Do the Drag
ExportDI = New DragItem(Self, Me.Left, Me.Top, Me.Width, Me.Height)
ExportDI.Picture = Me.Backdrop
ExportDI.DragPicture = Me.Backdrop
ExportDI.FolderItem = TmpFI // Added on [2017-01-02]
ExportDI.Drag // Allow the drag

// [2017-03-04] Clears the DragItem
ExportDI = Nil

// [2017-03-04] Delete the Temporary file
// If TmpFI.Exists Then
// // Delete the newly created Temporary file
// TmpFI.Delete
//
// // Display the Temporary folder contents
// SpecialFolder.Temporary.Launch
// End If[/code]
Commented code after the line ExportDI = Nil: added today, I want to delete the copy of the dropped image, but the image at the dropped location is deleted at the same time.

Sorry to not be clear enough.

PS: the code above works fine if in the MouseDown Event.

You can download a previous version of the application here (macOS and Windows 10 versions).

There is nothing.

https://www.mediafire.com/?2iozi0eip5alm

Replace the # with a ?. Sorry for the error.

Edit:

is the link from a recognized user, ? is for general use.

Okay, so here’s the issue. When you give the Finder a file as a dragitem, by default it moves it to the dropped location, hence when you delete the temporary file, you’re actually deleting the dropped file as well.

There’s 3 things I can think of.

  1. Once the drag has happened, recreate the folderitem and point it to where the temporary file used to be; if it exists then the drop failed. If it’s no longer at the original location, it’s been accepted.

Dim tempLocation as string = TmpFi.nativePath // --- Do Drag ExportDi.Drag Dim checkFile as folderitem = getFolderItem( tempLocation, folderitem.pathTypeNative ) if checkFile.exists then // --- Drag failed checkFile.delete end if
I have not tested this code, just wrote it to give you an example.

  1. Add a picture to the drag item instead of the file and let the macOS create a picture clipping for you.

  2. Use a promised drop (read the documentation about this), basically you tell the Finder that you promise to give it the file, without giving it the file. When the drag completes you check the .destination property, if there’s a folder there, you create the file there. If not, you move on.

Thank you Sam.

Here is the Apple point of view for this case:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DragandDrop/DragandDrop.html