获取Finder选择的文件的posix路径可以通过以下步骤实现:
tell application "Finder"
set selectedItems to selection
if (count of selectedItems) is 1 then
set selectedItem to item 1 of selectedItems
set itemPath to POSIX path of (selectedItem as alias)
return itemPath
else
return "请选择一个文件"
end if
end tell
以下是一些常见的编程语言和开发环境的示例代码:
NSAppleScript *script = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:@"/path/to/get_selected_file_path.scpt"] error:nil];
NSDictionary *errorInfo = nil;
NSAppleEventDescriptor *result = [script executeAndReturnError:&errorInfo];
if (!errorInfo) {
NSString *posixPath = [result stringValue];
NSLog(@"%@", posixPath);
} else {
NSLog(@"执行脚本出错:%@", errorInfo);
}
import Foundation
let scriptURL = URL(fileURLWithPath: "/path/to/get_selected_file_path.scpt")
var error: NSDictionary?
if let script = NSAppleScript(contentsOf: scriptURL, error: &error) {
if let output = script.executeAndReturnError(&error).stringValue {
print(output)
} else if let error = error {
print("执行脚本出错:\(error)")
}
} else if let error = error {
print("加载脚本出错:\(error)")
}
osascript
命令执行脚本):import subprocess
script_path = "/path/to/get_selected_file_path.scpt"
result = subprocess.run(["osascript", script_path], capture_output=True, text=True)
if result.returncode == 0:
posix_path = result.stdout.strip()
print(posix_path)
else:
print(f"执行脚本出错:{result.stderr}")
请注意,上述示例代码中的"/path/to/get_selected_file_path.scpt"应替换为实际保存脚本的路径。
这样,你就可以通过执行上述代码来获取Finder选择的文件的posix路径了。
没有搜到相关的文章