3

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?

Community
  • 1
  • 1
Zebi
  • 8,682
  • 1
  • 36
  • 42

3 Answers3

4

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.

Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
  • How can I check if an application is scripting enabled? Check the function browser in apples script editor? – Zebi May 04 '11 at 19:52
  • 1
    There are two ways: (1) in the Apple Script Editor, choose File > Open Dictionary; the appplication should be listed or (2) in a Terminal prompt, type "sdef /Applications/.app"; if there is an error, then the application is not scriptable. – Laurent Etiemble May 05 '11 at 06:19
1

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.

ddelver
  • 456
  • 3
  • 9
  • This would be really nice, I will try this as soon as I manage to get some free time :) – Zebi Mar 19 '12 at 13:21
0

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/

Peter
  • 9,643
  • 6
  • 61
  • 108