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 6.0.app")
if f8.exists then
MsgBox "The file is here."
Msgbox "The file can't be found"
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
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 6.0.app”)
f4 =app.ExecutableFile.Parent.Parent.Child(“Resources”).Child(“Brannigans Building Construction 6.0.app”)
If Not f4.Exists Then
MsgBox “Application Does Not Exist”
This code does not overwrite the older application file.
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 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.
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.