简体   繁体   English

MonoMac - 我如何执行苹果脚本

[英]MonoMac - how can I execute apple script

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 ).我正在尝试使用 MonoMac 创建一个简单的应用程序,并且需要执行苹果脚本来访问来自不同媒体播放器的播放列表信息(请参阅我之前的问题)。

I decided to give MonoMac a try because I am very familiar with C# and .net development and there is a nice Websocket implementation.我决定尝试 MonoMac,因为我非常熟悉 C# 和 .net 开发,并且有一个不错的 Websocket 实现。

Unfortunately there seems to be no working wrapper for NSAppleScript.不幸的是,似乎没有适用于 NSAppleScript 的包装器。 I tried Frederic Forjans implementation and tried to use monobjc .我尝试了Frederic Forjans 的实现并尝试使用monobjc

Frederics wrapper class does not even compile when I use his code I get the following exception:当我使用他的代码时,Frederics 包装器 class 甚至无法编译我得到以下异常:

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.创建新的NSString实例时。 However new NSDictionary();然而new NSDictionary(); which is located in the same assembly works fine.位于同一组件中的工作正常。

When using monobjc I get the following error:使用 monobjc 时出现以下错误:

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?谁能建议一种在 mono 环境中执行苹果脚本的简单且有效的方法?

You cannot use the Monobjc assemblies directly without running them with the Monobjc runtime binary.如果不使用 Monobjc 运行时二进制文件运行它们,则不能直接使用 Monobjc 程序集。 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.无论您是想构建基于 Cocoa 的应用程序还是控制台应用程序,Monobjc 的网站上都有相应的教程来帮助您入门。

The easiest way to achieve inter-application communication is to use the ScriptingBridge framework.实现应用程序间通信的最简单方法是使用 ScriptingBridge 框架。 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).请注意,脚本桥仅适用于启用脚本的应用程序(如 iTunes、iCal、iMovie、Finder 等)。

Monobjc supports the ScriptingBridge framework; Monobjc 支持 ScriptingBridge 框架; there are even two sample applications ( SBSetFinderComment and ScriptingBridgeiCal ) that show how to integrate it.甚至还有两个示例应用程序( SBSetFinderCommentScriptingBridgeiCal )展示了如何集成它。

If possible try using Monobjc.如果可能,请尝试使用 Monobjc。 Monobjc provides a very straightforward way of executing custom AppleScript. Monobjc 提供了一种非常直接的方式来执行自定义 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.还提供了一个示例 Monobjc 项目。

I was trying to execute AppleScript from a WinForms Mono project and ended up building my own little AppleScript execution library called AppleScript Slim.我试图从 WinForms Mono 项目执行 AppleScript 并最终构建了我自己的小 AppleScript 执行库,名为 ZFF34154EAA67BCD7822D.F .00% of the code came from the Mono Develop project. 0.00% 的代码来自 Mono 开发项目。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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