我可以使用Carbon API通过jna启动应用程序吗?

时间:2011-08-04 01:29:35

标签: osx-snow-leopard macos-carbon jna core-foundation

简而言之:Carbon对此任务有效,还是应该转储它并寻找Cocoa解决方案?

尝试编写一个询问客户端系统的小程序(Snow Leopard及更高版本),列出声称能够编辑单个给定文件的应用程序。用户选择一个应用程序,然后我的applet调用Launch Services来启动应用程序,并以文件作为参数。

我可以通过调用LSCopyApplicationURLsForURL获取符合条件的应用程序列表。 我可以通过调用FSPathMakeRef将文件路径转换为FSRef对象。 我不能做的是构造和使用LSApplicationParameters对象(其成员之一是FSRef),以便成功调用LSOPenURLsWithRole(其中一个参数是LSApplicationParameters)。

到目前为止我做了什么:

interface MyCarbonWrapper extends com.sun.jna.Library
{
public static final MyCarbonWrapper INSTANCE =
  (MyCarbonWrapper) Native.loadLibrary("Carbon", MyCarbonWrapper.class);
// .. various function declarations, including
  com.sun.jna.Pointer LSCopyApplicationURLsForURL(Object curlRef, int rolesMask);
  int FSPathMakeRef(Object path, PointerByReference ref, Void isDirectory);
  int LSOpenURLsWithRole(Pointer ptrArray, int roles, Void inAEParam,
    Structure myLSApplicationParams, Void outPsns, int inMaxPSCount);
}

// unsuccessful attempt to define a mapped LSApplicationParameters
public static class LSApplicationParameters
{
public int version;
public int flags;
public Pointer Application;
public Void asyncLaunchRefCon;
public Void environment;
public Void argv;
public Void initialEvent;
public static final int sizeof = 28;
}

public void openWith(String filePath)
{
  // create a CURLRef for the selected application - OK
  // Create a FSRef from the CURLRef - OK
  // Create a CFArray to contain the file argument - OK
  // create and attempt to populate a LSApplicationParameters instance - problematic
  // call LSOpenURLsWithRole - failure. Returned error code is -50
}


返回的错误代码我通常理解为映射到消息:
“用户参数列表中的错误”。

据我所知,Snow Leopard似乎已经放弃了对以FSRef为参数的一系列API的支持。对我而言,我一点也不清楚我支持什么,不支持什么。

所以我应该得出结论,碳是这项活动的死鸭?还是我比我想的更接近?

的Tx

1 个答案:

答案 0 :(得分:0)

放弃了Carbon的LSOpenApplication后,我实现了一个使用Rococoa桥接Objective-C和Java的解决方案。不是必须将Cocoa复合方法名称转换为openFile:withApplication to openFile_withApplication。

// Declare an interface which will call on a rococoa class:

public interface NSWorkspace extends NSObject
{

    public static final _Class CLASS = Rococoa.createClass("NSWorkspace", _Class.class);

    public interface _Class extends NSClass
    {
        // static method to get the workspace in
        NSWorkspace sharedWorkspace();
    }
    boolean openFile_withApplication(NSString fullPath, NSString appName);
}

// then we can call on it to do stuff

final NSWorkspace nsWorkspace = NSWorkspace.CLASS.sharedWorkspace();
boolean isRunning = nsWorkspace.openFile_withApplication(
    NSString.stringWithString(targetFilePathStr),
    NSString.stringWithString(executableApplicationPathStr));

我仍在使用Carbon进行其他一些LaunchApplication服务。 java.net/projects/rococoa/是它的主页,如果在java.net/projects/rococoa/lists/users/archive上有一些有用的聊天