AppleScript:根据图像尺寸整理图像

时间:2013-08-18 21:34:33

标签: applescript

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

在〜/ Dropbox / Camera Uploads文件夹中查找图像(PNG),这些文件正好是640x1136(iPhone 5截图)并将它们移动到〜/ Dropbox / Camera Uploads / Screenshots。

这看起来非常简单,但到目前为止我还没能弄明白。

3 个答案:

答案 0 :(得分:3)

这是我将如何做到的。我不担心性能。我在200个文件上运行了Image Events部分,只用了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

答案 1 :(得分:0)

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

iPhoto应该能够做到这一点。 iPhoto字典表示“照片”同时具有图像的宽度和高度。因此,您应该能够编写AppleScript,先将其导入iPhoto,然后选择符合条件的AppleScript,然后将它们保存到相应的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

应该有一种方法告诉Image Events只查看尺寸与你想要的相匹配的文件,但我看不到它。

答案 2 :(得分: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