为什么Photoshop不会在脚本中恢复到早期的历史状态?

时间:2011-12-28 17:57:21

标签: applescript photoshop input-history

我写了一个Applescript来为我公司自动化水印和调整图像大小。一切都运行正常 - 脚本将初始历史状态保存到变量,调整图像大小,添加适当的水印,保存jpeg,然后恢复到另一个调整大小和水印循环的初始历史状态。

问题在于,当我尝试不使用水印时,只需将变量wmColor设置为"None""None for all"即可调整大小。似乎在调整大小并保存jpeg之后,当我尝试恢复到初始历史状态时,Photoshop不喜欢它。这非常烦人,因为显然调整大小应该算作历史步骤,并且我不想重写脚本以在原始文件上实现多个打开/关闭操作。有谁知道可能会发生什么?这是产生问题的那一行(它在doBig和doSmall方法中,每次我要求它只是为了调整图像大小并改变当前历史状态时抛出错误:

    set current history state of current document to initialState

这是整个剧本:

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 myFolder
global wmYN
global wmColor
global nameUse
global rootName
global nameCount
property myFolder : ""

-- This droplet processes files dropped onto the applet 
on open these_items
    -- FILTER THE DRAGGED-ON ITEMS BY CHECKING THEIR PROPERTIES AGAINST THE LISTS ABOVE
    set wmColor to null
    set nameCount to 0
    set nameUse to null
    if myFolder is not "" then
        set myFolder to choose folder with prompt "Choose where to put your finished images" default location myFolder -- where you're going to store the jpgs
    else
        set myFolder to choose folder with prompt "Choose where to put your finished images" default location (path to desktop)
    end if

    repeat with i from 1 to the count of these_items
        set totalFiles to 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
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)
    set this_image to this_item as text
    tell application id "com.adobe.photoshop"
        set saveUnits to ruler units of settings
        set display dialogs to never
        open file this_image
        if wmColor is not in {"None for all", "White for all", "Black for all"} then
            set wmColor to choose from list {"None", "None for all", "Black", "Black for all", "White", "White for all"} with prompt "What color should the watermark be?" default items "White for all" without multiple selections allowed and empty selection allowed
        end if
        if wmColor is false then
            error number -128
        end if
        if nameUse is not "Just increment this for all" then
            set nameBox to display dialog "What should I call these things?" default answer ("image") with title "Choose the name stem for your images" buttons {"Cancel", "Just increment this for all", "OK"} default button "Just increment this for all"
            set nameUse to button returned of nameBox -- this will determine whether or not to increment stem names 
            set rootName to text returned of nameBox -- this will be the root part of all of your file names
            set currentName to rootName
        else
            set nameCount to nameCount + 1
            set currentName to rootName & (nameCount as text)
        end if
        set thisDocument to current document
        set initialState to current history state of thisDocument
        set ruler units of settings to pixel units
    end tell
    DoSmall(thisDocument, currentName, initialState)
    DoBig(thisDocument, currentName, initialState)
    tell application id "com.adobe.photoshop"
        close thisDocument without saving
        set ruler units of settings to saveUnits
    end tell
end process_item

to DoSmall(thisDocument, currentName, initialState)
    tell application id "com.adobe.photoshop"
        set initWidth to width of thisDocument
        if initWidth < 640 then
            resize image thisDocument width 640 resample method bicubic smoother
        else if initWidth > 640 then
            resize image thisDocument width 640 resample method bicubic sharper
        end if
        set myHeight to height of thisDocument
        set myWidth to width of thisDocument
        if wmColor is in {"White", "White for all"} then
            set wmFile to (path to resource "water_250_white.png" in bundle path to me) as text
        else if wmColor is in {"Black", "Black for all"} then
            set wmFile to (path to resource "water_250_black.png" in bundle path to me) as text
        end if
        if wmColor is not in {"None", "None for all"} then
            open file wmFile
            set wmDocument to current document
            set wmHeight to height of wmDocument
            set wmWidth to width of wmDocument
            duplicate current layer of wmDocument to thisDocument
            close wmDocument without saving
            translate current layer of thisDocument delta x (myWidth - wmWidth - 10) delta y (myHeight - wmHeight - 10)
            set opacity of current layer of thisDocument to 20
        end if
        set myPath to (myFolder as text) & (currentName) & "_640"
        set myOptions to {class:JPEG save options, embed color profile:false, quality:12}
        save thisDocument as JPEG in file myPath with options myOptions appending lowercase extension
        set current history state of current document to initialState
    end tell
end DoSmall

to DoBig(thisDocument, currentName, initialState)
    tell application id "com.adobe.photoshop"
        set initWidth to width of thisDocument
        if initWidth < 1020 then
            resize image thisDocument width 1020 resample method bicubic smoother
        else if initWidth > 1020 then
            resize image thisDocument width 1020 resample method bicubic sharper
        end if
        set myHeight to height of thisDocument
        set myWidth to width of thisDocument
        if wmColor is in {"White", "White for all"} then
            set wmFile to (path to resource "water_400_white.png" in bundle path to me) as text
        else if wmColor is in {"Black", "Black for all"} then
            set wmFile to (path to resource "water_400_black.png" in bundle path to me) as text
        end if
        if wmColor is not in {"None", "None for all"} then
            open file wmFile
            set wmDocument to current document
            set wmHeight to height of wmDocument
            set wmWidth to width of wmDocument
            duplicate current layer of wmDocument to thisDocument
            close wmDocument without saving
            translate current layer of thisDocument delta x (myWidth - wmWidth - 16) delta y (myHeight - wmHeight - 16)
            set opacity of current layer of thisDocument to 20
        end if
        set myPath to (myFolder as text) & (currentName) & "_1020"
        set myOptions to {class:JPEG save options, embed color profile:false, quality:12}
        save thisDocument as JPEG in file myPath with options myOptions appending lowercase extension
        set current history state of current document to initialState
    end tell
end DoBig

1 个答案:

答案 0 :(得分:2)

如果选择颜色:save document "Ducky.tif" as JPEG in file .....,当前文档将为document "Ducky.tif"

如果您选择“”或“无所有人”:save document "Ducky.tif" as JPEG in file ......当前文档将为document "image_640"

因此变量initialState = history state 2 of document "Ducky.tif"会出错,因为此文档已不存在。

要保留原始版本,这是一个解决方案,请在保存命令中使用copying true

save thisDocument as JPEG in file myPath with options myOptions appending lowercase extension with copying