Search code examples
user-interfaceapplescript

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.


Solution

  • 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