简体   繁体   English

我可以将应用程序用作库(Mac,Objective-c)吗?

[英]Can I use an application as a library (Mac, Objective-c)?

Is there a way to load classes from a mac app, and use them in a different mac app? 有没有办法从mac应用程序加载类,并在不同的Mac应用程序中使用它们?

I'd like to make an Automator action that accesses some of the classes in my mac app, and this seems like the sort of way I'd ideally do it (means you have to have bought my app to use the Automator action, etc.) 我想做一个Automator动作来访问我的mac应用程序中的一些类,这看起来像我理想的那样(意味着你必须购买我的应用程序才能使用Automator动作等等) 。)

Depending on what you want to do (I'm not quite clear), a Service might do the trick for you. 根据你想做的事情(我不太清楚), 服务可能会为你做到这一点。 You make a helper app which can pass data back and forth with your app, using a shared pasteboard. 您创建了一个帮助应用程序,可以使用共享的粘贴板在您的应用程序之间来回传递数据。 You can get a fairly wide range of action, because you can pass any object that conforms to the NSPasteboardWriting and NSPasteboardReading protocols; 您可以获得相当广泛的操作,因为您可以传递任何符合NSPasteboardWritingNSPasteboardReading协议的对象; as it says there in the docs, NSString , NSAttributedString , NSURL , NSColor , NSSound , and NSImage are already available for you, and of course you can write a custom class that suits your needs exactly. 正如它在文档中说的那样, NSStringNSAttributedStringNSURLNSColorNSSoundNSImage已经可以使用了,当然你可以编写一个完全符合你需求的自定义类。

Have you tried creating a stand-alone Automator plugin project, or tried adding an Automator bundle target to your application's project? 您是否尝试过创建独立的Automator插件项目,或尝试将Automator捆绑目标添加到应用程序的项目中?

I'm assuming that you want to create Automator actions for your main app, but are unclear how you get these actions to interact with your application (or with the classes present in your application). 我假设您要为主应用程序创建Automator操作,但不清楚如何使这些操作与您的应用程序(或应用程序中存在的类)进行交互。

There are 3 basic types of Automator actions: AppleScript-based, shell-script based, and Objective-C based. Automator操作有3种基本类型:基于AppleScript,基于shell脚本和基于Objective-C。 You'll most likely want to make yours Objective-C-based, which will allow you to easily incorporate other Objective-C code from your main application into the action itself (see Implementing an Objective-C Action ). 您很可能希望基于Objective-C创建基于Objective-C的,这将允许您轻松地将主应用程序中的其他Objective-C代码合并到操作本身中(请参阅实现Objective-C操作 )。 (Note that by default, when you add a new target for an automator bundle, it's an AMAppleScriptAction type). (请注意,默认情况下,为automator包添加新目标时,它是AMAppleScriptAction类型)。

To see how an Objective-C automator action is set up compared to an AppleScript-based action, you might want to try creating a separate standalone project. 要查看与基于AppleScript的操作相比如何设置Objective-C自动机操作,您可能需要尝试创建单独的独立项目。

Let's say your app is document-based, and uses the KWDocument class, which exposes a method named -duplicateObjects:(NSArray *)objects toDocument:(KWDocument *)destDocument; 假设您的应用程序是基于文档的,并使用KWDocument类,该类公开名为-duplicateObjects:(NSArray *)objects toDocument:(KWDocument *)destDocument;的方法-duplicateObjects:(NSArray *)objects toDocument:(KWDocument *)destDocument; . You also have a KWRegistrationManager that knows whether your app is registered or not. 您还有一个KWRegistrationManager ,它知道您的应用程序是否已注册。 And let's say you want to create an automator action that's called "Duplicate Objects to Document". 而且假设你想要创建一个名为“Duplicate Objects to Document”的自动机器人动作。 The action will be implemented in KWDuplicateObjectsToDocument , which is as a subclass of AMBundleAction . 该操作将在KWDuplicateObjectsToDocument中实现,该文件作为AMBundleAction的子类。 In the Info.plist for Duplicate Objects to Document.action, the NSPrincipalClass will be KWDuplicateObjectsToDocument . 在Info.plist for Duplicate Objects to Document.action中, NSPrincipalClass将是KWDuplicateObjectsToDocument

