gpt4 book ai didi

macos - 使用 AppleScript 在 Safari 中选择文件

转载 作者:行者123 更新时间:2023-12-02 12:38:04 25 4
gpt4 key购买 nike

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

最佳答案

更多搜索,我在这里找到了一个很好的答案:Applescript file dialog with UI scripting

这是我最终使用的:

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

结束运行

关于macos - 使用 AppleScript 在 Safari 中选择文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5170142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com