How i can run my app with root or exec sudo command

Dear all,
I need to edit file hosts locate at “/etc/hosts” or /private/etc/hosts. However, all of them was need root permission to edit.
So i think can update this by shell script, but this not work. I have research on forum but i not get a solution can use.
Thanks for approve ask again topic!

The easiest solution is with a plugin (Monkeybread Xojo plugin - AuthorizationMBS class). Though I think that there is a newer API available.

If you don’t want to use a plugin you can try with AppleScript. It’s super clunky, but it still works for macOS BS:

set scr to “launchctl load -wF /Library/LaunchDaemons/com.paradigmasoft.vserver_x64.plist”

do shell script scr with prompt “Start Valentina Server” with administrator privileges

2 Likes

The “Current” security practices suggest that you do not do this.

Instead you are meant to write a special kind of helper (in a different tool and language) that only performs the read and write, then you need to use a bunch of code gymnastics to launch that special helper and utilize it from Xojo.

Instead what @Beatrix_Willius suggest about Apple Script calling a shell script is probably the easiest way. It does mean that you use will be asked for their password (I couldn’t get the fingerprint sensor to work when doing this) for each time the file is read or written to.

A word of warning, the App Store rules state that apps which use elevated privileges will be rejected. So Apple may refuse to sell your app on their App Store. Yes, I know that there are apps on the Mac App Store that do this. But you need a “special” relationship with Apple to bend the rules that are applied evenly and fairly.

1 Like

I recently found this tutorial Cocoa: implement a privileged Helper | Woody's findings. It seems to be a lot of work.

2 Likes

T’was last time I looked into it too. So I did the same as you, AppleScript to call a shell script.

1 Like

You may look into the preferred way from Apple nowadays with a helper command line tool.
See ServiceManagementModuleMBS module.

2 Likes

Thanks you for your ideas, however our application just use internal we not have plan to sell or upload this to appstore.
We’r just try to make a application to also make our work faster.
I’m also fresher with Xojo so that stack on there, search on forum i see same people like me.

1 Like

Hope you can share more your exp, i see many commented of your at old topic, but i’m not know how to instance this into xojo. Do you have a same same tutorial or document for that?
Thanks you so much, Beatrix!!

Check out the docs for using AppleScripts: UserGuide:AppleScripts - Xojo Documentation

Basically, you drag the AppleScripts into the IDE and then you can call them by name. What you MUST do is to set the correct security for the AppleScripts. The docs has links on that, too.

2 Likes

Thanks Beatrix,
How about for , i look it at Monkeybread Xojo plugin - AuthorizationMBS class but i don’t know how to install this plugin into xojo.
I search on forum and see a last topic of you at here. it like me trouble, how do you solve this?

Have a look at MBS Xojo Plugin Installation - YouTube . Other users might tell you that you can do everything yourself, which is true. But the plugin will save you a lot of time. And sometimes it will save your behind.

3 Likes

Thanks, My code bellow:
dim a as AuthorizationMBS
dim params( ) as string

a = new AuthorizationMBS
if a.SimpleNewAuthorization then
  a.SimpleAuthorize
  
  if not a.Authorized then
    'return
    MsgBox "admin password required to install the system extensions, no changes were made."
    return
  end if
  
  params.Append "'127.0.0.1 local.example.com'  >> /etc/hosts"
  
  a.Execute( "/bin/echo" , params)
  MsgBox str(a.LastError)
end if

So this not work, i want add more row to file. please check this help me.

Thanks to @Beatrix_Willius @Sam_Rowlands @Christian_Schmitz for let time to help me in this issue.
So In this case i use AuthorizationMBS Plugin to resolve, and was be done.


Why:
Because I was need to edit the file at protected folder of Unix (MacOS) at SpecialFolder.Etc (/private/etc/).
Solution:

  1. Use AuthorizationMBS Plugin
  2. Set chmod this file to 777 (Allow edit, execute)
  3. Use Shell Excute of Xojo to edit file easy
1 Like

Glad to hear that you got it working, I would recommend that you once you save the data, you set the permissions back to what they were before.

1 Like
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.