簡體   English   中英

macOS:如何從代碼中打開“打開方式”菜單?

[英]macOS: How to open the “Open with” menue from code?

在 macOS 上的 Finder 中,文件的上下文菜單中有“使用 > 打開”選項,它顯示了一個菜單,其中所有可用的應用程序都可以打開文件。 因此,用戶可以選擇必須使用哪個應用程序打開文件。

我想從特定文件的代碼中打開此菜單。 在 Windows 它是這樣的

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

但我不知道這在 macOS 中是如何進行的。

也許您可以按照 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

腳步:

  1. 按 command-space 啟動終端 --> 輸入:終端
  2. 當終端運行時,您切換到應用程序文件夾:'cd Applications'
  3. 您可以像這樣運行應用程序: open -a GIMP-2.10.app

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM