MattJayC

Q: Get list of layer names from PSD file.

This the script I  am using, it gets a list of the layer names of a PSD file.

 

I am just trying to find out if the layer contains the named layer "Main Image"

 

But my logic appears not to be working.

Any ideas please.

 

set this_item to (choose file with prompt "Choose file to record path name") as string

 

set nLayersName to my getNameOfLayers(this_item as string)

log nLayersName as text

if "Main Image" is in {(nLayersName as text)} then

  display dialog "Do something"

end if

 

 

on getNameOfLayers(f)

  ((do shell script "/usr/bin/mdls -name kMDItemLayerNames " & (quoted form of POSIX path of f)))

end getNameOfLayers

Posted on Mar 11, 2015 4:47 AM

Close

Q: Get list of layer names from PSD file.

  • All replies
  • Helpful answers

Previous Page 2
  • by Hiroto,

    Hiroto Hiroto Mar 13, 2015 10:54 AM in response to MattJayC
    Level 5 (7,306 points)
    Mar 13, 2015 10:54 AM in response to MattJayC

    Since you're calling user defined handlers in Finder tell block, you need to qualify them with "my". Like this:

     

            set nLayersName to my getNameOfLayers(tFile)
            set matchList to my isMatchLayer(nLayersName, matchLayer)
    

     

    Regards,

    H

  • by VikingOSX,

    VikingOSX VikingOSX Mar 13, 2015 11:05 AM in response to Hiroto
    Level 7 (21,056 points)
    Mac OS X
    Mar 13, 2015 11:05 AM in response to Hiroto

    Maybe. When I tested the same code that the OP is using in the return from the handlers, it worked fine for me without the my possessive. Tested on 10.9.5.

  • by Hiroto,

    Hiroto Hiroto Mar 13, 2015 12:10 PM in response to MattJayC
    Level 5 (7,306 points)
    Mar 13, 2015 12:10 PM in response to MattJayC

    OK. I have read the script more carefully and found other problem.

     

    The argument for getNameOfLayers() must be POSIX path of file but a reference to Finder object:

     

    set nLayersName to my getNameOfLayers(tFile)
    

     

    should have been:

     

    set nLayersName to my getNameOfLayers((tFile as alias)'s POSIX path)
    

     

     

     

    The whole script would look something like this:

     

     

    property type_list : {"TIFF", "JPEG", "PNGf", "PICT"}
    property extension_list : {"tif", "tiff", "jpg", "jpeg", "png", "pict", "psd"}
    
    set nLayersName to {}
    set matchList to {}
    set matchLayer to "Shadows"
    
    tell application "Finder"
        set HotDestination to ((path to pictures folder) as text) & "HotDestination"
        set HotFolder2do to ((path to pictures folder) as text) & "HotFolder2do"
        set HotFolderDone to ((path to pictures folder) as text) & "HotFolderDone"
        
        repeat with tFile in (get document files of folder HotDestination whose name extension is in extension_list and (name does not start with "50" or name does not start with "MJC"))
            set nLayersName to my getNameOfLayers((tFile as alias)'s POSIX path) -- # added "my" and changed to pass POSIX path
            set matchList to my isMatchLayer(nLayersName, matchLayer) -- # added "my"
            if item 1 of matchList then
                move tFile to folder HotFolderDone
            else
                move tFile to folder HotFolder2do
            end if
        end repeat
    end tell
    
    on getNameOfLayers(f)
        set matchCmdStr to " | awk '!/kMDItemLayerNames|\\)/ { sub(/[ ]+/, \"\"); print }' "
        set mdlsCmd to "mdls -name kMDItemLayerNames "
        return do shell script (mdlsCmd & f's quoted form & matchCmdStr)
    end getNameOfLayers
    
    on isMatchLayer(psdLayers, matchItem)
        set matched to false --as boolean
        set matchCount to 0 --as integer
        repeat with layer in paragraphs of psdLayers
            if matchItem is in layer then
                set matchCount to matchCount + 1
                set matched to true
            end if
        end repeat
        return {matched, matchCount}
    end isMatchLayer
    

     

     

     

    * I have not tested shell script using mdls because I have completely killed spotlight in my environment.

     

     

    Good luck,

    H

  • by VikingOSX,

    VikingOSX VikingOSX Mar 13, 2015 12:27 PM in response to Hiroto
    Level 7 (21,056 points)
    Mac OS X
    Mar 13, 2015 12:27 PM in response to Hiroto

    Yes. In my testing of the OP sample psd file, I incorporated a POSIX path with the result of a choose file statement. That is why mine worked. Good eye.

  • by MattJayC,

    MattJayC MattJayC Jun 7, 2016 3:40 AM in response to VikingOSX
    Level 1 (5 points)
    Desktops
    Jun 7, 2016 3:40 AM in response to VikingOSX

    Hi does anyone know if the kMD is still working? Today the script has stopped working for me. I manual ran this command in Terminal and everything is NULL.

     

    Could the data be stripped at some point?

     

    mdls /Users/StudioServer/Dropbox/__Sorting__/HotDestination/FR10BW02343PU1CM_2.PSD

    kMDItemFSContentChangeDate = (null)

    kMDItemFSCreationDate      = (null)

    kMDItemFSCreatorCode       = ""

    kMDItemFSFinderFlags       = (null)

    kMDItemFSHasCustomIcon     = (null)

    kMDItemFSInvisible         = 0

    kMDItemFSIsExtensionHidden = (null)

    kMDItemFSIsStationery      = (null)

    kMDItemFSLabel             = (null)

    kMDItemFSName              = (null)

    kMDItemFSNodeCount         = (null)

    kMDItemFSOwnerGroupID      = (null)

    kMDItemFSOwnerUserID       = (null)

    kMDItemFSSize              = (null)

    kMDItemFSTypeCode          = ""

  • by MattJayC,

    MattJayC MattJayC Jun 7, 2016 4:04 AM in response to MattJayC
    Level 1 (5 points)
    Desktops
    Jun 7, 2016 4:04 AM in response to MattJayC

    I may have found the source of the problem. Am I right to think when you run the command mdls the information about the file comes from the spotlight file rather than the file directly?

     

    If this is the case it is possible that  because the computer moving the files is located remotely and the files arrive by dropbox, as soon as the file arrives there. the script runs to sort them. I am thinking within that time the spotlight file has not updated hence there is no KMD information about the file????

  • by VikingOSX,

    VikingOSX VikingOSX Jun 7, 2016 6:47 AM in response to MattJayC
    Level 7 (21,056 points)
    Mac OS X
    Jun 7, 2016 6:47 AM in response to MattJayC

    I just shoved a 30MB Photoshop file with 13 layers out on my iCloud Drive. The following command sequence returned just the layer names,  so it is not a spotlight issue.

    mdls -name kMDItemLayerNames ~/Library/Mobile\ Documents/com~apple~CloudDocs/*.PSD | trmdls.awk
    

    The trmdls.awk script is smart enough to return a single item, or multiple items.

     

    Code:

     

    #!/usr/bin/awk -f

    #

    # trmdls.awk

    # Usage: mdls -name kMDItemFonts filename | trmdls.awk

     

     

    # 1) one line to process. Print entire third field

    NR == 1 && !/\(/ {print substr($0, index($0,$3))}

     

     

    # 2) multiple lines. Skip first and last lines containing '(' and ')'

    #    Remove quotes and trailing ",". Remove leading/trailing whitespace,

    NR > 2 {print l} {$1=$1}1 {gsub(/,$/, "");gsub(/"/, "");l=$0}

    END {exit}

  • by MattJayC,

    MattJayC MattJayC Jun 7, 2016 7:40 AM in response to VikingOSX
    Level 1 (5 points)
    Desktops
    Jun 7, 2016 7:40 AM in response to VikingOSX

    I've just done some more testing, So on computer A I move a file into HotDestination. HotDestination is within Dropbox. If I use terminal to check kMDItem LayerNames it returns the information as expected. meanwhile the file has also synced via dropbox on Computer B. Run the same command on terminal and the file has no kMD information.

     

    I kept testing it but it does not update.

     

    Now Reboot the computer... and of course everything is fine, all info comes up.

    It would be good to see what this issue is since every so often the files containing the "named layer" go to the 2do folder.

     

    Thanks for getting back to me quickly!

Previous Page 2