【Mac】Safariブラウザで複数ページのスライド画像をキャプチャする方法

Safariブラウザで複数ページのスライド画像をキャプチャする方法。

左右のキーを押して画像が切り替わる画面を想定している。

前回書いた記事を参考にして、

↓以下のHTMLをsafariブラウザで開く

<html>
<head>
    <script>
        window.onload = function() {
            setResizeWidth(1000);
            setResizeHeight(1100);
        }

        function onChangeResize(vh_type){
            if (vh_type == "h"){
                var w = document.getElementById("txt_horizontal").value;
                setResizeWidth(parseInt(w));
            }
            if (vh_type == "v"){
                var h = document.getElementById("txt_vertical").value;
                setResizeHeight(parseInt(h));
            }
        }

        function onClickResize(pixel,vh_type){
            if (vh_type == "h"){
                var w = document.getElementById("frm").width;
                setResizeWidth(parseInt(w) + pixel);
            }
            if (vh_type == "v"){
                var h = document.getElementById("frm").height;
                setResizeHeight(parseInt(h) + pixel);
            }
        }

        function setResizeWidth(w){
            document.getElementById("frm").width = w;
            document.getElementById("txt_horizontal").value = w;
        }

        function setResizeHeight(h){
            document.getElementById("frm").height = h;
            document.getElementById("txt_vertical").value = h;            
        }

    </script>
</head>
<body>

縦幅:<input type="button" value="-10" onclick="onClickResize(-10,'v');"/><input type="button" value="-1" onclick="onClickResize(-1,'v');"/><input id="txt_vertical" type="text" onChange="onChangeResize('v');"><input type="button" value="+1" onclick="onClickResize(1,'v');"/><input type="button" value="+10" onclick="onClickResize(10,'v');"/>
<br/>
横幅:<input type="button" value="-10" onclick="onClickResize(-10,'h');"/><input type="button" value="-1" onclick="onClickResize(-1,'h');"/><input id="txt_horizontal" type="text" onChange="onChangeResize('h');"><input type="button" value="+1" onclick="onClickResize(1,'h');"/><input type="button" value="+10" onclick="onClickResize(10,'h');"/>

<br/>
<iframe id="frm" src="【サイトURL】" scrolling="no" frameborder="no">
</body>
</html>

キャプチャしたいサイズを調整する。

Command + Shift + 『5』を使って、1度、範囲指定キャプチャをしておく。

(※【オプション】から 【最後の選択部分を記憶】にチェックを入れる)

以下のAppleScript で、連続キャプチャ開始。

on run argv

    display dialog "スクリーンショットを撮る画面数:" default answer "1"
    set number_shots to text returned of the result

    set read_lr to display dialog "左読み or 右読み" buttons {"左読み", "右読み"}
    set read_lr_key to 0
    set read_lr_label to button returned of read_lr

    tell application "Safari"
        activate
    end tell

    delay 1 -- アプリケーションが切り替わるのを待つ

    repeat number_shots times

        my sendCommandShift5()

        delay 0.1

        my sendKeyEnter()

        delay 0.5

        tell application "Safari"
            activate
            my sendKeyArrow(read_lr_label)
        end tell

        delay 0.5 -- Safariのページが更新されるのを待つ

    end repeat

end run

on sendKeyArrow(read_lr_label)

    if (offset of "左読み" in read_lr_label) > 0 then
        my sendKeyLeft()
    end if

    if (offset of "右読み" in read_lr_label) > 0 then
        my sendKeyRight()
    end if

end sendKeyArrow

on sendKeyEnter()
    tell application "System Events"
        key code 76 -- Enter
    end tell
end sendKeyEnter

on sendKeyLeft()
    tell application "System Events"
        key code 123 -- 左矢印キー(←)
    end tell
end sendKeyLeft

on sendKeyRight()
    tell application "System Events"
        key code 124 -- 右矢印キー(→)
    end tell
end sendKeyRight

on sendCommandShift5()
    tell application "System Events"
        key code 23 using {command down, shift down}
    end tell
end sendCommandShift5

自動的に、左右どちらかの矢印キーを押しながら、画面をキャプチャしていく。

参考サイト