xamarin.mac multiple do shell script with administrator privilege with one password request

andyeduandyedu Andrea DellaviaCHMember

Dear all of you, I'm trying to convert an old Applescript that used different commands "do shell script... with administrator privilege" so that you can run packages and other similar commands in the MacOS system with high privileges.

With Applescript, the password was requested only once during the first command "... with administrator privilege" while trying a similar code in C# with the function NSApplescript and Xamain.Mac I do not find the way to allow the execution of other commands using the same credentials previously entered by the user.

I've read that normally for 5 minutes these should be valid, but in this case it's as if they were reset after the execution of the first command "do shell script... with administrator privilege".

Does anyone have an idea of how it is the right way to proceed or an alternative to use a similar procedure in this new program created with Xamarin.Mac?

Thank you very much to everyone who can help me in some way.


  • ChrisHamonsChrisHamons Chris Hamons USXamarin Team Xamurai

    Xamarin.Mac can invoke apple scripts via NSAppleScript. We don't have a sample for those, but Apple has a write up here:


    You might need a refresher on obj-c syntax to follow it:


    The following stack overflow post may also help:


  • andyeduandyedu Andrea Dellavia CHMember
    edited September 13

    Dear Chris, thank you very much for your help and for answering my question.

    I found an example of code that uses NSAppleScript, but I still have to try to modify it by analyzing the information that is indicated on the websites you have indicated to me.

    In the meantime I have also tried to use another function called "ExecuteWithPrivileges" that seems to work, even if I think I understood that it should not be used anymore because it was deprecated by Apple.


    using System;
    using AppKit;
    using Foundation;
    namespace PlayiTunes
        static class MainClass
            static void Main(string[] args)
                // Cocoa初期化(NS〜系クラスを使うためのおまじない)
                // iTunesを起動し再生するAppleScript
                // 使い終わったら必ずDispose()すること。
                using (var script = new NSAppleScript("tell application \"iTunes\" to play"))
                    // スクリプトを実行する
                    var errors = new NSDictionary();
                    var result = script.ExecuteAndReturnError(out errors);
                    if (result != null)
                        // 戻り値はNSAppleEventDescriptor
                }//dispose script
        string[] nomiFiles = { "pippo.txt", "pluto.txt", "pappa.txt" };
                var defaults = Security.AuthorizationFlags.Defaults;
                using (var auth = Security.Authorization.Create(defaults))
                    foreach (string value in nomiFiles)
                        var args = new[] { "-c", "\"\"" + "whoami" + " " + ">> /Users/Andrea/Desktop/" + value + "\"\"" };
                        var result = auth.ExecuteWithPrivileges("/bin/sh", defaults, args);
  • ChrisHamonsChrisHamons Chris Hamons USXamarin Team Xamurai

    If you select code blocks and use the "code" formatting option, it will make your posts much more readable. I've taken the liberty to fix the post above.

    ExecuteWithPrivileges works, but Apple wants you to use this API:


    It is a total nightmare to use however, and they haven't removed ExecuteWithPrivileges for many releases at this point.

  • andyeduandyedu Andrea Dellavia CHMember

    Dear Chris, thank you for settling the formatting of the code in the previous commentary.

    For the problem mentioned in my question, I was thinking about using the "ExecuteWithPrivileges" function for now and then trying to run that "NSAppleScript" to get an alternative if the first one no longer works in the future.

    Maybe as MacOS 10.13 will come out in about ten days, I can check if the "deprecated" function will also work with this version and then proceed in this way if everything is OK.

    Thank you again.

Sign In or Register to comment.