Skip navigation

Copy Folder Structure and convert contents from PSD to JPG

863 Views 12 Replies Latest reply: Mar 6, 2013 7:56 PM by red_menace RSS
MattJayC Level 1 Level 1 (0 points)
Currently Being Moderated
Mar 1, 2013 7:23 AM

I've found this and I am just trying to work out where I can place a photoshop file and then save as jpg in the copied folder location?



-- Open this script in script editor and save with the options:

--   * File type: Application

--   * Uncheck the option "Startup screen"

-- Now you can either drag and drop a set of folders on the application or just

-- run the application from the Finder and it will ask you to choose a folder.


on run

          set source_folder to choose folder with prompt "Select folder to be duplicated:" as string

          my do_main_script(source_folder)

end run


on open of source_folder_list

          repeat with i from 1 to number of items in the source_folder_list

                    set this_folder_path to item i of the source_folder_list as string

                    if last character of this_folder_path is ":" then

                              my do_main_script(this_folder_path)

                    end if

          end repeat

end open


on do_main_script(source_folder)

          tell application "Finder" to set source_folder to folder (source_folder)

          tell application "Finder" to set the target_folder to (parent of source_folder)


          if source_folder is not "" and target_folder is not "" then

                    set new_folder_name to (name of source_folder as string) & " duplicate"

                    set source_folder to source_folder as string

                    set target_folder to target_folder as string


                    my create_new_folder(target_folder, new_folder_name)

                    my duplicate_folder_structure(source_folder, target_folder & new_folder_name & ":")

          end if

end do_main_script


on duplicate_folder_structure(source_folder, target_folder)

          tell application "Finder"


                              get name of folders of folder (source_folder)

                              set folder_list to result

                              repeat with i from 1 to number of items in the folder_list

                                        set this_folder_name to item i of the folder_list as string

                                        my create_new_folder(target_folder, this_folder_name)

                                        my duplicate_folder_structure(source_folder & this_folder_name & ":", target_folder & this_folder_name & ":")

                              end repeat

                    end try

          end tell

end duplicate_folder_structure


on create_new_folder(target_folder, new_folder_name)

          tell application "Finder"


                              if not (exists item (target_folder & new_folder_name)) then

  make new folder at folder target_folder with properties {name:new_folder_name}

                              end if

                    end try

          end tell

end create_new_folder

iMac, Mac OS X (10.6.8)
  • rccharles Level 5 Level 5 (5,115 points)

    run script in script editor to see what it does.  add log statement for more info. click on event log.


    Picture 13.png




    It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.
        Author: rccharles
        For testing, run in the Script Editor.
          1) Click on the Event Log tab to see the output from the log statement
          2) Click on Run
    on run
        -- Write a message into the event log.
        log "  --- Starting on " & ((current date) as string) & " --- "
        --  debug lines
        set desktopPath to (path to desktop) as string
        log "desktopPath = " & desktopPath
    end run
  • red_menace Level 6 Level 6 (14,275 points)

    Although the script you found is a bit convoluted, it looks like it is just duplicating a folder structure.  What are you trying to do with your script - match a converted file's path into the new folder structure?  Convert all of the files from the old structure to the new?

  • red_menace Level 6 Level 6 (14,275 points)

    Give the following script a try.  It prompts for source and destination folders, copies the source folder structure, and then puts the processed image files into the same folder path in the destination.  It keeps track of the source and destination folders between runs, but puts up a dialog to allow you to change them.


    property sourceFolder : missing value
    property destinationFolder : missing value
    on run -- application double-clicked or run from the AppleScript Editor
         set theImages to (choose file of type "PSD" with multiple selections allowed)
         open theImages
    end run
    on open theItems -- dropped items
    end open
    on processStuff(theStuff) -- convert files to a matching folder structure
         repeat with anItem in theStuff
              set anItem to anItem as text
              tell application "Finder" to set extension to name extension of file anItem
              set filePath to text ((length of sourceFolder) + 1) thru -1 of anItem -- strip source folder
              if extension is not "" then set filePath to text 1 thru -((count extension) + 2) of filePath -- strip extension
              tell application "Image Events"
                   set theImageReference to open file anItem
                   tell theImageReference
                        scale to size 1000
                        save in destinationFolder & filePath & ".JPG" as JPEG
                   end tell
              end tell
         end repeat
    end processStuff
    on checkSetup() -- make sure source and destination folders are set and structure is copied
         if (sourceFolder is not missing value) and (destinationFolder is not missing value) then
              display alert "Folder Settings" message "The follwing folders will be used - is this OK?" & return & return & "Source folder:" & return & sourceFolder & return & return & "Destination folder:" & return & destinationFolder & return & return buttons {"Cancel", "Change", "OK"} cancel button "Cancel"
              if button returned of the result is "Change" then
                   set sourceFolder to missing value
                   set destinationFolder to missing value
              end if
         end if
         if (sourceFolder is missing value) or (destinationFolder is missing value) then
              set sourceFolder to (choose folder with prompt "Choose a source folder - the structure of this folder will be copied into the destination folder:") as text
              set destinationFolder to (choose folder with prompt "Choose a destination folder - the structure of the source folder will be copied into this folder:") as text
              copyStructure(sourceFolder, destinationFolder)
         end if
    end checkSetup
    to copyStructure(source, destination) -- copy the folder structure of a source folder into a destination folder
         set {source, destination} to {source as alias, destination as alias}
         tell application "Finder" to set theFolders to (folders of entire contents of source) as alias list
         log (count theFolders)
         set folderNames to ""
         repeat with someFolder in theFolders -- set up the folder names parameter for the shell script
              set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
              set baseName to text ((count (source as text)) + 1) thru -2 of (someFolder as text)
              set {namePieces, AppleScript's text item delimiters} to {text items of baseName, "/"}
              set folderNames to folderNames & space & quoted form of (namePieces as text)
              set AppleScript's text item delimiters to tempTID
         end repeat
         do shell script "cd " & quoted form of POSIX path of destination & "; mkdir -p" & folderNames
    end copyStructure

  • red_menace Level 6 Level 6 (14,275 points)

    The error sounds like you are dropping folders or just changed choosing files to choosing folders - the file keyword would be incorrect in that situation.  The script copies the structure of the selected folder, but then individual files are selected to process (this is what you were doing with your original code pieces) - you now want to get a folder (or maybe multiple folders) and process the entire contents?  In that case, I am going to guess that the original folder isn't going to be changed or added to?


    The article you referenced is just about shortening the name/path of a volume, which doesn't really apply here since you aren't typing anything.  I'm not sure what you are going to do with symbolic links or aliases, since all that does is create a shortcut (more or less) to an item - you will still be reading the original item.

  • Frank Caggiano Level 7 Level 7 (22,745 points)

    You should be doing this in two steps. You can copy the folders/files using the Finder. Once there run a little script that goes through and changes the files.


    No sense in doing this in one step.

  • Frank Caggiano Level 7 Level 7 (22,745 points)

    You can probably salvage the code from red_menace's example above, the function processStuff().


    Basically you just want to walk the directory structure starting at the top level and use Image Events to convert psd files to jpg. As I said red_menace's code can probably be used for a good part of this just pull out the stuff that copied the files.



  • red_menace Level 6 Level 6 (14,275 points)

    The script I posted will keep track of the source and destination folders, so once they are set you can select groups of files at your leisure - files processed from the source folder will get placed into the matching folder in the destination.  I haven't had a chance to modify the script to process the entire contents of a folder.


More Like This

  • Retrieving data ...

Bookmarked By (1)


  • 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.