【问题标题】:macOS: How to open the "Open with" menue from code?macOS:如何从代码中打开“打开方式”菜单?
【发布时间】:2021-01-04 20:33:14
【问题描述】:

在 macOS 的 Finder 中,文件的上下文菜单中有“使用 > 打开”选项,它显示了一个菜单,其中所有可用的应用程序都可以打开文件。因此,用户可以选择必须使用哪个应用程序打开文件。

我想从特定文件的代码中打开此菜单。 在 Windows 中是这样的

Process.Start("rundll32.exe", "shell32.dll, OpenAs_RunDLL " + file);

但我不知道这在 macOS 中是如何进行的。

【问题讨论】:

  • 您是在询问如何在您正在编写的应用程序中显示菜单,还是希望在您未编写的现有应用程序中以编程方式激活“打开方式...”菜单?也就是说,您是在构建用户界面还是尝试操作现有应用的 UI?
  • “您是否询问如何在您正在编写的应用程序中显示菜单/您是否正在构建用户界面” - 是的

标签: macos finder


【解决方案1】:

也许您可以根据自己的需要调整以下 AppleScript 代码。

如果您在 Finder 中选择了一个文件,并在 Script Editor.app 中运行以下代码,它将打开一个对话框供您选择一个新应用程序来打开您选择的 Finder 文件,然后将打开该文件您选择的应用。

activate
set chosenApp to (choose application with prompt ¬
    "Choose  Your Preferred Default Application" as alias)

tell application "Finder"
    set thisFile to selection -- The Selected File In Front Finder Window
    set appID to id of chosenApp
    try
        open thisFile as text using application file id appID
    on error errMsg number errNum
        display dialog "Please Go Back And Select A File In Finder To Be Opened." & linefeed & ¬
            "Then Come Back And Run This Code Again" buttons {"Cancel", "OK"} default button "OK"
    end try
end tell

【讨论】:

  • 谢谢,将尝试找出,如何从代码中调用open thisFile as text using application file id appID
【解决方案2】:

步骤:

  1. 按命令空格启动终端 --> 输入:终端
  2. 当终端运行时,您切换到应用程序文件夹: 'cd 应用程序'
  3. 您可以像这样运行应用程序:open -a GIMP-2.10.app

更多信息: https://osxdaily.com/2007/02/01/how-to-launch-gui-applications-from-the-terminal/

【讨论】:

  • OP 想从他的代码而不是终端显示“打开方式...”菜单。
  • 我的错,我忽略了从代码部分运行,并认为目的是从命令行启动应用程序(作为“代码”而不是通过 GUI)。不会再发生了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多