如何告诉 applescript 使用新的 CMD+Shift+5 方法录制屏幕?
How to tell applescript to record the screen with the new CMD+Shift+5 method?
我正在尝试自动录制我创建的程序的屏幕。我的 AppleScript 打开该程序,调整其大小并将其放置在主屏幕的中央。现在我想在操作程序之前开始录制。
我想延迟开始录制,只捕获我的程序window所在的区域并将其保存在预定义的位置。如果可能的话,我想在录音 运行.
时隐藏或最小化所有其他 windows
主屏幕尺寸和屏幕居中由这个脚本完成:
set {mainScreenWidth, mainScreenHeight, mainScreenScale} to words of (do shell script "system_profiler SPDisplaysDataType | awk '/Main Display: Yes/{found=1} /Resolution/{width=; height=} /Retina/{scale=( == \"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=; height=} /Retina/{scale=( == \"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
在顶部的 'properties' 部分设置各种必要的东西(您正在录制的应用程序的名称、输出的文件路径、录制持续时间等),然后 运行它。
将 recordingDuration
属性 设置为足够大的值。 screencapture
需要参数——它不能无限期地设置为 运行——但是你可以随时通过单击 Command-Control-Escape 来结束屏幕录制过程,因此请确保数字足够大该过程不会提前结束。
如果您需要在开始 screencapture
后执行一些比 运行 更复杂的操作,您可能需要将其切换为带有 idle
处理程序。但这超出了这个问题的范围。
我正在尝试自动录制我创建的程序的屏幕。我的 AppleScript 打开该程序,调整其大小并将其放置在主屏幕的中央。现在我想在操作程序之前开始录制。
我想延迟开始录制,只捕获我的程序window所在的区域并将其保存在预定义的位置。如果可能的话,我想在录音 运行.
时隐藏或最小化所有其他 windows主屏幕尺寸和屏幕居中由这个脚本完成:
set {mainScreenWidth, mainScreenHeight, mainScreenScale} to words of (do shell script "system_profiler SPDisplaysDataType | awk '/Main Display: Yes/{found=1} /Resolution/{width=; height=} /Retina/{scale=( == \"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=; height=} /Retina/{scale=( == \"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
在顶部的 'properties' 部分设置各种必要的东西(您正在录制的应用程序的名称、输出的文件路径、录制持续时间等),然后 运行它。
将 recordingDuration
属性 设置为足够大的值。 screencapture
需要参数——它不能无限期地设置为 运行——但是你可以随时通过单击 Command-Control-Escape 来结束屏幕录制过程,因此请确保数字足够大该过程不会提前结束。
如果您需要在开始 screencapture
后执行一些比 运行 更复杂的操作,您可能需要将其切换为带有 idle
处理程序。但这超出了这个问题的范围。