Pergunta

I am trying to create a simple app with MonoMac and need to execute apple script to access playlist information from different media players (see my previous question).

I decided to give MonoMac a try because I am very familiar with C# and .net development and there is a nice Websocket implementation.

Unfortunately there seems to be no working wrapper for NSAppleScript. I tried Frederic Forjans implementation and tried to use monobjc.

Frederics wrapper class does not even compile when I use his code I get the following exception:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'MonoMac, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
File name: 'MonoMac, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'

when creating a new NSString instance. However new NSDictionary(); which is located in the same assembly works fine.

When using monobjc I get the following error:

Unhandled Exception: System.TypeInitializationException: An exception was 
thrown by the type initializer for Monobjc.ObjectiveCRuntime ---> 
System.EntryPointNotFoundException: monobjc_install_bridge
  at (wrapper managed-to-native) Monobjc.NativeMethods:InstallBridge ()
  at Monobjc.ObjectiveCRuntime..cctor () [0x00000] in :0 
  --- End of inner exception stack trace ---
  at CocoaApplication1.Program.Main () [0x00000] in :0 

Can anyone suggest a simple and working way to execute apple scripts in a mono environment?

Foi útil?

Solução

You cannot use the Monobjc assemblies directly without running them with the Monobjc runtime binary. Whether you want to build a Cocoa based or console application, there are tutorials for that on the Monobjc's website to help you getting started.

The easiest way to achieve inter-application communication is to use the ScriptingBridge framework. Many applications provide scripting definitions that can be then used in an object-oriented way: take a look at this guide for more details.

Note that the scripting bridge will only works with scripting enabled applications (like iTunes, iCal, iMovie, Finder, etc).

Monobjc supports the ScriptingBridge framework; there are even two sample applications (SBSetFinderComment and ScriptingBridgeiCal) that show how to integrate it.

Outras dicas

If possible try using Monobjc. Monobjc provides a very straightforward way of executing custom AppleScript.

The following code snippet shows the main steps:

string script = ". . . . . . . . . ."; // some applescript text
NSDictionary error = null;
NSAppleScript scr = new NSAppleScript(script);
NSAppleEventDescriptor result = scr.ExecuteAndReturnError(out error);
string retval = result.StringValue;

I have written a blog post explaining the same. There's also a sample Monobjc project provided.

I was trying to execute AppleScript from a WinForms Mono project and ended up building my own little AppleScript execution library called AppleScript Slim. !00% of the code came from the Mono Develop project.

AppleScript Slim: https://applescriptslim.codeplex.com/

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top