如何告诉Applescript使用新的CMD + Shift + 5方法录制屏幕



我正在尝试自动录制我创建的程序的屏幕。我的AppleScript打开该程序,该程序会调整其大小并将其放置在主屏幕的中央。现在我想在操作程序之前开始录制。

我想延迟开始录制,仅捕获程序窗口所在的区域并将其保存在预定义的位置。如果可能的话,我想在录制运行时隐藏或最小化所有其他窗口。

主屏幕大小和屏幕中心由以下脚本完成:

set {mainScreenWidth, mainScreenHeight, mainScreenScale} to words of (do shell script "system_profiler SPDisplaysDataType | awk '/Main Display: Yes/{found=1} /Resolution/{width=$2; height=$4} /Retina/{scale=($2 == "Yes" ? 2 : 1)} /^ {8}[^ ]+/{if(found) {exit}; scale=1} END{printf "%d %d %d\n", width, height, scale}'")
-- .....
set windowWidth to 1000
set windowHeight to 700
set windowPosX to round (mainScreenWidth - windowWidth) / 2 as integer
set windowPosY to round (mainScreenHeight - windowHeight) / 2 - 50 as integer
set the bounds of theWindow to {windowPosX, windowPosY, windowWidth + windowPosX, windowHeight + windowPosY}

使用screencapture命令找到了一种方法:

-- screencapture -R x,y,width,height -V 10 file_name
set captureOffset to 46
set captureScript to "screencapture -d -R " & windowPosX & "," & (windowPosY + captureOffset) & "," & windowWidth & "," & (windowHeight - captureOffset) & " -V 6 rec.mov"
log captureScript
do shell script captureScript

这是一个完整的脚本,应该做你想做的事(根据上面的muuvmuuv回答(

(* set properties *)
set {mainScreenWidth, mainScreenHeight, mainScreenScale} to words of (do shell script "system_profiler SPDisplaysDataType | awk '/Main Display: Yes/{found=1} /Resolution/{width=$2; height=$4} /Retina/{scale=($2 == "Yes" ? 2 : 1)} /^ {8}[^ ]+/{if(found) {exit}; scale=1} END{printf "%d %d %d\n", width, height, scale}'")
set windowWidth to 1000
set windowHeight to 700
set recordedApp to "App Name"
set saveFilePath to quoted form of "/path/to/some/file.mov"
set windowMode to true -- false is full screen mode, true is window-only mode
set showCursor to true -- true value shows cursor movement, false hides it
-- length of recording in secoonds
set recordingDuration to 10
-- seconds to delay before beginning
set startDelay to 3
(* begin *)
tell application "System Events"
    (* delete old save file, otherwise capture will fail *)
    if exists saveFilePath then
        do shell script "rm -f " & saveFilePath
    end if
    (* hide other apps *)
    set procs to get (every application process whose visible is true)
    repeat with thisProc in procs
        tell thisProc
            if name is not recordedApp then
                set visible to false
            else
                set frontmost to true
            end if
        end tell
    end repeat
    (* center app window *)
    tell application process recordedApp
        tell first window
            set position to {round (mainScreenWidth - windowWidth) / 2, round (mainScreenHeight - windowHeight) / 2}
            set size to {windowWidth, windowHeight}
            set captureSize to my captureSizeString(position & size)
            log captureSize
        end tell
    end tell
end tell
set captureScript to "screencapture -v -T " & startDelay & " -V " & recordingDuration
if windowMode then
    -- window-only mode
    set captureScript to captureScript & " -R " & captureSize
else
    -- full screen mode
    set captureScript to captureScript & " -m"
end if
if showCursor then
    -- show Cursor
    set captureScript to captureScript & " -C"
end if
-- specify output file
set captureScript to captureScript & " " & saveFilePath
(* 
    this following phrase is added to detach the shell script and return its 
    process ID. The process ID can be used to refer to screencapture process
    later in the script (I've used it to set up a loop that checks to see if 
    the process is still running, so that I can run a clean-up routine). The 
    script will release the screencapture process immediately and move on to
    the next command.
    *)
set captureScript to captureScript & " &> /dev/null & echo $!"
set processID to do shell script captureScript
(* demonstration shell script *)
do shell script "say Hello -v Amelie"
(* repeat loop that tests to see if the screencapture session has ended *)
tell application "System Events"
    repeat while (first process whose unix id is processID) exists
        delay 0.5
    end repeat
    my cleanUp()
end tell
on cleanUp()
    (* recording done, do clean up *)
    say "recording done"
    tell application "System Events"
        (* re-show hidden apps *)
        repeat with thisProc in procs
            tell thisProc
                set visible to true
            end tell
        end repeat
    end tell
end cleanUp
on captureSizeString(boundsArray)
    set tid to my text item delimiters
    set my text item delimiters to ","
    set s to boundsArray as text
    set my text item delimiters to tid
    return s
end captureSizeString

在顶部的"属性"部分中设置各种必要的内容(您正在录制的应用程序的名称,输出的文件路径,录制持续时间等(,然后运行它。

recordingDuration 属性设置为足够大的值。 screencapture需要参数 - 它不能设置为无限期运行 - 但您可以随时通过单击命令-控制-逃脱来结束屏幕录制过程,因此请确保数字足够大,以使该过程不会提前结束。

如果您需要执行比在启动后运行一些命令更复杂的操作 screencapture ,您可能希望将其切换到具有idle处理程序的保持打开小程序。但这超出了这个问题的范围。

最新更新