2

我正在尝试制作一个脚本,该脚本将拍摄屏幕截图,将图像保存到桌面,并将其命名为日期。类似于如果我使用 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

4 回答 4

4

传递上述路径的正确方法是使用以下引用形式:

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
于 2013-05-29T00:07:44.960 回答
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.

于 2013-05-28T17:47:45.520 回答
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

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

于 2013-05-29T01:45:44.480 回答
1

模拟你的键盘:

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

于 2018-11-18T03:14:12.410 回答