简体   繁体   中英

How to pass data from Mac Automator to Xamarin.Mac C# application?

I am new to osx and xamarin development. I have developed an application to do some stuff on a given folder using Xamarin.Mac in C#. I added this application to right-click menu of folders in Finder using Automator Service. How can I pass the name of the right-clicked folder to Xamarin application? If this is not possible how to add a file / folder browse button to my application in XCode? Thanks in advance

In Automator you can use a "Run Shell Script" action and open the XamMac application with arguments like you would if you were manually launching it via the cmd line.

This would be a "Run Shell Script" as an "Automator Service":

for f in "$@"
do
    myArgs+="\""${f}"\""" "
done
echo "${myArgs}"
/Applications/XamMacStartupArgs.app/Contents/MacOS/XamMacStartupArgs "${myArgs}" &

In your XamMac application, Main will receive those args passed in, you of course would actually do something with them:

static class MainClass
{
    static void Main (string[] args)
    {
        Console.WriteLine (args[0]);
        NSApplication.Init ();
        NSApplication.Main (args);
    }
}

NOTE: If you need to pass "arguments" to a running XamMac app, you would need to hook the open and reopen Apple event handlers and handle them that way instead:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_handle_AEs/SAppsHandleAEs.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM