使用 AppleScript 在 Safari 中选择文件 [英] Using AppleScript to choose a file in Safari
问题描述
我正在尝试编写一些自动化代码(主要使用 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.
推荐答案
更多的搜索,我在这里找到了一个很好的答案:带有 UI 脚本的 Applescript 文件对话框
Some more searching and I found a great answer here: 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
结束运行
这篇关于使用 AppleScript 在 Safari 中选择文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!