使用 AppleScript 在 Safari 中选择文件

发布于 2024-10-20 12:17:13 字数 196 浏览 3 评论 0原文

我正在尝试编写一些自动化代码(主要是用 Ruby Selenium 编写)。有时,Safari 中会打开文件选择器,以便用户可以选择要上传的文件。 Selenium 无法处理这个问题,但我认为 AppleScript 应该可以。我是 AppleScript 新手,无法找到自动执行文件选择器对话框的任何样板代码。我正在阅读 AppleScript 文档,但任何想法都会很有帮助。

I am trying to write some automation code (primarily in Ruby Selenium). At some point, a file chooser is opened in Safari so that the user can select a file for upload. Selenium cannot handle this, but I think AppleScript should be able to. I am new to AppleScript and haven't been able to find any boilerplate code of someone automating a file chooser dialog. I'm reading through the AppleScript docs, but any ideas would be most helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雄赳赳气昂昂 2024-10-27 12:17:13

更多搜索,我在这里找到了一个很好的答案:带有 UI 脚本的 Applescript 文件对话框

这是我最终使用的:

on run argv
tell application "Safari"
    activate

    -- Usage check
    set argc to count argv
    if argc is not greater than 0 then
        return "Usage: SafariFileChooser file_name [window_name]"
    end if

    -- The file we will choose to open
    set file_name to item 1 of argv

    -- Flip to the named window, if specified
    if argc is equal to 2 then
        set window_name to item 2 of argv
        set flip_count to index of window window_name
        repeat (flip_count - 1) times
            activate
            tell application "System Events" to keystroke "`" using command down
        end repeat
    end if

    -- Interact with the dialog using System Events (thanks mcgrailm)
    tell front window
        activate
        tell application "System Events"
            keystroke "g" using {shift down, command down}
            keystroke file_name
            delay 1
            keystroke return
            delay 1
            keystroke return
        end tell
    end tell
end tell
return 0

end run

Some more searching and I found a great answer here: Applescript file dialog with UI scripting

Here's what I ended up using:

on run argv
tell application "Safari"
    activate

    -- Usage check
    set argc to count argv
    if argc is not greater than 0 then
        return "Usage: SafariFileChooser file_name [window_name]"
    end if

    -- The file we will choose to open
    set file_name to item 1 of argv

    -- Flip to the named window, if specified
    if argc is equal to 2 then
        set window_name to item 2 of argv
        set flip_count to index of window window_name
        repeat (flip_count - 1) times
            activate
            tell application "System Events" to keystroke "`" using command down
        end repeat
    end if

    -- Interact with the dialog using System Events (thanks mcgrailm)
    tell front window
        activate
        tell application "System Events"
            keystroke "g" using {shift down, command down}
            keystroke file_name
            delay 1
            keystroke return
            delay 1
            keystroke return
        end tell
    end tell
end tell
return 0

end run

花间憩 2024-10-27 12:17:13

我刚刚发现的另一个选项是使用命令行指定目录:

    do shell script "defaults write com.apple.Safari NSNavLastRootDirectory /path/to/directory"

这样您可以在 UI 脚本编写中稍微减少一些工作。在打开文件选择器之前运行此命令,它将把您置于指定的目录中。在此目录中包含您需要的所有文件,您只需编写命令 +a 即可将它们全部选中,然后返回。

Another option I just discovered is to specify the directory using the command-line:

    do shell script "defaults write com.apple.Safari NSNavLastRootDirectory /path/to/directory"

This way you can do slightly less in UI scripting. Run this command before you open the file chooser, and it will put you into the directory specified. Include all the files you need in this directory, and you can just script command+a to select them all, and return.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文