AppleScript - 如何在从应用程序(而不是从 Finder)提示时设置 finder window 提示的 target/path?

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

我正在使用 AppleScript 在 GUI 上自动执行流程。

我使用脚本在 GUI 中单击“添加源文件夹”按钮(类似于在任何应用程序中打开文件),这会提示集成的 Finder window (image here)。

实际上,一旦提示查找器 window 打开,我希望从别名对象 theFolderToProcess 自动设置路径(之前已经在脚本)。

我无法设置它的路径,因为我无法在应用内提示查找器 window 中设置别名 theFolderToProcess。那么如何让脚本导航到别名的路径?

代码如下:

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

使用 UI Elements,我得到 sheet 1 of window "MyApp" of application process "MyApp" of application "System Events" 其中 sheet 1 是提示符 window。

注意:在提示window中设置路径无效。

在 'open' 对话框中,您应该可以使用 Finder 的 Go to Folder… 功能,或 command-shift-G(或 OS 上的任何功能)。然后使用 keystroke 输入您想要的位置。位置参考应遵循以下格式,而不是使用您的别名。跟进 key code 以接受您的位置。我发现短暂的延迟有助于键盘脚本,但可以根据需要进行编辑。

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)

有一个完整的例子但这应该足够了。

A 不知道你的“App”,所以下面的例子是使用 TextEdit。如果您使用其他方式成功打开 OPEN 对话框(单击按钮“添加源文件夹”...),然后用您的替换我的脚本的相应代码行。我的脚本还展示了如何设置自动延迟。以及最后如何点击“开始”按钮。

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