2014-07-07 66 views
2

我需要在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 

这工作,文件下载后运行。 但我无法按开始安装扩展按钮安装。

我想类似的东西

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

但是,这并没有帮助。 云你请帮我完成扩展安装的脚本?

回答

2

您必须指定这样的窗口:click button 1 of window 1

在Safari中打开块的“safariextz”文件中的脚本,你必须使用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 
+0

谢谢您的帮助! 我检查了辅助功能检查器中的按钮,它的编号为3. 所以我更新了这一行:单击前窗的按钮3 - 安装 但不幸的是,这并没有帮助,我仍然没有按脚本按钮。 我一直在等待约1分钟,然后显示“Safari出错:AppleEvent超时”。 它可能与忽略过程有关吗?在我的脚本中可能会出现错误,并且在代码之后不会继续:将应用程序“Safari”打开以打开this_item – Ellina

+0

“GUI脚本”取决于操作系统和Safari的版本。 我的脚本适用于Safari 7.0.5和OS X 10.9.4 – jackjr300

+0

在这里,我只有两个按钮在这个窗口中。试试这个:在'tell process'之后添加'set frontmost to true'Safari'' – jackjr300

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 
相关问题