Home > Software engineering >  AppleScript - How to set the target/path of a finder window prompt when it's prompted from an A
AppleScript - How to set the target/path of a finder window prompt when it's prompted from an A

Time:10-16

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.

CodePudding user response:

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.

CodePudding user response:

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
  • Related