拍摄屏幕截图并以当前时间作为名称保存到桌面

时间:2013-05-28 17:25:27

标签: applescript screenshot

我正在尝试创建一个将拍摄屏幕截图的脚本,将图像保存到桌面,并将其命名为日期。类似于我使用cmd + shift + 3时会发生的情况。唯一的问题是图像的名称只是“屏幕”而不是我指定的整个名称。任何人都知道如何解决这个问题?

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

4 个答案:

答案 0 :(得分:3)

将完整的文件路径放在双引号中,如下所示:

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

文件名包含空格,因此,在您的版本中,命令行会将其解释为/usr/sbin/screencapture的多个参数。

答案 1 :(得分:3)

通过上述路径的正确方法是使用引用形式:

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

答案 2 :(得分:1)

我只是使用这样的shell命令:

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 screencaptureman strftime

如果您使用AppleScript,则不需要运行处理程序,默认情况下/usr/sbin/位于路径上,您可以使用quoted form of转义参数。

"Screen Shot " & (current date) & ".png"
do shell script "screencapture ~/Desktop/" & quoted form of result

如果文件名在Finder中看起来像Screen Shot Wednesday, May 29, 2013 4/47/15 AM.png,那是因为HFS使用冒号作为路径名分隔符。 shell中的:在Finder中显示为/,反之亦然。

答案 3 :(得分:0)

模拟键盘:

-- 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

完整的键控代码列表可在此处找到:

https://eastmanreference.com/complete-list-of-applescript-key-codes

相关问题