如何通过AppleScript在Safari中安装扩展程序

时间:2014-07-07 15:39:08

标签: safari applescript

我需要在Safari中执行自动安装扩展。

现在我有了这部分代码:

property extension_list : {"safariextz"}
  on adding folder items to this_folder after receiving these_items
  try
    tell application "Finder"
      repeat with i from 1 to (number of items in these_items)
      set this_item to item i of these_items
      set item_extension to name extension of this_item 
        if item_extension = "safariextz" then
        tell application "Safari" to open this_item
      delete this_item
        end if
      end repeat
    end tell
on error errmsg
    display dialog errmsg buttons {"OK"} default button 1 
  end try
end adding folder items to

这个工作,文件在下载后运行。 但是我不能按下Install按钮来启动安装扩展。

我尝试了类似的东西

tell application "System Events"
  tell process "Safari"
    click the button "Install"
  end tell
end tell

但这没有用。 云请你帮我完成扩展安装脚本?

2 个答案:

答案 0 :(得分:2)

您必须指定如下窗口:click button 1 of window 1

打开" safariextz" Safari中的文件阻止了脚本,您必须使用ignoring application responses

脚本必须检查对话框是否显示

on adding folder items to this_folder after receiving these_items
    repeat with this_item in these_items
        if (this_item as string) ends with ".safariextz" then
            ignoring application responses
                tell application "Safari" to open this_item
            end ignoring
            tell application "System Events"
                tell process "Safari"
                    set frontmost to true
                    repeat until (exists window 1) and subrole of window 1 is "AXDialog" -- wait until the dialog is displayed.
                        delay 1
                    end repeat
                    click button 1 of front window -- install
                end tell
            end tell
        end if
    end repeat
end adding folder items to

更新:由于ignoring application responses不适合您,请尝试此操作

on adding folder items to this_folder after receiving these_items
        repeat with this_item in these_items
            if (this_item as string) ends with ".safariextz" then
                    tell application "Finder" to open this_item

答案 1 :(得分:1)

对于Safari 9,按钮的上下文已更改。这是我想出来让它恢复工作的脚本。 (这只是为了显示命令的顺序以及如何单击按钮。

tell application "Safari" to activate
delay 2 
tell application "System Events" 
    tell application process "Safari" 
        set frontmost to true
        tell application "Safari" to open location "/path/to/SafariDriver.safariextz" 
        delay 2 
        click button 1 of sheet 1 of window 1
    end tell 
end tell 
tell application "Safari" to quit
相关问题