Applescript Droplet - 将文件夹/子文件夹中所有文件的图像分辨率导出为文本文件

Posted

技术标签:

【中文标题】Applescript Droplet - 将文件夹/子文件夹中所有文件的图像分辨率导出为文本文件【英文标题】:Applescript Droplet - Export Image Resolution of All Files in Folder/Subfolder as text file 【发布时间】:2014-11-16 22:46:41 【问题描述】:

我可能犯了一个简单的错误,但我似乎无法让我的代码正常工作。图我会向这个网站上的天才寻求帮助。我是一个新手脚本编写者,更像是代码拼写者。这是我的第一篇文章,请温柔。

我想要做什么...我有一个文件夹(和子文件夹),里面装满了图片。我想要一个applescript droplet,我可以将这个文件夹拖到这个droplet上,以获得该图像的分辨率和路径的文本文件报告。这样,我可以确保我的文件在发送给最终供应商之前都是相同且正确的分辨率。如果文件不是正确的分辨率,我有需要调整文件的路径。我能够创建一个可以处理单个文件的 droplet,但是一旦我拖动一个包含图像和子文件夹的文件夹,我就会得到一个空白的文本文档。

这是我在文件的文本文件中的初始结果:

72.0 dpi /Users/chewbacca/Desktop/untitled 文件夹/pixels-300x232 复制 2.png 72.0 dpi /Users/chewbacca/Desktop/untitled 文件夹/pixels-300x232 复制 3.png 72.0 dpi /Users/chewbacca/Desktop/untitled 文件夹/pixels-300x232 复制 4.png

这是我尝试让文件夹/子文件夹 droplet 工作:

on open (rawList)

set outFile to (choose file name with prompt "Output file:" default name "ResolutionReport.txt")
open for access outFile with write permission

set fileList to every file of rawList
repeat with i from 1 to number of items in fileList

    tell application "Image Events"
        launch
        set thisImage to open file (oneFile as string)
        copy the resolution of thisImage to H_res, V_res
        close thisImage
    end tell

    write (H_res as string) & " dpi " & (POSIX path of oneFile) & return to outFile
end repeat
close access outFile

end open

我意识到这在 perl 中可能更简单,但我想暂时保留这个纯 Applescript,因为我开始掌握如何使用这种语言。

【问题讨论】:

真正有用的是模板(见菜单“文件”)和代码-sn-ps(CTRL-单击进入脚本)。对你来说,模板“Droplets”->“递归图像文件处理Droplet”可能值得一看。 感谢零的有用参考。我去看看! 【参考方案1】:

模板(菜单File)和code-sn-ps(CTRL-Click into a script)真的很有帮助。

模板Droplets->Recursive Image File Processing Droplet 提供一切。这是一个经过编辑的版本,可以满足您的需求:

(* 
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. ("Apple") in consideration of your agreement to the following terms, and your use, installation, modification or redistribution of this Apple software constitutes acceptance of these terms. If you do not agree with these terms, please do not use, install, modify or redistribute this Apple software.

In consideration of your agreement to abide by the following terms, and subject to these terms, Apple grants you a personal, non-exclusive license, under Apple's copyrights in this original Apple software ( the "Apple Software" ), to use, reproduce, modify and redistribute the Apple Software, with or without modifications, in source and / or binary forms; provided that if you redistribute the Apple Software in its entirety and without modifications, you must retain this notice and the following text and disclaimers in all such redistributions of the Apple Software. Neither the name, trademarks, service marks or logos of Apple Inc. may be used to endorse or promote products derived from the Apple Software without specific prior written permission from Apple. Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by Apple herein, including but not limited to any patent rights that may be infringed by your derivative works or by other works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON - INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.

IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND / OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT ( INCLUDING NEGLIGENCE ), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Copyright ( C ) 2011 Apple Inc. All Rights Reserved.
*)

(* INSTRUCTIONS
This droplet is designed to process one or more files, or folders containing files, whose icons are dragged onto the droplet icon.
The droplet processes recursively, and will examine the contents of every folder and every sub-folder within those folders, to find and process any appropriate files found.
To use, replace the example processing code in the process_item sub-routine with your code. Save as an application.
*)

(* TO FILTER FOR IMAGE FILES, LOOK FOR QUICKTIME SUPPORTED IMAGE FORMATS *)
property type_list : "JPEG", "TIFF", "PNGf", "8BPS", "BMPf", "GIFf", "PDF ", "PICT"
property extension_list : "jpg", "jpeg", "tif", "tiff", "png", "psd", "bmp", "gif", "jp2", "pdf", "pict", "pct", "sgi", "tga"
property typeIDs_list : "public.jpeg", "public.tiff", "public.png", "com.adobe.photoshop-image", "com.microsoft.bmp", "com.compuserve.gif", "public.jpeg-2000", "com.adobe.pdf", "com.apple.pict", "com.sgi.sgi-image", "com.truevision.tga-image"


