Skip navigation

Open files in folder and repeat

668 Views 11 Replies Latest reply: Mar 22, 2012 8:52 AM by MattJayC RSS
MattJayC Level 1 Level 1 (0 points)
Currently Being Moderated
Mar 14, 2012 4:45 AM

How can i open the files in the folder  if you scroll to "--open file items of the name folder" you can see where I am struggling to run the script.

 

How should this look to run, currently the script runs, with no errors or anything appearing in the folders.

 

 

property type_list : {"PHOTOSHOP"}

property extension_list : {"psd"}

 

--Setup list of folders and process details of folders

 

set dtF to paragraphs of (do shell script "ls -F ~/Desktop | grep '/' | cut -d'/' -f1")

repeat with i from 1 to number of items in dtF

          set this_item to item i of dtF

          if this_item does not start with "2_" and this_item does not start with "Hot" and this_item does not start with "Press" then

                    try

                              set folderPath to this_item

                              set {oldTID, my text item delimiters} to {my text item delimiters, ":"}

                              set folderName to last text item of folderPath

                              set my text item delimiters to "_WK"

                              set FolderEndName to last text item of folderName

                              set brandName to first text item of folderName

                              set my text item delimiters to "_PSD"

                              set weekNumber to first text item of FolderEndName

                              set my text item delimiters to oldTID

                              set theFolder to ("Hal 9000:Users:matthew:Desktop:" & folderName)

 

                    end try

          end if

 

  --set up names to destination folders and create locally based on brand name and week number

          try

                    set this_local_folder to "Hal 9000:Users:matthew:Pictures:2011-2012"

                    set localWeekFolder to my getFolderPath("WK" & weekNumber, this_local_folder)

                    set localBrandFolder to my getFolderPath(brandName, localWeekFolder)

                    set localBrandFolder_Low_Res to my getFolderPath(brandName & "_WK" & weekNumber & "_LR", localBrandFolder)

                    set localBrandFolder_High_Res to my getFolderPath(brandName & "_WK" & weekNumber & "_HR", localBrandFolder)

 

  --set up names to destination folders and create over Netwrok including an already exisiting folder

 

                    set this_Network_folder to "DCKGEN:Brands:Zoom:Brand - Zoom:Upload Photos:2012:"

                    set networkWeekFolder to my getFolderPath("WK" & weekNumber, this_Network_folder)

                    set networkBrandFolder to my getFolderPath(brandName, networkWeekFolder)

                    set networkBrandFolder_Low_Res to my getFolderPath(brandName & "_WK" & weekNumber & "_LR", networkBrandFolder)

 

                    set website_images to "DCKGEN:Website_Images:"

 

  --set up names to destination folders and create over Netwrok for FTP collection (based on a mounted drive)

 

                    set this_ftp_folder to "Impulse:"

                    set ftpWeekFolder to my getFolderPath("Week" & weekNumber, this_ftp_folder)

                    set ftpBrandFolder to my getFolderPath(brandName, ftpWeekFolder)

                    set ftpBrandFolder_Low_Res to my getFolderPath(brandName & "_WK" & weekNumber & "_LR", ftpBrandFolder)

                    set ftpBrandFolder_High_Res to my getFolderPath(brandName & "_WK" & weekNumber & "_HR", ftpBrandFolder)

 

 

  --taking the folder identify which process it must follow.

 

--open file items of the name folder

 

                    if brandName is equal to "Diva" then set filelist to (files of alias theFolder whose ¬

                              file type is in the type_list or name extension is in the extension_list)

 

 

                    repeat with alist in filelist

                              tell application "Finder"

                                        set thefile to aFiles as alias

                              end tell

                              tell application "Adobe Photoshop CS5.1"

  activate

 

                                        set myOptions to {class:JPEG save options, quality:12}

                                        tell front document

 

  --If the quick mask mode has been left on then delete the channel Quick Mask

 

                                                  if (quick mask mode) then delete channel ¬

                                                            "Quick Mask"

 

                                                  delete layer "Original Image"

  flatten

  resize image resolution 300 resample method none

 

  --sharpen image

 

  filter current layer using unsharp mask with options ¬

                                                            {amount:80, radius:3.2, threshold:0}

 

 

  save in file ((localBrandFolder_High_Res as string) & name) as JPEG with options myOptions without copying

                                                  tell application "Finder"

  duplicate last file of localBrandFolder_High_Res to ftpBrandFolder_High_Res with replacing

                                                  end tell

 

  --Prepare for Low RES by resetting image history

 

                                                  tell application "Adobe Photoshop CS5.1"

                                                            set current history state of current document to history state 3 ¬

                                                                      of current document

                                                  end tell

 

  resize image width 1020

  resize image resolution 300 resample method none

  filter current layer using unsharp mask with options ¬

                                                            {amount:80, radius:3.2, threshold:0}

 

  --add save to lowResFolder with same options

  save in file ((localBrandFolder_Low_Res as string) & name) as JPEG with options myOptions without copying

                                                  tell application "Finder"

  duplicate last file of localBrandFolder_Low_Res to website_images with replacing

  duplicate last file of localBrandFolder_Low_Res to ftpBrandFolder_Low_Res with replacing

  duplicate last file of localBrandFolder_Low_Res to networkBrandFolder_Low_Res with replacing

                                                  end tell

 

  --close file

                                                  close

 

 

 

                                        end tell

 

                              end tell

 

                    end repeat

          end try

 

 

 

 

end repeat

 

on getFolderPath(tName, folderPath)

          tell application "Finder" to tell folder folderPath

                    if not (exists folder tName) then

                              return (make new folder at it with properties {name:tName}) as string

                    else

                              return (folder tName) as string

                    end if

          end tell

end getFolderPath

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Mar 14, 2012 11:11 AM (in response to MattJayC)

    You can't use a filter reference form on regular script objects such as lists, it can only be used on application objects.  In your statement, just use the Finder (or System Events) to get the desired fies of a folder, e.g.

     

    tell application "Finder" to set filelist to files of alias theFolder whose file type is in the type_list or name extension is in the extension_list

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Mar 15, 2012 7:58 AM (in response to MattJayC)

    I don't have Photoshop either, but you should be able to add log statements and check the Script Editor's event log to see what is going in and coming out of any given area.  You also should not blindly enclose large areas of your script in try statements without error handling, since any error would then just be eaten - better to at least output the error using a log or display dialog statement, for example:

     

    try

      -- some suspect code

      -- an error here jumps to the following error handler

    on error errorMessage number errorNumber

      -- show or otherwise handle the error

      log errorMessage

      display dialog errorMessage

    end try

  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    Mar 15, 2012 8:22 AM (in response to MattJayC)

    Hi,

     

    the problem are :

     

    1- several variables are not defined

    example: repeat with alist in filelist

                                  set thefile to aFiles as alias -- aFiles is not defined

     

    2- The conditions (if, else and end if) are not properly placed

     

    3- Don't use a block try before the script is working correctly, because it's easier to know the error

     

     

    Try this script

    -----------------------------------------------------------

     

    property type_list : {"8BPS"}

    property extension_list : {"psd"}

     

    --Setup list of folders and process details of folders

     

    set dtF to paragraphs of (do shell script "ls -F ~/Desktop | grep '/' | cut -d'/' -f1")

    repeat with i from 1 to number of items in dtF

        set this_item to item i of dtF

        if this_item does not start with "2_" and this_item does not start with "Hot" and this_item does not start with "Press" then

           

            set folderPath to this_item

            set {oldTID, my text item delimiters} to {my text item delimiters, ":"}

            set folderName to last text item of folderPath

            set my text item delimiters to "_WK"

            set FolderEndName to last text item of folderName

            set brandName to first text item of folderName

            set my text item delimiters to "_PSD"

            set weekNumber to first text item of FolderEndName

            set my text item delimiters to oldTID

            set theFolder to ("Hal 9000:Users:matthew:Desktop:" & folderName)

           

           

           

            --set up names to destination folders and create locally based on brand name and week number

           

            set this_local_folder to "Hal 9000:Users:matthew:Pictures:2011-2012"

            set localWeekFolder to my getFolderPath("WK" & weekNumber, this_local_folder)

            set localBrandFolder to my getFolderPath(brandName, localWeekFolder)

            set localBrandFolder_Low_Res to my getFolderPath(brandName & "_WK" & weekNumber & "_LR", localBrandFolder)

            set localBrandFolder_High_Res to my getFolderPath(brandName & "_WK" & weekNumber & "_HR", localBrandFolder)

           

            --taking the folder identify which process it must follow.

           

           

           

           

            if brandName is equal to "Diva" then

                tell application "Finder" to set filelist to (files of alias theFolder whose file type is in the type_list or name extension is in the extension_list)

                repeat with thefile in filelist

                    set thefile to thefile as string -- convert Finder item to string

                    --DIVA Folder Photoshop Process

                    tell application "Adobe Photoshop CS5.1"

                        activate

                        open alias thefile

                        set myOptions to {class:JPEG save options, quality:12}

                        tell current document

     

                            --If the quick mask mode has been left on then delete the channel Quick Mask

                            if (quick mask mode) then delete channel "Quick Mask"

                            delete layer "Original Image" flatten

                            resize image resolution 300 resample method none

     

                            --sharpen image

                            filter current layer using unsharp mask with options {amount:80, radius:3.2, threshold:0}

     

                            save in (localBrandFolder_High_Res & name) as JPEG with options myOptions without copying

                           

                            -- example : duplicate file (localBrandFolder_High_Res & name) to folder localBrandFolder_Low_Res

                            --my duplicateFile(localBrandFolder_High_Res & name, localBrandFolder_Low_Res) --  use the handler to duplicate file

                           

                            --Prepare for Low RES by resetting image history

                            set current history state to history state 3

     

                            resize image width 1020

                            resize image resolution 300 resample method none

                            filter current layer using unsharp mask with options {amount:80, radius:3.2, threshold:0}

     

                            --add save to lowResFolder with same options

                            save in (localBrandFolder_Low_Res & name) as JPEG with options myOptions without copying

                          

                            --my duplicateFile(localBrandFolder_Low_Res & name, aFolderPath) --  use the handler to duplicate file

                            close

                        end tell

                    end tell

                   

                end repeat

                -- end brandName = "Diva"

            else if brandName is equal to "another brandName 1" then

                --another code

               

               

            else if brandName is equal to "another brandName 2" then

                --another code

               

               

               

            end if

           

        end if

    end repeat

     

    on duplicateFile(tFilePath, folderPath)

        tell application "Finder"

            duplicate file tFilePath to folder folderPath with replacing

        end tell

    end duplicateFile

     

     

    on getFolderPath(tName, folderPath)

        tell application "Finder" to tell folder folderPath

            if not (exists folder tName) then

                return (make new folder at it with properties {name:tName}) as string

            else

                return (folder tName) as string

            end if

        end tell

    end getFolderPath

  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    Mar 21, 2012 2:43 PM (in response to MattJayC)

    Hi,

     

    MattJayC wrote:

     

    This is how it looks.

     

    A few things i need to add.

     

    How can I save of time by not having to save to the multiple locations (i did try t using finder to duplicate last file of folder but that didn't work too well)

     

    Also the line where it saves to website_images in need to exclude those files that end in _2 and _3 (or files that are exactly 16 digits long).

     

    And finally if in photoshop it come across and error I need to skip the file and log it to a text file or just leave it open and continue.

     

    Also how do I have it run in the background so i can continue to use the computer?

    Here is the script :

     

    --------------------------

    property type_list : {"8BPS"}

    property extension_list : {"psd"}

    script o

        property theseNames : {}

    end script

    -- empty log file

    do shell script "echo  'Files not processed in Photoshop :'  > ~/Desktop/LogPhotoshopError.txt"

    set noError to true

     

     

    --Setup list of folders and process details of folders

     

     

    set dtF to paragraphs of (do shell script "ls -F ~/Desktop | grep '/' | cut -d'/' -f1")

    set tc to (count dtF)

    repeat with i from 1 to tc

        set folderName to item i of dtF --<:  is the folder name, no need to use text item delimiters -->":"

        if folderName does not start with "2_" and folderName does not start with "Hot" and folderName does not start with "Press" then

     

            set {oldTID, my text item delimiters} to {my text item delimiters, "_WK"}

            set FolderEndName to last text item of folderName

            set brandName to first text item of folderName

            set my text item delimiters to "_PSD"

            set weekNumber to first text item of FolderEndName

            set my text item delimiters to oldTID

     

            set theFolder to ("Hal 9000:Users:matthew:Desktop:" & folderName)

     

     

            --set up names to destination folders and create locally based on brand name and week number

     

            set this_local_folder to "Hal 9000:Users:matthew:Pictures:2011-2012"

            set localWeekFolder to my getFolderPath("WK" & weekNumber, this_local_folder)

            set localBrandFolder to my getFolderPath(brandName, localWeekFolder)

            set localBrandFolder_Low_Res to my getFolderPath(brandName & "_WK" & weekNumber & "_LR", localBrandFolder)

            set localBrandFolder_High_Res to my getFolderPath(brandName & "_WK" & weekNumber & "_HR", localBrandFolder)

     

     

            set this_Network_folder to "DCKGEN:Brands:Zoom:Brand - Zoom:Upload Photos:2012:"

            set networkWeekFolder to my getFolderPath("WK" & weekNumber, this_Network_folder)

            set networkBrandFolder to my getFolderPath(brandName, networkWeekFolder)

            set networkBrandFolder_Low_Res to my getFolderPath(brandName & "_WK" & weekNumber & "_LR", networkBrandFolder)

     

            set website_images to "DCKGEN:Website_Images:"

     

            --set up names to destination folders and create over Netwrok for FTP collection (based on a mounted drive)

            set this_ftp_folder to "Impulse:"

            set ftpWeekFolder to my getFolderPath("Week" & weekNumber, this_ftp_folder)

            set ftpBrandFolder to my getFolderPath(brandName, ftpWeekFolder)

            set ftpBrandFolder_Low_Res to my getFolderPath(brandName & "_WK" & weekNumber & "_LR", ftpBrandFolder)

            set ftpBrandFolder_High_Res to my getFolderPath(brandName & "_WK" & weekNumber & "_HR", ftpBrandFolder)

     

     

     

            --taking the folder identify which process it must follow.

            if brandName is equal to "Diva" then

                try

                    tell application "Finder" to set o's theseNames to (name of files of alias theFolder whose file type is in the type_list or name extension is in the extension_list)

                on error

                    set o's theseNames to {} -- no psd files or "8BPS"

                end try

     

                set numOfNames to (count o's theseNames)

                repeat with j from 1 to numOfNames

                    set thefile to theFolder & ":" & (item j of o's theseNames)

                    --    D   I   V   A    Folder Photoshop Process

     

                    tell application "Adobe Photoshop CS5.1"

                        -- I remove the command activate, Photoshop stay in background

                        try

                            open (alias thefile) showing dialogs never

                            set myOptions to {class:JPEG save options, quality:12}

                            tell current document

     

                                --If the quick mask mode has been left on then delete the channel Quick Mask

                                if (quick mask mode) then delete channel "Quick Mask"

                                (delete layer "Original Image") flatten

     

                                resize image resolution 300 resample method none

     

                                --sharpen image

                                filter current layer using unsharp mask with options {amount:80, radius:3.2, threshold:0}

     

                                save in (localBrandFolder_High_Res & name) as JPEG with options myOptions without copying

     

                                --get file path, return path of the JPEG file, work with (without copying)

                                -- (with copying) : it return path of PSD file

                                set newFile to file path --( return path of type alias )

     

                                -- duplicate file using the Finder  -->on duplicateFile(..)

                                my duplicateFile(newFile, {ftpBrandFolder_High_Res})

     

                                --Prepare for Low RES by resetting image history

                                set current history state to history state 3

     

                                flatten

     

                                resize image width 1920

                                resize image resolution 300 resample method none

                                filter current layer using unsharp mask with options {amount:80, radius:3.2, threshold:0}

     

                                --add save to lowResFolder with same options

                                save in (localBrandFolder_Low_Res & name) as JPEG with options myOptions without copying

     

                                set newFile to file path

                                set newFile2 to newFile as string -- for testing end of name

                                if newFile2 ends with "_2.jpg" or newFile2 ends with "_3.jpg" then -- exclude website_images

                                    my duplicateFile(newFile, {networkBrandFolder_Low_Res, ftpBrandFolder_Low_Res})

                                else

                                    my duplicateFile(newFile, {networkBrandFolder_Low_Res, ftpBrandFolder_Low_Res, website_images})

                                end if

     

                                close saving no

                            end tell

                        on error

                            set noError to false

                            my myLogs(thefile) -- write path to log file in Desktop

                            try

                                close saving no --if exists, close current document

                            end try

                        end try

                    end tell

     

                end repeat

     

                -- end brandName = "Diva"

            else if brandName is equal to "another brandName 1" then

                --another code

     

            else if brandName is equal to "another brandName 2" then

                --another code

     

            end if

        end if

    end repeat

    if not noError then do shell script "/usr/bin/open  ~/Desktop/LogPhotoshopError.txt'"

     

     

    on myLogs(t)

        try

            do shell script "echo " & (quoted form of t) & ">> ~/Desktop/LogPhotoshopError.txt"

        end try

    end myLogs

     

     

    on duplicateFile(tFile, foldersPath) -- tFile is an alias, foldersPath is a list of folder

        tell application "Finder" to repeat with folderPath in foldersPath

            with timeout of 200 seconds -- adjust it,  error if the copy  is longer that 200 seconds

                duplicate tFile to folder folderPath with replacing

            end timeout

        end repeat

    end duplicateFile

     

     

    on getFolderPath(tName, folderPath)

        tell application "Finder" to tell folder folderPath

            if not (exists folder tName) then

                return (make new folder at it with properties {name:tName}) as string

            else

                return (folder tName) as string

            end if

        end tell

    end getFolderPath

  • Frank Caggiano Level 7 Level 7 (22,760 points)
    Currently Being Moderated
    Mar 22, 2012 7:11 AM (in response to MattJayC)

    "/usr/bin/open  ~/Desktop/LogPhotoshopError.txt'"

    There's a single quote in front of the double quote.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.