我有一个名为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中,通过安全性启用全磁盘访问等。我需要的辅助参数,即单击的文件的路径,永远不会传入。