代码之家  ›  专栏  ›  技术社区  ›  Joshua Terrill

将MacOS.app文件注册为打开文件扩展名时的默认文件,将文件作为参数单击

  •  0
  • Joshua Terrill  · 技术社区  · 1 年前

    我有一个名为FileOpener.app的应用程序,它有以下内容:

    FileOpener.app
      - Contents
        Info.plist
        - MacOS
          - FileOpener
        - Resources
    

    Info.plist的内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>CFBundleDevelopmentRegion</key>
        <string>English</string>
        <key>CFBundleExecutable</key>
        <string>FileOpener</string>
        <key>CFBundleIdentifier</key>
        <string>com.example.FileOpener</string>
        <key>CFBundleInfoDictionaryVersion</key>
        <string>6.0</string>
        <key>CFBundleName</key>
        <string>FileOpener</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleShortVersionString</key>
        <string>1.0</string>
        <key>CFBundleVersion</key>
        <string>1.0</string>
    
        <!-- Document Types -->
        <key>CFBundleDocumentTypes</key>
        <array>
            <dict>
                <key>CFBundleTypeName</key>
                <string>Test File</string>
                <key>CFBundleTypeRole</key>
                <string>Editor</string>
                <key>LSHandlerRank</key>
                <string>Owner</string>
                <key>LSItemContentTypes</key>
                <array>
                    <string>com.example.testfile</string>
                </array>
            </dict>
        </array>
    
        <key>UTExportedTypeDeclarations</key>
        <array>
            <dict>
                <key>UTTypeIdentifier</key>
                <string>com.example.testfile</string>
                <key>UTTypeDescription</key>
                <string>Test File</string>
                <key>UTTypeConformsTo</key>
                <array>
                    <string>public.data</string>
                </array>
                <key>UTTypeTagSpecification</key>
                <dict>
                    <key>public.filename-extension</key>
                    <array>
                        <string>testfile</string>
                    </array>
                    <key>public.mime-type</key>
                    <string>application/x-testfile</string>
                </dict>
            </dict>
        </array>
    
    
        <key>NSDocumentsFolderUsageDescription</key>
        <string>Description goes here</string>
    
        <key>LSApplicationQueriesSchemes</key>
        <array>
            <string>file</string>
        </array>
    </dict>
    </plist>
    
    

    我有一些超级简单的生锈代码:

    fn log_message(message: &str) {
        let mut log_file = OpenOptions::new()
            .create(true)
            .append(true)
            .open("/tmp/fileopener.log")
            .unwrap();
        writeln!(log_file, "{}", message).unwrap();
    }
    
    fn main() -> notify::Result<()> {
        let args: Vec<String> = env::args().collect();
        log_message(&format!("Received arguments: {:?}", args));
    
        if args.len() != 2 {
            eprintln!("Usage: {} <path to .testfile file>", args[0]);
            log_message("Incorrect number of arguments.");
            return Ok(());
        }
    }
    
    

    因此,每当调用二进制文件时,它都会将参数记录到tmp文件中,期望将文件路径的辅助参数传递给它。

    如果我跑/FileOpener/Contents/MacOS/FileOpener/path/to/file.testfile

    参数传递正确。

    如果我尝试将file.testfile拖动到FileOpener.app上,则file.testfile的路径不会作为参数传递,唯一的参数是FileOpener.app的路径。如果我尝试双击file.testfile,FileOpener.app确实会打开,但file.testfile的路径不会作为arg传递,只有arg是FileOpener.app的路径。

    我想做的是注册.testfile扩展名,这样当双击时,它会打开FileOpener.app,并将点击(或拖动)的文件的路径作为第二个参数传递。

    我也尝试过代码签名, sudo codesign --deep --force --sign - ./FileOpener.app ,将其复制到/Applications中,通过安全性启用全磁盘访问等。我需要的辅助参数,即单击的文件的路径,永远不会传入。

    1 回复  |  直到 1 年前
        1
  •  1
  •   Kevin Reid    1 年前

    macOS不会通过传入args来打开文档。相反,在应用程序事件循环开始后,它会收到打开文件的事件。

    (请注意,与Windows和Linux不同,但与Android和iOS一样,macOS默认具有 每个应用程序只有一个进程 ; 因此,当应用程序已经运行时,需要类似于此机制来处理打开的文件。但是,同样,macOS也一直以这种方式工作,因为它被称为Macintosh系统,没有Unix,也没有 argv 在下面)

    您需要启动一个事件循环,并在事件到达时进行处理。 tao 是一个看起来可以做到这一点的图书馆,但我自己还没有用过。

    推荐文章