2
votes

I am at my wit's end. I have tried all variations to get this script to work. The error I get is Adobe Photoshop CS6 got an error: Can’t get current document. and the highlighted script error is my "export in file newFileName.." block. I've tried putting alias in different positions, using file, not using file. Also I get this error message, but the actual script seems to stop working right after "set docName to name of docRef"

And basically I just copied this code from another script that was working fine and just changed a save this file... to a export this file...

-- set the folders that you want to use
set inputFolder to choose folder with prompt "Choose the folder of images to downsize."
set pathToDesktop to (path to desktop folder as string)
set outputFolder to pathToDesktop & "PhotoshopRetina:"


tell application "Finder"
set filesList to files in folder inputFolder
if not (exists folder outputFolder) then
    make new folder at desktop with properties {name:"PhotoshopRetina"}
end if
end tell
with timeout of 86400 seconds
tell application "Adobe Photoshop CS6"
    set display dialogs to never
    close every document saving no
end tell

repeat with aFile in filesList

    tell application "Finder"
        -- The step below is important because the 'aFile' reference as    returned by
        -- Finder associates the file with Finder and not Photoshop. By converting
        -- the reference below 'as alias', the reference used by 'open' will be
        -- correctly handled by Photoshop rather than Finder.
        set theFile to aFile as string
        set theFileName to name of aFile
        set theFileInfo to info for alias theFile
        if kind of theFileInfo is "Adobe Photoshop JPEG file" then
            my retinaDisplay(theFile)
        end if
    end tell

end repeat
end timeout
end

on retinaDisplay(theFile)

tell application "Adobe Photoshop CS6"
    open alias theFile

    set docRef to the current document
    -- Convert the document to a document mode that supports saving as jpeg
    if (mode of docRef is not RGB) then
        change mode docRef to RGB
    end if

    tell docRef

        set color profile kind to none

    end tell

    set infoRef to get info of docRef
    set docName to name of docRef
    set docBaseName to getBaseName(docName) of me

    set newFileName to (my outputFolder as string) & docBaseName & ".jpg"



    tell current document
        export in file newFileName as save for web with options {class:save for web export options, web format:JPEG, embed color profile:false, quality:45} with copying
    end tell

    close current document without saving

end tell
end retinaDisplay

-- Returns the document name without extension (if present)
on getBaseName(fName)
set baseName to fName
repeat with idx from 1 to (length of fName)
    if (item idx of fName = ".") then
        set baseName to (items 1 thru (idx - 1) of fName) as string
        exit repeat
    end if
end repeat
return baseName
end getBaseName
end
1
>> but the actual script seems to stop working right after "set docName to name of docRef" <<. How can you tell, specifically? When you run in AppleScript Editor, what do you see in the Events output tab? Not that I know why it would make a difference, but try the standard object specifier document 1 in lieu of current document. - mklement0

1 Answers

1
votes

If I open an image in photoshop I can run this code with no errors.

set f to (path to desktop as text) & "test.jpg"

tell application "Adobe Photoshop CS6"
    tell current document
        export in file f as save for web
    end tell
end tell

However, if I additionally add your "with options" code then I get your error. I don't even know what the "with copying" part is. I don't think that means anything to photoshop. So the problem is not with the "current document". The problem is with your options. You must be doing that part wrong.

Good luck.