如何使用AppleScript在Mac上关闭文件选择器对话框?

发布于 2025-01-27 11:00:13 字数 252 浏览 5 评论 0原文

我的MacOS应用程序中有文件选择器,该应用程序可以通过AppleScript命令来工作:

choose file of type {"", "png", "jpeg", "jpg"} with prompt ""

我目前遇到的问题是用户可以一次打开多个文件选择器对话框。我正在寻找一种在打开新​​文件选择器对话框之前关闭所有打开的文件选择器对话框的方法,但我对此没有任何进展。 有什么方法可以关闭先前打开的文件选择器对话框?

I have file picker in my macos application that works through AppleScript command:

choose file of type {"", "png", "jpeg", "jpg"} with prompt ""

The problem I currently have is that users can open multiple file picker dialogs at once. I am looking for a way to close all of the opened file picker dialogs before opening a new file picker dialog but I have not no progress with this.
Is there any way to close previously opened file picker dialogs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清君侧 2025-02-03 11:00:13

以下脚本将关闭以前打开的应用程序所有实例的“选择文件”对话框。

set processName to "Script Editor" -- edit here the name of your app

tell application "System Events"
    repeat with aProcess in (processes whose name is processName)
        set frontmost of aProcess to true
        try
            click button "Cancel" of window "Choose a File" of aProcess
        end try
    end repeat
end tell

注意:要用“脚本编辑器”测试上面的脚本,您应该打开2个实例“脚本编辑器”(带有单个“选择文件” 代码行),然后打开“脚本编辑器”的第三个实例,上面的脚本并运行所有内容。首先,运行2首先创建实例。

您可以使用此辅助脚本打开脚本编辑器(或其他应用程序)的实例:

use framework "AppKit"
use framework "Foundation"
use scripting additions

set theApp to choose application
set appPath to POSIX path of (path to theApp)
set appURL to current application's class "NSURL"'s fileURLWithPath:appPath

set theWorkspace to current application's class "NSWorkspace"'s sharedWorkspace()
set startOptions to current application's class "NSWorkspaceOpenConfiguration"'s configuration()
set startOptions's activates to true
set startOptions's createsNewApplicationInstance to true
theWorkspace's openApplicationAtURL:appURL configuration:startOptions completionHandler:(missing value)

Following script will close "Choose File" dialogs of all instances of your app opened before.

set processName to "Script Editor" -- edit here the name of your app

tell application "System Events"
    repeat with aProcess in (processes whose name is processName)
        set frontmost of aProcess to true
        try
            click button "Cancel" of window "Choose a File" of aProcess
        end try
    end repeat
end tell

NOTE: to test the script above with "Script Editor", you should open 2 instances of "Script Editor" (with single "choose file" code line) then open 3rd instance of "Script Editor" with script above and run all of them. First, run 2 firstly created instances.

You can open the instances of Script Editor (or, other app) with this helper script:

use framework "AppKit"
use framework "Foundation"
use scripting additions

set theApp to choose application
set appPath to POSIX path of (path to theApp)
set appURL to current application's class "NSURL"'s fileURLWithPath:appPath

set theWorkspace to current application's class "NSWorkspace"'s sharedWorkspace()
set startOptions to current application's class "NSWorkspaceOpenConfiguration"'s configuration()
set startOptions's activates to true
set startOptions's createsNewApplicationInstance to true
theWorkspace's openApplicationAtURL:appURL configuration:startOptions completionHandler:(missing value)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文