AppleScript on root file


I just try to write my first AppleScript that would aim to add lines at the very end of the hosts file.

set fileHosts to "/etc/hosts"
set hostToAdd to "#mes hosts à rajouter"

open for access fileHosts with write permission
write hostToAdd to fileHosts
write hostToAdd to fileHosts starting at eof
close access fileHosts

When I do a test file of my account, no problem, but when I do it on the real hosts file (in root account), I get the following error: “Error network file permission.”. The “network” seems to me strange, but speculates that it is a permission problem.

Are there means he asks the administrator password in order to save this file? And if possible, how to do it?

Thank you in advance.

Does this work?

do shell script "echo '#mes hosts à rajouter' >>~/../../private/etc/hosts" with administrator privileges

You will be asked for the administrators password!

set hostToAdd to "echo '

#my hosts

' >>"
set fileHosts to "/etc/hosts"

do shell script hostToAdd & fileHosts with administrator privileges

Well, require using the shell script, after all, it’s even less typing.

Thank you!