Open picture file with mail (to be attachment)

Hello, I would like to open a picture file with mail, to be an attachment. Used to work in the old days. Example below opens the png file in Preview. Suggestions to open it with Mail?

Var fApp As FolderItem
Var fDoc As FolderItem

fApp=SpecialFolder.Applications.child(“Mail.app”)
fDoc=app.theTemp.Child(“picTemp.png”)
thePicTape.Save(fDoc, Picture.Formats.PNG)
fDoc.Open(fApp.NativePath,True)

Manual says: FolderItem.Open (parameters As String = “”, activate As Boolean = True)
parameters is the app’s parameters to be passed to the app being open.

it opens the app for file with this extension
so if folderitem is a .pdf it opens a registered pdf viewer
if folderitem is a .html it open the default browser.

somehow you open a picture and give the paint app the fApp path.

Open command replaced launch. Launch could open a file with an app you choose. It this option gone now?

On macOS, use NSSharingService.
Example project: NSSharingService example

And don’t assume that every macOS user is using Mail.app.
NSSharingService (Apple Developer Documentation) should take care of that, too.

Thanks Jurg, I will try this.
Still I would like to know if Xojo can open a file with a specific app like it used to.

this open Mozilla Thunderbird Mailer with Text File as Attachment

Var a As FolderItem = New FolderItem("C:\Program Files\Mozilla Thunderbird\thunderbird.exe",FolderItem.PathModes.Native)
a.Open(Encodings.UTF8.Chr(34)+"C:\Users\Markus\Desktop\Neues Textdokument.txt"+Encodings.UTF8.Chr(34))

i used “” around because space in path. without “” the space would be a argument separator.

your problem is the mixed up fDoc & fApp
u will open the app with the pic as argument.

i think
fDoc.Open(fApp.NativePath,True)
should be
fApp.Open(fDoc.NativePath,True)

1 Like

I’m still having macOS in mind in this reply…

In theory and according to Xojo’s Documentation this should work (assuming the .app you’re launching is accepting a parameter, which is a shell path to a file that it’s going to open):

fApp.Open(fDoc.ShellPath, True)

If that doesn’t work, another option would be to use a Shell (same assumption as above regarding parameters):

Dim s As New Shell
s.Execute("open -n " + fApp.ShellPath + " " + fDoc.ShellPath)

Having said that, just another thing to keep in mind:

That most likely won’t work on the latest macOS Versions. System Applications are located in a different folder/volume. You shouldn’t try to locate an .app like this, but ask the OS where to find the app with a specific BundleID (in your example this would be: com.apple.mail).
Here is an example project that shows how to find an .app by BundleID, list all possible “Application Directories”, list all applications that can deal with a given file: App Directories example

I’ve just tried this quickly on macOS Big Sur.

  • fApp: /System/Applications/Preview.app
  • fDoc: /path/to/a/picture.png

Code:

fApp.Open(fDoc.ShellPath, True)    [2021r2.1]
fApp.Launch(fDoc.ShellPath, True)  [2021r2.1, 2018r4]
  • works as expected when built with Xojo 2018r4
  • just opens Preview.app (which is asking for a file to be opened) when built with Xojo 2021r2.1

Makes me think that the revamped FolderItem (Xojo 2019r2: FolderItem updated to use latest OS APIs on macOS) is still not working as expected for this situation :frowning:
If so… then Shell should be an alternative.

Only just saw and downloaded your NSSharingService example. Looks great! Trying tomorrow.