How to close file picker dialog on mac with AppleScript? - macos

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?

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)

Related

AppleScript - How to set the target/path of a finder window prompt when it's prompted from an App (not from the Finder)?

I am using AppleScript to automate a process on a GUI.
I use the script to click a button "Add Source Folder" (similar to opening a file in any apps) in the GUI, which prompts an integrated Finder window (image here).
In practice, once the prompted finder window opened, I'd like the path to be set automatically from an alias object theFolderToProcess's (which would've been set previously in the script).
I am unable to set its path because I have nowhere to set the alias theFolderToProcess within the in-app prompt finder window. So how do I get the script to navigate to the path of the alias?
Here is the code:
set theFolderToProcess to choose folder with prompt "Step 1: Select the folder to convert:"
tell application "System Events"
tell application process "MyApp"
tell its window "MyApp"
activate
click button "Add Source Folder"
set uiElems to UI elements
end tell
end tell
end tell
With UI Elements, I get sheet 1 of window "MyApp" of application process "MyApp" of application "System Events" where sheet 1 is the prompt window.
Note: Setting the path in the prompt window doesn't work.
From within the 'open' dialogue, you should be able to use the Finder's Go to Folder… function, or command-shift-G (or whatever it is on your OS). Then use keystroke to enter your desired location. The location reference should follow the format below rather than using your alias. Follow up with a key code to accept your location. I find the short delays help with keyboard scripting but edit as desired.
set posixFolder to posix path of theFolderToProcess
--> "~/Desktop/exports/"
delay 0.1
key code 5 using {command down, shift down} -- Go to folder…
delay 0.1
keystroke posixFolder -- folder must exist
--> keystroke "~/Desktop/exports/" -- folder must exist
delay 0.1
key code 76 -- type Return (Go button)
There is a full example here but this should be sufficient.
A don't know your "App", so following example is using TextEdit. If you successfully open OPEN dialog using other way (click button "Add Source Folder"...) then replace corresponding code line of my script with yours. My script shows how to set automated delays as well. And how to click the "Go" button at the end.
set processName to "TexEdit"
set sourceFolder to POSIX path of (path to pictures folder)
tell application processName to activate -- bring to front
tell application "System Events"
-- this is instead of your 'click button "Add Source Folder"'
-- it opens OPEN dialog window
keystroke "o" using command down
-- wait
repeat until window "Open" of process processName exists
delay 0.02
end repeat
-- open go to path sheet
keystroke "g" using {command down, shift down}
repeat until sheet 1 of window "Open" of process processName exists
delay 0.02
end repeat
-- keystroke full posix path
keystroke sourceFolder
-- go
click button "Go" of sheet 1 of window "Open" of process processName
end tell

Show/hide Finder, and create new window if none exist via AppleScript

I'd like to create an AppleScript to trigger via key command in Keyboard Maestro that allows me to toggle showing or hiding the Finder window(s) directly. And if when toggling to show the Finder, if there are no existing windows, create one and open it up to my home directory.
The following AppleScript works. However, there seems to be a race condition between activating the finder and detecting if there are any open windows with if not (window 1 exists), hence the delay 0.5.
The problem (which I think is a race condition in detecting the presence of an existing Finder window) results in this script frequently creating new Finder windows when one already existed. The if not (window 1 exists) doesn't always get it right.
Any thoughts, tweaks, or affirmations that this is just the way it is would be appreciated!
tell application "System Events"
set activeApp to name of application processes whose frontmost is true
if ((activeApp as string) is equal to "Finder") then
set visible of process "Finder" to false
else
tell application "Finder"
activate
delay 0.5
if not (window 1 exists) then
make new Finder window
set thePath to POSIX file "/Users/jon"
set the target of the front Finder window to folder thePath
end if
end tell
end if
end tell
Please try this simpler syntax, it uses only Finder terminology
tell application "Finder"
if frontmost then
set visible of process "Finder" to false
else
if (count windows) is 0 then reveal home
activate
end if
end tell
Edit:
To run a Keyboard Maestro macro, open Keyboard Maestro Editor, select the macro and then select Copy as > Copy UUID from the Edit menu.
Then in AppleScript write
tell application "Keyboard Maestro Engine" to do script "<Script-UUID>"
replacing <Script-UUID> with the copied real UUID
Ultimately I needed to activate Finder before running the count windows command or I'd get inconsistent window counts. Sometimes it'd be 0 even when there was a window open already. This code is working well for me so far.
tell application "Finder"
if frontmost then
set visible of process "Finder" to false
else
activate
if (count windows) is 0 then
open home
tell application "Keyboard Maestro Engine" to do script "<Script-UUID>"
end if
end if
end tell

With Applescript I need to use System Events to navigate a Finder Window that opens within an application

I'm currently writing some Applescript to import some material into software which doesn't have much applescript support and virtually no documentation on Applescript. The two methods are to somehow adapt this script to work:
on adding folder items to this_folder after receiving these_items
try
tell application "Isadora"
import media into document 1 from these_items
end tell
on error msg
display dialog "Error importing file into Isadora: " & msg
end try
end adding folder items to
However when I try to use "import media into document 1 from ________" I always get an error, in every incarnation of that combination.
My second approach then is using System Events to navigate through the menu bar to import everything. When I get to the import part of things where I can navigate and select the files, a finder window pops up. My question is:
How do I navigate this finder window within the application? This is like the inception of scripts. A finder window within a program.
I tried a few simple things like calling the front window of finder to navigate somewhere and what not. The current script that gets me to the import window is:
tell application "IsadoraCore"
activate
delay 2
tell application "System Events"
click menu bar item "File" of menu bar 1 of application process "IsadoraCore"
click menu item "Import Media..." of menu "File" of menu bar item "File" of menu bar 1 of application process "IsadoraCore"
end tell
end tell
Any help would be appreciated!
I would guess the "import media" window is a Finder window where you select a file to import. This window is easy to handle. To select the file you must have the posix path of the file. In that window if you keystroke "shift-command-g" then you get a text field where you can keystroke the posix path. For example, let's say I want to open a file in TextEdit. The file is on my desktop with the name test.txt. I can do this...
set filePath to (path to desktop as text) & "test.txt"
set posixPath to POSIX path of filePath
set aShortDelay to 0.5
tell application "TextEdit" to activate
delay aShortDelay
tell application "System Events"
keystroke "o" using command down -- bring up the "open dialog window"
end tell
delay aShortDelay
tell application "TextEdit" to activate
delay aShortDelay
tell application "System Events"
keystroke "g" using {shift down, command down} -- bring up the "goto folder sheet" in the open dialog window
delay aShortDelay
keystroke posixPath -- enter the posix file path
delay aShortDelay
keystroke return -- dismiss the "goto folder sheet"
delay aShortDelay
keystroke return -- dismiss the "open dialog window"
end tell
Notice I have delays after every command. That is good practice when you're using this type of scripting. You don't want the code to run faster than the computer can perform the tasks so the delay gives the computer time to keep up with the code. Notice that I use a variable for the delay. This way I can play with aShortDelay making it longer or shorter as needed to make the script run properly.
Good luck.
Aren’t you asking Isadora to import a list of folders? Perhaps it can only handle one folder at a time?
I don’t have Isadora to test, but you might try this:
on adding folder items to this_folder after receiving these_items
try
tell application "Isadora"
repeat with the_item in these_items
import media into document 1 from the_item
end repeat
end tell
on error msg
display dialog "Error importing file into Isadora: " & msg
end try
end adding folder items to

Opening programs in applescript using "Choose file"

I have an application were the user chooses an application to send a command to. but I keep getting an error message when I run it.
set setApp to choose file
tell application [setApp]
end tell
There's a special dialog for choosing an application:
set theApp to choose application
tell theApp to activate

AppleScript Clicking On dialog box

In PCSX, (ps1 emulator), i'm trying to automate the steps to play an iso. So, i'm doing this:
set thepath to path to me
set thesecondpath to POSIX path of thepath
set thethirdpath to "Contents/PSX/ROMS/img.bin"
set thefourthpath to "/Contents/PSX/PCSX.app"
set thefifthpath to thesecondpath & thefourthpath
set theultimatepath to thesecondpath & thethirdpath
tell application thefifthpath
activate
tell application "System Events"
keystroke "i" using {command down}
keystroke theultimatepath
delay 1.0
tell process "PCSX"
click button "Go"
end tell
key code 53
end tell
end tell
Running from the AppleScript Editor won't work. I made it to work running from the App it creates. PCSX and the img.bin are inside the Generated Package.
after pressing command+i, it opens a "Go to the folder" dialog, and i need to click Go and then Open
But doing this way, it won't find the dialog box. What am i doing wrong?
If Go and Open are the default buttons, try:
tell application "System Events"
keystroke return
delay 2
keystroke return
end tell
Although I don't have PCX installed here is an example of how to click the Go button from Finder's Go to Folder command.
tell application "System Events"
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
end tell
The reason your script won’t work from AppleScript Editor is that the “me” in “path to me” is the application that ran the AppleScript. When you are running the AppleScript in AppleScript Editor, that means AppleScript Editor itself. When you saved your AppleScript as a script application and ran it, the path to me pointed to your script application, because it was running its own AppleScript.
Also, this is incorrect:
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
The “Go” button is not on the window “Go to Folder.” It’s on a sheet which is attached to a Finder window which has the name of whatever folder is currently being viewed. So you have to describe the button as being on sheet 1 of window 1:
tell application "System Events"
tell process "Finder"
click button "Go" of sheet 1 of window 1
end tell
end tell
… but keep in mind that in another app, a similar looking button on a sheet may be on sheet 1 of group 1 of group 2 of window 3. UI Scripting is complicated.

Resources