【问题标题】:Can I use Carbon APIs to launch an application via jna?我可以使用 Carbon API 通过 jna 启动应用程序吗?
【发布时间】:2011-08-04 01:29:34
【问题描述】:

简而言之:Carbon 是否适用于这项任务,还是我应该将其转储并寻找 Cocoa 解决方案?

试图编写一个小程序来询问客户端系统(Snow Leopard 及更高版本)以列出声称能够编辑单个给定文件的应用程序。用户选择一个应用程序,然后我的小程序调用 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 的支持。我完全不清楚我的立场是支持什么,什么不支持。

所以我应该得出结论,Carbon 对于这项活动来说是个死鸭子?还是我比我想象的更接近?

发送

【问题讨论】:

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


    【解决方案1】:

    放弃了 Carbon 的 LSOpenApplication,我实现了一个使用 Rococoa 来连接 Objective-C 和 Java 的解决方案。不是将 Cocoa 复合方法名称(例如 openFile:withApplication 转换为 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 上有一些有用的,如果最小的聊天的话

    【讨论】:

      猜你喜欢
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多