Open a file with TextEdit

Hello,
I am trying to launch a file, SpecialFolder.Desktop.child(“links.txt”), programatically with TextEdit, it does not launch but the message box is displayed.
What could be the problem?

Dim document As FolderItem
Dim TextEditApp As FolderItem = SpecialFolder.Applications.child("TextEdit.app")
document = SpecialFolder.Desktop.child("links.txt")
// https://forum.xojo.com/t/launch-and-open-file/54435/2
dim sh as new shell
dim command as String = "open -a " + TextEditApp.ShellPath + " " + document.ShellPath
sh.Execute(command) // Does not work
msgbox "Button pressed"

This link is closed Launch and Open File - #2 by Christian_Mézes

Thanks in advance.

Lennox

Hi Lennox,
First check the return code after the shell execution and test by running the command from the terminal.
It should give you some information.

I believe you need to use “/use/bin/open”. Shells are not terminal sessions, they don’t have default paths set up for you.

Change this line:

to:

dim command as String = "open -a /System" + TextEditApp.ShellPath + " " + document.ShellPath

This work too (if you prefer to change the SpecialFolder code, so change below without changing the above code):

'Dim TextEditApp As FolderItem = SpecialFolder.Applications.child("TextEdit.app")
Dim TextEditApp As FolderItem = SpecialFolder.System.child("Applications").child("TextEdit.app")

Edit:

This is what I did to find the problem:

The application /Applications/TextEdit.app cannot be opened for an unexpected reason, error=Error Domain=NSCocoaErrorDomain Code=260 “The file “TextEdit.app” couldn’t be opened because there is no such file.” UserInfo={NSURL=file:///Applications/TextEdit.app, NSFilePath=/Applications/TextEdit.app, NSUnderlyingError=0x600002a51440 {Error Domain=NSPOSIXErrorDomain Code=2 “No such file or directory”}}

  • right click on TextEdit.app and Get Info
  • where information to find where really TextEdit.app is (/System/Applications)

Hope this helps.

Edit2: you may want to test if TextEditApp exists too (to find the problem):

Edit3: Jared offers the simplest solution below.

You can also just do

"open -a TextEdit.app " + document.ShellPath

and it will find TextEdit without needing the path

1 Like

And that’s better, as TextEdit is still in /Applications in previous versions of the OS.

1 Like

Thanks AlbertoD and everyone else,
This works…

dim command as String = "open -a /System" + TextEditApp.ShellPath + " " + document.ShellPath

Thanks again.
Lennox

Thanks AlbertoD,

Your explanation and graphics really made me understand the issue, really appreciate your effort.
Thanks again.

Lennox

1 Like

If you want to always find the correct folder (or FolderItem) of an application (no matter which macOS version, if it’s in /Applications or /System/Applications, or maybe even /Users/username/Applications), then have a look at this example project showing the “macOS way(s)” of finding that (best: search by it’s BundleID):

Thanks Jürg Otter,
Lennox