Drag and drop for audio files

here is the goal:

drag a sound file onto a button.
the button displays the file name
click the button to play the audio file
will need to play wav, aif, mp3, aac files.

Where is the file type editor?

I looked at the docs but it doesn’t make sense to me.

[Dim](file:///Dim) jpegType [As](file:///As) [New](file:///New) FileType
jpegType.Name = “image/jpeg”
jpegType.MacType = “JPEG”
jpegType.MacCreator = “prvw”
jpegType.Extensions = “jpg;jpeg”

[Dim](file:///Dim) pngType [As](file:///As) [New](file:///New) FileType
pngType.Name = “image/png”
pngType.MacType = "PNG "
pngType.MacCreator = “ogle”
pngType.Extensions = “png”

[Dim](file:///Dim) f [As](file:///As) FolderItem

// Use the addition and conversion operators to combine the file types
f = GetOpenFolderItem(jpegType + pngType)

This is Greek to me.

This example loads a sound file called “hello.mp3” from the current directory (folder) into a Sound object and plays it.

Dim f As FolderItem
f = GetFolderItem(“hello.mp3”)

If f <> Nil And f.Exists Then
Dim s As Sound
s = f.OpenAsSound
s.Play
End If

Your example loads a “hello.mp3” file in a known, expected location, so it won’t work for drag&drop and does not need file types at all.

The code with “dim as filetype” is meant to be in a method/event, not global.

In the IDE, under the “Insert” menu, look for the item “File Type Group”. This will add an item to your project. Fill the properties you want to that object, including its name.

In code, use something like this:
MyPushButton.AcceptFileDrop(MyFileTypeGroup.DocumentName)

Handle the dropped item(s) in the Drop event.

I need so much more help. I am NOT a coder. I know Applescript on the Mac, and AutoHotKey on PC. I own XOJO but cannot get it to do 1 thing. The manual is useless to me. I have to beg someone to help me.

Sorry but if you can’t tell me step by step, this is probably not going to happen. The light bulb is not going off. Can someone help me.

When I click a button it plays a sound. How hard can it be. 2 Lines of code in Applescript or AHK. Thanks. Sorry for the rant and the tone.

I guess a step by step guide isn’t going to really help you understanding the concept.

I gave you answers for specific questions you had; for instance:

Have you tried that?
If yes, why/where are you stuck now? What doesn’t work?

Basically, you have to make these steps:
• Add a file type holding the type of files you want to accept (not in code, but using the File Type editor).
• Make your button accepting these files (not a great design, I’d say, but if it’s just for learning, that’s ok). For this, use MyButton.AcceptFileDropt(“My file type”).
• In the button’s DropObject event, find the dropped file(s), open its sound, and play it.

Xojo does not do things automatically as you’d expect (and that’s actually nice). It does what you ask, but you have to go deeper than how AppleScript works (which just uses other existing applications to do its work instead).

Shawn,

I’m inserting Xojo comments into the code, explaining what each line does, but this is all stuff you can get from the user guide and the language reference.

// allocate a new file object instance
Dim f As FolderItem

// assign the file named "hello.mp3" to it
// the file would have to exist in the same
// folder as your Xojo app
f = GetFolderItem(“hello.mp3”)

// make sure the file object was created
// and that it exists
If f <> Nil And f.Exists Then

// create an instance of the sound object
Dim s As Sound

// open the file instance as a sound and
// put it into the sound instance
s = f.OpenAsSound

// play the sound
s.Play

// end the If block from above
End If

FWIW, I did a lot of AppleScript programming 20 years ago, but I outgrew its capabilities, so I know where you are coming from.

That said, I understand that you’re frustrated and want this to work now, but you cannot short circuit the learning process of going from AppleScript to an object-oriented language like Xojo. There’s going to be a learning curve. I suggest you start here

To learn the relevant concepts in Xojo and object oriented programming.

I feel it’s also important to point out that programming is not something where someone can give you a step-by-step instruction on how to attack a problem because those instructions will only serve you once or twice in the long run. Sure you can create libraries of code that you call into, but the issue that you are running into right now, that you are new to the language and you can’t read and understand the examples yet is going to cause you (and the people trying to help you) a lot of pain and frustration.

Everyone on this forum is here by choice… including the Xojo employees that you see from time to time like me. But keep in mind that most people have learned that if you don’t help the user learn by discovering instead of just giving them the code means that when they run into the next problem, they’ll be back for another “code snippet”. What this leads to is an app that’s written by the community, that you don’t understand and if you have any bugs, you have to go back to the forum for debugging help.

4 Likes