Deleting Application from Applications Folder macOS


I am writing my own installation application for my macOS software, and I want to be able to delete the application itself in the macOS Applications folder. I can’t get anything to work. The code I have is:

Dim f8 As folderitem f8 = SpecialFolder.Applications.child("Brannigans Building Construction") if f8.exists then MsgBox "The file is here." f8.Delete else Msgbox "The file can't be found" End If

This code does nothing. I was wondering what I am doing wrong.

Did you try - previously - to delete an application from the macOS Applications folder (withing the Finder) ?

If you want to move an application out of the Applications folder, you will only get an Alias (I think). If you press the Option key while dragging the application out of the Applications folder, the application will be moved. Check manually for yourself if I am right (I hope so).

Try to do that with a different folder §from Documents or Downloadings) with your code…

@James Redway
Your code works for me in Mojave using Xojo 2019R3.1. What versions of macOS and Xojo are you targeting?

Hi Emile,

I can delete anything using Finder.

I’m trying to do it is code. Delete the .app file and then replace it with a new one. Copying a new .app. I thought migght replace the old one, but it does not.

[code]If DebugBuild Then
f4=GetFolderItem(“Brannigans Building Construction”)
f4 =app.ExecutableFile.Parent.Parent.Child(“Resources”).Child(“Brannigans Building Construction”)
End If

If Not f4.Exists Then
MsgBox “Application Does Not Exist”


End If[/code]

This code does not overwrite the older application file.

I am using Catalina. Xojo 2019 R1.1

I would try 2019R3.1. There were changes to FolderItem for Catalina. I don’t run Catalina in as my primary OS (just VMs), so maybe someone who does can chime in with their experiences here.

I just tried it with Xojo 2019 R3.0 on High Sierra and it still does not work. You actually ran the code with an Application file or just a regular file? It works with regular files.

I ran it using an app that does exist, and received the message box: “The file is here.” Ah, but Delete doesn’t actually work. Missed that line, and you said it “didn’t work”, so I assumed you were getting a crash or the “The file can’t be found” message box. That’s my bad.

I get “The File Is Here Message” too, but nothing deletes. Don’t know what I am doing wrong.

You may not be doing anything wrong. It may be that macOS doesn’t allow you to delete from the Applications folder without elevation.

If I just drag a new version of the app into the folder, if asks me to replace it does, so I don’t know why I can’t do it in code.

It may also be that it’s seen as a folder. APP “files” on macOS are actually directories, and Xojo cannot delete directories that contain files. For more information on that, see this page.

I did see that page, but I was not really sure how to implement it. I was just hoping for and easy solution, but I guess it does not exist. I appreciate your help. Thanks so much.

Happy to help.

It’s not a file, but a non-empty folder.

[quote=494101:@James Redway]
I get “The File Is Here Message” too, but nothing deletes.[/quote]
You can’t delete a non empty folder.
See the Documentation: FolderItem.Remove

Oh, search this forum for Catalina and SpecialFolder.Applications… It’s most likely not the Folder you think it is… on Catalina :wink:

I don’t really understand why. If I drag the application directly into the Applications folder with Finder, then all the files are overwritten with the newer files. If I use DMG Canvas to make an install program, it will overwrite. I just don’t understand why you can’t do the same in code, but I guess you can’t unless you first delete all the files in that folder using the code from the link above.

So maybe I will just rely on DMG Canvas to do that for me.

Yeah, I wouldn’t recommend it with the current Security theme. Apple will find ways to break this over updates, forcing you to spend a lot of time re-implementing the wheel.

Which you’ve already run into one.

You do have multiple options, so you can choose the lesser evil one.

  1. Apple provide an installer that can be used. App Wrapper can wrap your application into this. It’s pretty basic, but conforms to Apple’s Security initiative.
  2. DMG installation, DMG Canvas and DropDMG. App Wrapper features integration with DMG Canvas, so it can help to improve your workflow.

I’m using my own installer, too. On Catalina and higher you MUST use authorization when removing or copying files to the application folder. AuthorizationMBS is the old class. There is a newer one that is async.

The Finder is more complex than an API to move files or directories.

When you move something using the Finder to a location where an item with the same name already exists, the Finder handles this for you by using several API calls (functions). Generally, something along those lines:
• It prompts you whether you really want to replace the existing item, keep both or cancel.
• If you cancel, it stops here.
• If you choose “keep both”, the item is moved/copied having another, unique name (same case as if the destination item didn’t exist). It doesn’t go further.
• At this point, you chose “Replace”. The Finder now manages to do the replacement.
• It removes the existing item (target). If it’s a folder, it’ll do it recursively (at the file system level (of most file systems), you can’t delete a non-empty folder in one call; once you know that, you realise it’s “logical”: you told the file system to delete a folder, not some other items contained in it).
So, for folders (including packages like application packages (folders with an “app” extension)), it’ll do two separate steps:
1: look whether there are locked/in use/prohibited items (for which you don’t have proper permissions to continue) inside the whole folder. This is sometimes seen as the “Preparing” phase, visible when the existing folder is huge.
2: dive inside all the folders, delete every file and, for subfolders, recursively delete what’s inside the folders; only when each folder becomes empty will it be able to delete the “parent” folder, until the target folder (the one already existing in the dropped location) is emptied.
• if all that went without error, the target, existing item no longer exists. The Finder can now do the move/copy.

No file is “overwritten”, that’s just the final feeling (after all, the source/target may very well contains more or less files; overwriting files that don’t exist on either end doesn’t mean anything).
That being said, you can clearly mimic the Finder with pure Xojo code.