Delete file from within Apple script as root with administrator privileges does not work, if file was created/downloaded by user, not root

  Kiến thức lập trình

I need to solve following problem on macOS. I need to work with some files on on macOS from the Apple script.

For example, I have a file in the file system in folder /Users/username/Documents/Test/test.zip which has 777 permissions.

a) I can delete the file from the terminal as current user by rm /Users/username/Documents/Test/test.zip
b) I can delete the file from the terminal as sudo by sudo rm /Users/username/Documents/Test/test.zip
c) but If I do this from within Apple script with administrator privileges! it is not possible, I get permission error

=> It strongly depends on who downloaded the file to the hard drive!

Apple script like this:

— Define the path to the file you want to delete set filePath to (path to documents folder as text) & “/Users/username/Documents/Documents/Test/test.zip” — Use do shell script with rm command to delete the file with admin privileges do shell script “rm ” & quoted form of POSIX path of filePath with administrator privileges

This is example, I want actually to call my app to manipulate some files in the system. I can create files in the system directories also from within this script, but I can never delete (ovewrite) them, if somebody else than me (root in this case) created them. If I did created them, I can delete them freely.

If I download the file from within the AppleScript as root, I can then delete it either by method a) b) and c) Method c) is most important for me. But the problem is, if I want to download the file, which already exist in the location and somebody else created it, then I am not able to update it…

I tried to set permission to target files (chmod) 0777 (and so to its folders), change the owner to the root (chown), remove xattributes but nothing helped , removed non mutable attributes, nothing helped, it is irrelevant in this case what is removed from the file. It looks like it only depends who created it.

I tried to execute the rm from within the Apple script via other shell script, OSA script, still the same issue. Simply macOS SIP is blocking it. No matter which file, it happens on all my three Macs (Ventura, Sonoma…) so it is consistent, file system is not damaged. My file system is OK, I run disk check and this happens to any file I try on more Macs. If I disable SIP, it works, but this is of course not the solution.

Script editor and my App which I call from within the apple script has full disc access and so my app binary is in 0777 mode.

Method c) must work, as I am actually calling my app from within the Apple script, but it does not even work from the Apple script itself, so the problem is there, not my app. There must be some settings or workaround for this.

The reason I am using Apple script is I need to copy some files to the system folders and this I can do as root, not as user and of course I can do it If I call my app as sudo from termnial, but this is not nice solution, therefore I thought the Apple script will be the right one.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT