Applescript Leopard 10.5.8中调整图像大小的文件夹操作

Applescript Leopard 10.5.8中调整图像大小的文件夹操作,applescript,directory,Applescript,Directory,我对AppleScript一无所知,所以请原谅我 我们的目标是创建一个文件夹,让我们的制图人员可以将库存照片放入其中,并将其大小调整为1280。所选文件夹是网络文件夹,因此不知道这是否是问题的一部分。他将从Windows工作站将图像粘贴到文件夹中 以下是我到目前为止拼凑的内容: on adding folder items to this_folder after receiving added_items repeat with this_item in added_items

我对AppleScript一无所知,所以请原谅我

我们的目标是创建一个文件夹,让我们的制图人员可以将库存照片放入其中,并将其大小调整为1280。所选文件夹是网络文件夹,因此不知道这是否是问题的一部分。他将从Windows工作站将图像粘贴到文件夹中

以下是我到目前为止拼凑的内容:

on adding folder items to this_folder after receiving added_items
    repeat with this_item in added_items
        try
            rescale_and_save(this_item)
        end try
    end repeat
end adding folder items to

to rescale_and_save(this_item)
    tell application "Image Events"
        launch
        set the target_height to 1280
        -- open the image file
        set this_image to open this_item

        set typ to this_image's file type

        copy dimensions of this_image to {current_width, current_height}
        if current_height is greater than current_width then
            scale this_image to size target_height
        else
            -- figure out new height
            -- y2 = (y1 * x2) / x1
            set the new_width to (current_width * target_height) / current_height
            scale this_image to size new_width
        end if

        tell application "Finder" to set new_item to ¬
            (container of this_item as string) & "ex" & (name of this_item)
        save this_image in new_item as typ

    end tell
end rescale_and_save
我右键单击了文件夹,启用了文件夹操作,并添加了脚本。我复制了测试文件,但什么也没发生

有什么想法吗

编辑:

我查看了这些示例,得出了以下有效的脚本!:

property done_foldername : "Scaled Images"
property target_height : 1280
-- the list of file types which will be processed 
-- eg: {"PICT", "JPEG", "TIFF", "GIFf"} 
property type_list : {"JPEG", "TIFF", "PNGf"}
-- since file types are optional in Mac OS X, 
-- check the name extension if there is no file type 
-- NOTE: do not use periods (.) with the items in the name extensions list 
-- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"} 
property extension_list : {"jpg", "jpeg", "tif", "tiff", "png"}


on adding folder items to this_folder after receiving these_items
    -- CHECK FOR THE DESTINATION FOLDER WITHIN THE ATTACHED FOLDER
    -- IF IT DOESN'T EXIST, THEN CREATE IT
    tell application "Finder"
        if not (exists folder done_foldername of this_folder) then
            make new folder at this_folder with properties {name:done_foldername}
            set current view of container window of this_folder to list view
        end if
        set the target_folder to folder done_foldername of this_folder
    end tell
    -- PROCESS EACH OF THE ITEMS ADDED TO THE ATTACHED FOLDER
    try
        repeat with i from 1 to number of items in these_items
            set this_item to item i of these_items
            set the item_info to the info for this_item
            -- CHECK TO SEE IF THE ITEM IS AN IMAGE FILE OF THE ACCEPTED FILE TYPE
            if (alias of the item_info is false and the file type of the item_info is in the type_list) or (the name extension of the item_info is in the extension_list) then
                tell application "Finder"
                    -- LOOK FOR EXISTING MATCHING ITEMS IN THE DESTINATION FOLDER
                    -- IF THERE ARE MATCHES, THEN RENAME THE CONFLICTING FILES INCREMENTALLY
                    my resolve_conflicts(this_item, target_folder)
                    -- MOVE THE ITEM TO THE DESTINATION FOLDER
                    set the target_file to (move this_item to the target_folder with replacing) as alias
                end tell
                -- PROCESS THE ITEM
                process_item(target_file)
            end if
        end repeat
    on error error_message number error_number
        if the error_number is not -128 then
            tell application "Finder"
                activate
                display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
            end tell
        end if
    end try
end adding folder items to

on resolve_conflicts(this_item, target_folder)
    tell application "Finder"
        set the file_name to the name of this_item
        if (exists document file file_name of target_folder) then
            set file_extension to the name extension of this_item
            if the file_extension is "" then
                set the trimmed_name to the file_name
            else
                set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
            end if
            set the name_increment to 1
            repeat
                set the new_name to (the trimmed_name & "." & (name_increment as string) & "." & file_extension) as string
                if not (exists document file new_name of the target_folder) then
                    -- rename to conflicting file
                    set the name of document file file_name of the target_folder to the new_name
                    exit repeat
                else
                    set the name_increment to the name_increment + 1
                end if
            end repeat
        end if
    end tell
end resolve_conflicts

-- this sub-routine processes files 
on process_item(this_item)
    -- NOTE that the variable this_item is a file reference in alias format 
    -- FILE PROCESSING STATEMENTS GOES HERE 
    try
        -- convert alias reference to string
        set this_item to this_item as string
        with timeout of 900 seconds
            tell application "Image Events"
                launch -- always use with Folder Actions
                set this_image to open file this_item
                scale this_image to size target_height
                save this_image with icon
                close this_image
            end tell
        end timeout
    on error error_message
        tell application "Finder"
            activate
            display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
        end tell
    end try
end process_item

我刚刚在一个文件夹上测试了您的脚本,在其中放入了1个图像,结果表明它会重复运行,因为生成的(调整大小的)图像也会添加到该文件夹中。下面是几秒钟后,在我打断它之前的样子:


您应该查看
/Library/scripts/Folder Action scripts
中的默认图像处理脚本,并可能使用其中一个作为模板。它们基本上为处理后的图像使用一个子文件夹,并包含一些可能有用的文件名冲突解决功能。

这是一个在网络上共享的文件夹,这一事实不应该引起任何问题。你确定你将脚本作为文件夹操作正确附加了吗?6年的解决方案仍然有效!我认为您应该提供您编辑的解决方案作为答案,因为它实际上回答了您的问题。谢谢。这正是我所做的,并且我能够让它工作。我已将工作脚本发布到我的问题中。