OS X 10.10中的AppleScript调整Safari窗口大小并拍摄该窗口的屏幕截图并重复多种尺寸


1

我想构建一个AppleScript应用程序,在运行时将执行以下操作:

  1. 打开Safari并导航到特定URL或行分隔文件或CSV。这不是必需的,但可以更快地生成不同页面的多个屏幕截图。

  2. 将Safari窗口的大小调整为多种尺寸之一

  3. 获取Safari窗口的屏幕截图,其中文件名为url以及屏幕大小

Answers:


2

我只得到了一个部分解决方案,除了一些小问题之外。

首先,您不能将URL存储在文件名中(由于斜杠和特殊字符),因此您需要指定网站名称。

但更重要的是,到目前为止还没有实现CSV导入。CSV真的是一个很难的要求,或者你能获得不同格式的URL列表吗?如果没有,请使用CSV文件的示例行更新您的问题。我会继续努力并尽快更新我的答案。

on open_url(theUrl, theUrlName, x0, y0, xSize, ySize)
    tell application "Safari"
        open location theUrl
        activate

        set bounds of window 1 to {x0, y0, xSize, ySize}
        set windowID to id of window 1
        set the_state to missing value

        repeat until the_state is "complete"
            set the_state to (do JavaScript "document.readyState" in document 1)
            delay 0.3
        end repeat

        set theFolder to POSIX path of (path to desktop as string)
        set shellCommand to "/usr/sbin/screencapture -l " & windowID & " " & quoted form of (theFolder & "Screen Shot " & theUrlName & " " & xSize & "x" & ySize & ".png")
        do shell script shellCommand

    end tell
end open_url


set resolutionList to {{640, 480}, {1024, 768}}
set siteList to {{"http://www.apple.com", "Apple"}, {"http://www.google.com", "Google"}}

repeat with resolution in resolutionList
    set xSize to item 1 of resolution
    set ySize to item 2 of resolution
    repeat with theSite in siteList
        set theUrl to item 1 of theSite
        set theUrlName to item 2 of theSite
        open_url(theUrl, theUrlName, 0, 0, xSize, ySize)
    end repeat
end repeat

那是完美的谢谢你。只是一件小事,截图在页面加载之前拍得太快。我增加了延迟。此外,有没有办法告诉Safari模仿它,以便它认为它是iPhone或iPad?
迈克

是的,延迟部分很棘手。我试图驻留在JavaScript的readystate中,但是在浏览器完全呈现页面之前已经完成了很多次。要更改用户代理,您可以尝试在此处发布AppleScript(注意:我没有测试它):
Asmus 2015年

0

在进行屏幕截图之前,最好检查页面是否已完成加载。以Asmus的原始部分答案代码为基础......

delay 10

tell application "Safari"
    do JavaScript "document.readyState == 'complete'" in window 1's current tab
end tell
set loaded to result
if loaded is true then
    set shellCommand to "/usr/sbin/screencapture -l " & windowID & " " & quoted form of (theFolder & "Screen Shot " & theUrlName & " " & xSize & "x" & ySize & ".png")
    do shell script shellCommand
end if

...将负责它。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.