'How to close file picker dialog on mac with AppleScript?
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?
Solution 1:[1]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
