I have written (with the aid of posters around the world) a script that cleans up photoshop files, burrowing into smart objects and cleaning up there too. It works fine when run from Applescript Editor, however when dropping files on the application, the script only runs these handlers (my deleteHiddenLayers is commented out because it requires an action to run not included here):
my assignWorkingProfile()
-- my deleteHiddenLayers()
my reduceTo150ppi()
my selectAllandCrop()
and not the smart object one:
my editSmartLayers(layers)
The handler that does not work runs a javascript because of lack of functionality in Applescript in this area.
Any ideas anyone?
Here’s the script:
-- opens photoshop files without warnings then resizes, crops, deletes hidden layers. Even enters smart objects and does the same there
property thisDoc : missing value
property thisfile : missing value
property myLayerList : missing value
on run
set thesefiles to choose file with multiple selections allowed
tell application "Adobe Photoshop CS5.1"
activate
set display dialogs to never
my main(thesefiles)
set display dialogs to always
end tell
end run
on open thesefiles
tell application "Adobe Photoshop CS5.1"
activate
set display dialogs to never
my main(thesefiles)
set display dialogs to always
end tell
end open
# main handler
on main(thesefiles)
set myLayerList to {}
tell application "Adobe Photoshop CS5.1"
repeat with thisfile in thesefiles
open thisfile
set thisDoc to the current document
tell thisDoc
my assignWorkingProfile()
-- my deleteHiddenLayers()
my reduceTo150ppi()
my selectAllandCrop()
my editSmartLayers(layers)
my saveNotMaxCompat(thisfile)
close thisDoc
end tell
end repeat
end tell
end main
# handler to assign working profile. Should work for both cmyk and rgb
on assignWorkingProfile()
tell application "Adobe Photoshop CS5.1"
tell current document
if color profile kind is not working then
set color profile kind to working
end if
end tell
end tell
end assignWorkingProfile
# handler to loop through all smart objects and clean them up too
on editSmartLayers(myCurrentLayers)
tell application "Adobe Photoshop CS5.1"
--tell thisDoc
repeat with i from 1 to count myCurrentLayers
set myLayer to item i of myCurrentLayers
if class of myLayer is art layer then
if (kind of myLayer as string) is "smart object layer" then
set current layer of current document to myLayer
set myLayerName to name of myLayer
log myLayerName
set myLayerType to my checkSmartLayer()
log myLayerType
if myLayerType is equal to "rasterizeContent" then
my openSmartLayer()
tell current document
my editSmartLayers(layers)
end tell
--my deleteHiddenLayers()
my reduceTo150ppi()
my selectAllandCrop()
close current document saving yes
end if
set end of myLayerList to myLayer
end if
else if class of myLayer is layer set then
my editSmartLayers(layers of myLayer)
end if
end repeat
--end tell
end tell
end editSmartLayers
# this handler uses an action because the scripted version of this is way too slow
on deleteHiddenLayers()
tell application "Adobe Photoshop CS5.1"
try
do action "Delete hidden layers" from "Clean up files"
on error
display dialog "You need to load an action called \"Delete hidden layers\" into a set called \"Clean up files\" for this script to work." buttons "Cancel" default button "Cancel"
end try
end tell
end deleteHiddenLayers
# handler to de-res to 150ppi
on reduceTo150ppi()
tell application "Adobe Photoshop CS5.1"
tell current document
if resolution is greater than or equal to 150 then
resize image resolution 150
end if
end tell
end tell
end reduceTo150ppi
# handler to select all and crop, thereby chopping off loads of junk
on selectAllandCrop()
tell application "Adobe Photoshop CS5.1"
tell current document
select all -- of current document
set theCropBounds to bounds of selection
crop bounds theCropBounds
end tell
end tell
end selectAllandCrop
# changes the settings, saves the file and resets the settings
on saveNotMaxCompat(thisfile)
tell application "Adobe Photoshop CS5.1"
set oldSettings to maximize compatibility of settings -- get whatever settings the user has
set maximize compatibility of settings to never -- for saving small files
try -- errors may be caused by Photoshop's flaky server integration
tell current document to save
on error
save current document in file (thisfile as string) as Photoshop format with options {embed color profile:true, save alpha channels:true, save annotations:true, save layers:true, save spot colors:true} appending lowercase extension without copying
end try
set maximize compatibility of settings to oldSettings -- set the settings back
end tell
end saveNotMaxCompat
# handler using javascript to check whether vector or photoshop smart object
on checkSmartLayer()
tell application "Adobe Photoshop CS5.1"
do javascript "getSmartObjectType();
// Get Smart Object Type
function getSmartObjectType(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var desc = executeActionGet(ref);
if(desc.hasKey(stringIDToTypeID('smartObject'))){// is smart object?
var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('smartObject')).getEnumerationValue(stringIDToTypeID('placed'));
return typeIDToStringID(desc);
}
}"
end tell
end checkSmartLayer
# handler using javascript to open a smart object
on openSmartLayer()
tell application "Adobe Photoshop CS5.1"
do javascript "editSmartLayer();
// Edit Smart Layer
function editSmartLayer() {
function cTID(s) { return app.charIDToTypeID(s); };
function sTID(s) { return app.stringIDToTypeID(s); };
var desc01 = new ActionDescriptor();
executeAction( sTID('placedLayerEditContents'), desc01, DialogModes.NO );
};"
end tell
end openSmartLayer
Browser: Safari 534.57.2
Operating System: Mac OS X (10.7)