0

我是 AppleScript 的新手,我正在尝试编写一个执行以下操作的基本脚本:

在文件夹 ~/Dropbox/Camera Uploads 中查找精确为 640x1136(iPhone 5 屏幕截图)的图像 (PNG),并将它们移动到 ~/Dropbox/Camera Uploads/Screenshots。

这看起来很简单,但到目前为止我还没有弄清楚。

4

3 回答 3

3

这就是我将如何做到的。我不会担心性能。我在 200 个文件上运行了图像事件部分,只用了 1 秒。

set picFolder to alias "Path:to:Dropbox:Camera Uploads:"
set screenshotFolder to alias "Path:to:Dropbox:Camera Uploads:screenshots:"


tell application "System Events"
    set photos to path of files of picFolder whose kind is "Portable Network Graphics image"
end tell

set screenshots to {}
repeat with imgPath in photos
    set imgAlias to alias imgPath
    tell application "Image Events"
        set img to open imgPath
        if dimensions of img = {640, 1136} then
            set end of screenshots to imgAlias
        end if
        close img
    end tell
end repeat

tell application "Finder"
    move screenshots to screenshotFolder
end tell
于 2013-08-20T21:09:53.323 回答
0

您需要有一个可识别 AppleScript 的应用程序,该应用程序可以根据图像文件的尺寸进行操作。我不认为 Finder 可以做到这一点,尽管它能够在 Finder 视图中显示图像的尺寸。

iPhoto 应该能够做到这一点。iPhoto 字典表明“照片”具有图像的宽度和高度。因此,您应该能够编写一个 AppleScript,首先将它们导入 iPhoto,然后选择符合您条件的那些,然后将它们保存到适当的 Dropbox 文件夹中。

根据您的需要,您还可以查看 Automator。它还包含 iPhoto 操作,包括“过滤 iPhoto 项目”。如果您创建一个文件夹操作,您应该能够创建一个 Automator 脚本,该脚本在您将新内容添加到您的 Camera Uploads 文件夹时启动,将它们添加到 iPhoto,然后将它们复制到您的 Screenshots 文件夹。

如果不出意外,您应该能够使用图像事件来获取文件夹中的所有图像,然后仅对符合您的条件的图像进行操作。就像是:

tell application "Image Events"
    tell folder "Macintosh HD:Users:colin:Dropbox:Camera Uploads"
        copy (files where kind is "JPEG image") to potentialScreenshots
        repeat with potentialFile in potentialScreenshots
            set potentialScreenshot to open potentialFile
            set imageDimensions to dimensions of potentialScreenshot
            if item 1 of imageDimensions is 640 then
                set fileName to name of potentialFile
                tell me to display dialog fileName
            end if
        end repeat
    end tell
end tell

应该有一种方法可以告诉图像事件只查看尺寸与您想要的文件匹配的文件,但我看不到它。

于 2013-08-19T01:21:40.403 回答
0

尝试:

set folderPath to POSIX path of (path to home folder) & "Dropbox/Camera Uploads"
set screenshotsPath to POSIX path of (path to home folder) & "Dropbox/Camera Uploads/Screenshots"

try
    do shell script "mdfind -0 -onlyin " & quoted form of folderPath & " \"kMDItemPixelWidth == 640 && kMDItemPixelHeight == 1136\" | xargs -0 -I {} mv {} " & quoted form of screenshotsPath
end try
于 2013-08-20T12:14:25.470 回答