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

时间:2022-09-07 08:51:33

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.)

我想做一个自动操作来访问我的mac应用中的一些类,这似乎是我理想的方式(意味着你必须购买我的应用才能使用自动操作,等等)

5 个解决方案

#1


4  

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; 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.

取决于您想要做什么(我不是很清楚),服务可能会为您实现这个目的。你制作了一个助手应用程序,它可以使用共享的pasteboard将数据来回传递给你的应用程序。您可以获得相当广泛的操作范围,因为您可以传递任何符合NSPasteboardWriting和NSPasteboardReading协议的对象;正如文档中所说的,NSString NSAttributedString NSURL NSColor NSSound和NSImage已经为您提供了,当然,您可以编写一个完全符合您需要的定制类。

#2


3  

Have you tried creating a stand-alone Automator plugin project, or tried adding an Automator bundle target to your application's project?

您是否尝试过创建一个独立的自动程序插件项目,或者尝试过将自动程序包目标添加到应用程序的项目中?

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).

我假设您希望为主应用程序创建自动操作,但不清楚如何让这些操作与应用程序(或应用程序中出现的类)交互。

There are 3 basic types of Automator actions: AppleScript-based, shell-script based, and Objective-C based. 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). (Note that by default, when you add a new target for an automator bundle, it's an AMAppleScriptAction type).

自动操作有三种基本类型:基于应用程序的、基于shell脚本的和基于Objective-C的。您很可能希望使您的Objective-C基于,这将允许您轻松地将来自主应用程序的其他Objective-C代码合并到操作本身中(请参见实现Objective-C操作)。(注意,默认情况下,当您为automator bundle添加新目标时,它是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.

要查看Objective-C自动操作与基于applescript的操作相比是如何设置的,您可能需要尝试创建一个独立的项目。

Let's say your app is document-based, and uses the KWDocument class, which exposes a method named -duplicateObjects:(NSArray *)objects toDocument:(KWDocument *)destDocument;. You also have a KWRegistrationManager that knows whether your app is registered or not. And let's say you want to create an automator action that's called "Duplicate Objects to Document". The action will be implemented in KWDuplicateObjectsToDocument, which is as a subclass of AMBundleAction. In the Info.plist for Duplicate Objects to Document.action, the NSPrincipalClass will be KWDuplicateObjectsToDocument.

假设您的应用是基于文档的,并使用KWDocument类,它公开了一个名为-duplicate object:(NSArray *)的方法toDocument:(KWDocument *)destDocument;您还有一个KWRegistrationManager,它知道您的应用程序是否已注册。假设你想创建一个自动操作,叫做“复制对象到文档”。操作将在KWDuplicateObjectsToDocument中实现,它是AMBundleAction的子类。的信息。plist用于记录重复对象。动作,nsprincipal类将是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.

您需要确保所使用的必要的类(如KWRegistrationManager、KWDocument等)被编译并作为这个包的构建过程的一部分。

#3


2  

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.

这意味着您将无法从应用程序二进制文件中加载任何代码部分,就像在库中一样。

#4


2  

Take a look at distributed objects. Your application could vend one or more objects that your Automator action could use. 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. I think it's definitely worth a look.

看看分布式对象。您的应用程序可以vend一个或多个自动操作可以使用的对象。我从来没有用过Automator,但这是一个非常优雅的系统,近年来并没有受到太多关注。我认为这绝对值得一看。

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.

分布式对象的一个很酷的方面是,如果您愿意,应用程序可以运行在同一台计算机上,但它也可以同样容易地运行在另一台计算机上,甚至可能是非常远的一台计算机上。

#5


2  

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. 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). 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.

您可以通过Applescript使您的应用程序的某些行为成为可访问的,但是我认为您的意思是不可能访问实际的类。我得到的印象是,你的意思是访问加载到运行应用程序的内存中的类,这在OS X(或任何类似unix的系统)上是不可能的。应用程序在用户级运行。用户级的处理器不能从其他进程读取内存。操作系统的组件需要在内核级运行。

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. That looks pretty cool.

从NSGod的回答来看,你似乎可以使用同样的方法使它可以通过Applescript访问,并通过object - c访问。看起来很酷。

#1


4  

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; 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.

取决于您想要做什么(我不是很清楚),服务可能会为您实现这个目的。你制作了一个助手应用程序,它可以使用共享的pasteboard将数据来回传递给你的应用程序。您可以获得相当广泛的操作范围,因为您可以传递任何符合NSPasteboardWriting和NSPasteboardReading协议的对象;正如文档中所说的,NSString NSAttributedString NSURL NSColor NSSound和NSImage已经为您提供了,当然,您可以编写一个完全符合您需要的定制类。

#2


3  

Have you tried creating a stand-alone Automator plugin project, or tried adding an Automator bundle target to your application's project?

您是否尝试过创建一个独立的自动程序插件项目,或者尝试过将自动程序包目标添加到应用程序的项目中?

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).