KWDuplicateObjectsToDocument.h will look something like: KWDuplicateObjectsToDocument.h将如下所示:

#import <Cocoa/Cocoa.h>
#import <Automator/AMBundleAction.h>

@interface KWDuplicateObjectsToDocument : AMBundleAction {

}

- (id)runWithInput:(id)input fromAction:(AMAction *)anAction
                                  error:(NSDictionary **)errorInfo;

@end

And your KWDuplicateObjectsToDocument.m will look something like this: 你的KWDuplicateObjectsToDocument.m看起来像这样:

#import "KWDuplicateObjectsToDocument.h"
#import "KWDocument.h"
#import "KWRegistrationManager.h"


@implementation KWDuplicateObjectsToDocument

- (id)runWithInput:(id)input fromAction:(AMAction *)anAction
          error:(NSDictionary **)errorInfo {
      if (![[KWRegistrationManager defaultManager] isRegistered]) {
          return nil;
      }

    // eventually you'll call 
    // duplicateObjects:toDocument:

      return input;
}

@end

You'll need to make sure that the necessary classes you use (such as KWRegistrationManager , KWDocument , etc.) are compiled and included as part of the build process for this bundle. 您需要确保编译并包含您使用的必要类(例如KWRegistrationManagerKWDocument等)作为此捆绑包的构建过程的一部分。

You could make certain behavior from you app accessible via Applescript, but accessing the the actual classes is not possible in the way I think you mean. 您可以通过Applescript从您的应用程序中获取某些行为,但访问实际的类是不可能的,我认为你的意思。 I get the impression that you mean accessing the classes loaded into the memory of your running app. 我的印象是,您的意思是访问加载到正在运行的应用程序内存中的类。 This is not possible on OS X (or any UNIX-like system). 这在OS X(或任何类UNIX系统)上是不可能的。 Applications run at the user level. 应用程序在用户级别运行。 Processess at the user level are not able to read memory from other processes. 用户级别的进程无法从其他进程读取内存。 The components of the OS that need to do this sort of thing run at kernel level. 需要执行此类操作的操作系统组件在内核级别运行。

If you are just trying to reuse the code, you could build the parts you want to share into a static library, and others could link against it and share your code. 如果您只是尝试重用代码,则可以将要共享的部分构建到静态库中,而其他部分可以链接它并共享您的代码。

EDIT: 编辑:

From NSGod's answer it seems that you can use the same approach that makes it accessible via Applescript and make it accessible via Obj-C. 从NSGod的回答看来,你可以使用相同的方法,通过Applescript访问它,并通过Obj-C访问它。 That looks pretty cool. 这看起来很酷。

Take a look at distributed objects . 看看分布式对象 Your application could vend one or more objects that your Automator action could use. 您的应用程序可以提供Automator操作可以使用的一个或多个对象。 I've never tried it with Automator, but it's a very elegant system that hasn't gotten a lot of attention in recent years. 我从未尝试过使用Automator,但它是一个非常优雅的系统,近年来并没有引起太多关注。 I think it's definitely worth a look. 我认为这绝对值得一看。

One cool aspect of distributed objects is that the application could be running on the same computer if you wish, but it could just as easily be running on a different computer, perhaps even one that's very far away. 分布式对象的一个​​很酷的方面是,如果你愿意,应用程序可以在同一台计算机上运行,​​但它可以很容易地在不同的计算机上运行,​​甚至可能在很远的地方运行。

Basically, no: you cannot link with an executable. 基本上,不,你不能与可执行文件链接。

An application binary is in a specific format. 应用程序二进制文件采用特定格式。 And that format is different from the static or shared library format. 并且该格式与静态或共享库格式不同。

It means you won't be able to load any code parts from an application binary, as you would with a library. 这意味着您无法像使用库一样从应用程序二进制文件加载任何代码部分。

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

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