Applescript 拍摄屏幕截图并以当前时间作为名称保存到桌面
我正在尝试制作一个脚本,它将拍摄一个屏幕截图,将图像保存到桌面,并将其命名为日期。与使用cmd+shift+3时的情况类似。唯一的问题是图像的名称只是“Screen”,而不是我指定的整个名称。有人知道怎么解决这个问题吗Applescript 拍摄屏幕截图并以当前时间作为名称保存到桌面,applescript,screenshot,Applescript,Screenshot,我正在尝试制作一个脚本,它将拍摄一个屏幕截图,将图像保存到桌面,并将其命名为日期。与使用cmd+shift+3时的情况类似。唯一的问题是图像的名称只是“Screen”,而不是我指定的整个名称。有人知道怎么解决这个问题吗 on run set theDesktop to POSIX path of (path to desktop as string) set theCurrentDate to current date set shellCommand to "/usr/
on run
set theDesktop to POSIX path of (path to desktop as string)
set theCurrentDate to current date
set shellCommand to "/usr/sbin/screencapture " & theDesktop & "Screen Shot" & theCurrentDate & ".png"
do shell script shellCommand
end run
将完整的文件路径用双引号括起来,如下所示:
on run
set theDesktop to POSIX path of (path to desktop as string)
set theCurrentDate to current date
set shellCommand to "/usr/sbin/screencapture \"" & theDesktop & "Screen Shot" & theCurrentDate & ".png\""
do shell script shellCommand
end run
screencapture -i ~/Desktop/$(date +%Y%m%d%H%M%S).png
文件名包含空格,因此,在您的版本中,命令行将其解释为
/usr/sbin/screenscapture
的多个参数。传递上述路径的正确方法是使用以下引用形式:
on run
set theDesktop to POSIX path of (path to desktop as string)
set theCurrentDate to current date
set shellCommand to "/usr/sbin/screencapture " & quoted form of (theDesktop & "Screen Shot" & theCurrentDate & ".png")
do shell script shellCommand
end run
我只是使用如下shell命令:
on run
set theDesktop to POSIX path of (path to desktop as string)
set theCurrentDate to current date
set shellCommand to "/usr/sbin/screencapture \"" & theDesktop & "Screen Shot" & theCurrentDate & ".png\""
do shell script shellCommand
end run
screencapture -i ~/Desktop/$(date +%Y%m%d%H%M%S).png
-i
是交互式模式(如⇧⌘4). 在我的安装中,默认文件名格式如下:
date '+Screen Shot %Y-%m-%d at %-H.%M.%S %p.png'
请参见man屏幕截图
和man strftime
如果使用AppleScript,则不需要运行处理程序,/usr/sbin/
默认位于路径上,并且可以使用的引号形式
转义参数
"Screen Shot " & (current date) & ".png"
do shell script "screencapture ~/Desktop/" & quoted form of result
如果文件名在Finder中看起来像2013年5月29日星期三4/47/15 AM.png的屏幕截图,那是因为HFS使用冒号作为路径名分隔符<代码>:在Shell中显示为查找器中的/
,反之亦然。模拟键盘:
-- Take Screenshot
tell application "System Events"
-- Shift-Command-3
key code 20 using {shift down, command down}
end tell
钥匙代码20与编号3相同
结果:
Screen Shot 2018-11-18 at 12.47.39 pm
可在此处找到钥匙代码的完整列表:
谢谢!我总是很惊讶困难的问题有这么简单的答案。我只是做了一个非常类似的脚本。我没有费心为“截屏”给出完整的路径名,而且效果很好。这应该是公认的答案:更优雅的方式。(请注意<代码>结尾处的空白:“/Ur/sBI/ScCurnSurvivs/<代码>”,因为终端使用空白空间分隔变量)