我假设您希望为主应用程序创建自动操作,但不清楚如何让这些操作与应用程序(或应用程序中出现的类)交互。

There are 3 basic types of Automator actions: AppleScript-based, shell-script based, and Objective-C based. 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). (Note that by default, when you add a new target for an automator bundle, it's an AMAppleScriptAction type).

自动操作有三种基本类型:基于应用程序的、基于shell脚本的和基于Objective-C的。您很可能希望使您的Objective-C基于,这将允许您轻松地将来自主应用程序的其他Objective-C代码合并到操作本身中(请参见实现Objective-C操作)。(注意,默认情况下,当您为automator bundle添加新目标时,它是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.

要查看Objective-C自动操作与基于applescript的操作相比是如何设置的,您可能需要尝试创建一个独立的项目。

Let's say your app is document-based, and uses the KWDocument class, which exposes a method named -duplicateObjects:(NSArray *)objects toDocument:(KWDocument *)destDocument;. You also have a KWRegistrationManager that knows whether your app is registered or not. And let's say you want to create an automator action that's called "Duplicate Objects to Document". The action will be implemented in KWDuplicateObjectsToDocument, which is as a subclass of AMBundleAction. In the Info.plist for Duplicate Objects to Document.action, the NSPrincipalClass will be KWDuplicateObjectsToDocument.

假设您的应用是基于文档的,并使用KWDocument类,它公开了一个名为-duplicate object:(NSArray *)的方法toDocument:(KWDocument *)destDocument;您还有一个KWRegistrationManager,它知道您的应用程序是否已注册。假设你想创建一个自动操作,叫做“复制对象到文档”。操作将在KWDuplicateObjectsToDocument中实现,它是AMBundleAction的子类。的信息。plist用于记录重复对象。动作,nsprincipal类将是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.

您需要确保所使用的必要的类(如KWRegistrationManager、KWDocument等)被编译并作为这个包的构建过程的一部分。

#3


2  

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.

这意味着您将无法从应用程序二进制文件中加载任何代码部分,就像在库中一样。

#4


2  

Take a look at distributed objects. Your application could vend one or more objects that your Automator action could use. 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. I think it's definitely worth a look.

看看分布式对象。您的应用程序可以vend一个或多个自动操作可以使用的对象。我从来没有用过Automator,但这是一个非常优雅的系统,近年来并没有受到太多关注。我认为这绝对值得一看。

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.

分布式对象的一个很酷的方面是,如果您愿意,应用程序可以运行在同一台计算机上,但它也可以同样容易地运行在另一台计算机上,甚至可能是非常远的一台计算机上。

#5


2  

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. 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). 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.

您可以通过Applescript使您的应用程序的某些行为成为可访问的,但是我认为您的意思是不可能访问实际的类。我得到的印象是,你的意思是访问加载到运行应用程序的内存中的类,这在OS X(或任何类似unix的系统)上是不可能的。应用程序在用户级运行。用户级的处理器不能从其他进程读取内存。操作系统的组件需要在内核级运行。

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. That looks pretty cool.

从NSGod的回答来看,你似乎可以使用同样的方法使它可以通过Applescript访问,并通过object - c访问。看起来很酷。