使用AppleScript选择在Safari中的文件 [英] Using AppleScript to choose a file in Safari
问题描述
我试图写一些自动化code(主要在Ruby中硒)。在某些时候,一个文件选择器在Safari打开,使得用户可以选择要上传的文件。硒能不能处理这个问题,但我认为AppleScript的应该可以。我是新来的AppleScript和一直无法找人自动文件选择对话框中的任何样板code。我读通过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.
推荐答案
更多的一些搜索,我发现了一个伟大的答案在这里:<一href=\"http://stackoverflow.com/questions/2473091/applescript-file-dialog-with-ui-scripting\">Applescript与UI脚本文件对话框
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屋!