global outFile




-- This droplet processes files dropped onto the applet 
on open these_items


    set outFile to (choose file name with prompt "Output file:" default name "ResolutionReport.txt")
    open for access outFile with write permission


    -- FILTER THE DRAGGED-ON ITEMS BY CHECKING THEIR PROPERTIES AGAINST THE LISTS ABOVE
    repeat with i from 1 to the count of these_items
        set this_item to item i of these_items
        set the item_info to info for this_item without size
        if folder of the item_info is true then
            process_folder(this_item)
        else
            try
                set this_extension to the name extension of item_info
            on error
                set this_extension to ""
            end try
            try
                set this_filetype to the file type of item_info
            on error
                set this_filetype to ""
            end try
            try
                set this_typeID to the type identifier of item_info
            on error
                set this_typeID to ""
            end try
            if (folder of the item_info is false) and (alias of the item_info is false) and ((this_filetype is in the type_list) or (this_extension is in the extension_list) or (this_typeID is in typeIDs_list)) then
                -- THE ITEM IS AN IMAGE FILE AND CAN BE PROCESSED
                process_item(this_item)
            end if
        end if
    end repeat


    close access outFile

end open

-- this sub-routine processes folders 
on process_folder(this_folder)
    set these_items to list folder this_folder without invisibles
    repeat with i from 1 to the count of these_items
        set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
        set the item_info to info for this_item without size
        if folder of the item_info is true then
            process_folder(this_item)
        else
            try
                set this_extension to the name extension of item_info
            on error
                set this_extension to ""
            end try
            try
                set this_filetype to the file type of item_info
            on error
                set this_filetype to ""
            end try
            try
                set this_typeID to the type identifier of item_info
            on error
                set this_typeID to ""
            end try
            if (folder of the item_info is false) and (alias of the item_info is false) and ((this_filetype is in the type_list) or (this_extension is in the extension_list) or (this_typeID is in typeIDs_list)) then
                -- THE ITEM IS AN IMAGE FILE AND CAN BE PROCESSED
                process_item(this_item)
            end if
        end if
    end repeat
end process_folder

-- this sub-routine processes files 
on process_item(this_item)
    -- NOTE that the variable this_item is a file reference in alias format 

    set H_res to 0
    set V_res to 0

    try
        tell application "Image Events"

            -- start the Image Events application
            launch

            -- open the image file
            set this_image to open this_item

            -- perform action
            copy the resolution of this_image to H_res, V_res

            set outString to ""

            if H_res > 0 and V_res > 0 then
                set outString to (H_res as string) & " dpi " & (POSIX path of this_item)
            else
                set outString to "#### ERROR #### " & (POSIX path of this_item)
            end if


            try
                write outString & return to outFile

            on error the error_message number the error_number
                close access outFile
                beep
                tell me to activate
                display dialog "Error: " & the error_number & ". " & the error_message buttons "OK" default button 1
            end try

            -- purge the open image data
            close this_image
        end tell

    on error error_message number error_number
        display alert "PROCESSING ERROR " & error_number message error_message as warning buttons "Cancel" cancel button "Cancel"
    end try

end process_item

【讨论】:

老兄!钱就对了!多谢零!这超出了预期。 Love it 验证文件类型并仅处理图像文件。它也有错误警报。这正是我一直在寻找的。我还看了sn-ps,我从来不知道那些在那里。再次感谢您!【参考方案2】:

您可以尝试以下方式:

on open of theFolder
    set outFile to POSIX path of (choose file name with prompt "Output file:" default name "ResolutionReport.txt")
    set folderPath to POSIX path of (first item of theFolder)
    do shell script "find " & quoted form of folderPath & " -type f -flags nohidden \\! -name \".*\" -exec sips -g dpiHeight  + | sed 'N;s/\\n//' > " & quoted form of outFile
end open

【讨论】:

谢谢你。我必须解析你的 shell 脚本行才能完全理解它。我真的很想将整个内容保留在 Applescript 中,这样我就可以在转向另一种语言之前接受并完全理解该语言。我意识到在 shell 中只用一行比在 Applescript 中用多行看起来更容易。

以上是关于Applescript Droplet - 将文件夹/子文件夹中所有文件的图像分辨率导出为文本文件的主要内容,如果未能解决你的问题,请参考以下文章

如何使用DigitalOcean控制台访问你的Droplet

Applescript 将文件复制到新文件夹

自动将输出文件与输入文件匹配(Applescript x FFMPEG)

Xcode 13 AppleScript 框架无法将 UI 连接到 Applescript 代码

如何将Applescript scpt文件转换为命令

使用 AppleScript 将 finder 中的副本替换为脚本