How to tell an app's file navigator to go to a folder and select specific images?
I have this idea for how to get Affinity Photo 2 to create a focus stack document from selected "processed" images. I can successfully tell it to create the focus stack document that pops up a window where you "add" images to a list. I can tell it to click the "add" button. That opens a file navigator window. At that point I am stuck.
I tried sending CMD+SHIFT+G to have it ask for a folder path. It pops up the window. I am unsuccessful in sending it a string for the path. It only sends the first character.
In the script below where I "keystroke homeDir" it only sends the initial "/" of "/Users/<myusername>".
Any ideas?
on run
set theAppName to "Affinity Photo 2"
chooseMenuItem("Affinity Photo 2", "File", "New Stack…")
tell application "System Events"
set homeDir to (get POSIX path of (path to my home folder))
tell process theAppName
click button "Add" of window "New Stack"
keystroke "g" using {shift down, command down}
keystroke homeDir
end tell
end tell
end run
on chooseMenuItem(theAppName, theMenuName, theMenuItemName)
try
-- Bring the target app to the front
tell application theAppName
activate
end tell
-- Target the app
tell application "System Events"
tell process theAppName
-- Target the menu bar
tell menu bar 1
-- Target the menu by name
tell menu bar item theMenuName
tell menu theMenuName
-- Click the menu item
click menu item theMenuItemName
end tell
end tell
end tell
end tell
end tell
return true
on error
return false
end try
end chooseMenuItem
-
I figured out how to do this for Affinity Photo. If you write a simple Apple Script that "tells" system events to tell a specific application, then you can query that application's UI using "entire contents of ...". This will tell you the UI element hierarchy. If an app has a pop-up window, you can query just that window. If that window has a "sheet" you can query just that sheet of that window.
For example I was able to get the UI elements of the file list pop-up dialog within Affinity Photo after choosing the File > New Focus Merge… menu option and press the "Add" button that brings up the file chooser.
tell application "System Events"
tell process "Affinity Photo 2"
entire contents of sheet 1 of window "New Focus Merge"
end tell
end tellThis provided me valuable information about the UI elements of the file chooser making it easy to interact with it.
If you target a specific UI element you also can ask for its properties.
You also can query System Events for conditions about these elements such as whether they exist yet, whether a button is enabled (responds to "return"), etc. This makes it easier to build intelligent pauses into your scripts that wait for a specific UI condition to be true before it continues.
0
Please sign in to leave a comment.
Comments
1 comment