Previous 1 2 Next 17 Replies Latest reply: Jul 30, 2014 8:46 AM by VikingOSX
SouthernAtHeart Level 1 Level 1 (0 points)

Is there a way to automatic a process to zip up several files?  What I’m envisioning is, that I go to the finder, and locate a specific folder.  Then I hit some hot key or a custom button on the toolbar that will zip up 8 files in that folder.  The file names of these 8 files will always be different, but the file extensions will always be the same.  There are 8 file extensions, 1 each of:

.GBL

.GBO

.GBS

.GKO

.GTL

.GTO

.GTS

.TXT

There will never be more than 1 file of each file extension in the folder.  And if it’s possible, call this archive “GerberFiles”, and move it to my desktop.


Mac OS X (10.7.3)
  • VikingOSX Level 6 Level 6 (8,900 points)

    Will the eight files arrive in the folder together, or at different time intervals? After they are zipped, what becomes of these eight files? Do they remain in the folder, archived, or deleted?

  • SouthernAtHeart Level 1 Level 1 (0 points)

    They arrive (are created) from a CAD program (eagle CAD), all at the same instance.  They can be left there, or deleted, either way.  I guess deleted would be fine.  I need them zipped, to upload to the internet, then I'll be done with them.

  • VikingOSX Level 6 Level 6 (8,900 points)

    This took longer than planned, but had considerable testing. The final form is an automator application that can sit on your Desktop. You double-click it, and it will prompt you for a folder containing the Gerber files. After you select that folder, it will screen for only the specified file extensions that you stated, and then add each file to the GerberFiles.zip archive. Only the filename, and not the folder path or enclosing folder name is added to the archive. After 8 files have been processed, it provides an AppleScript dialog, and then makes a backup folder of the files that went into the archive. I have left the line that removes the original folder contents -- commented for now.

     

    Here is a screen capture of the Automator workflow, and a superimposed application dialog.

     

    Screen Shot 2014-07-19 at 8.19.07 PM.png

     

    When you launch Automator, select New Document, and then Application, and then click Choose. Look for the Library item on the left named Files and Folders, and just drag the first item, Ask for Finder Items, across to the large workflow window and drop it. Use the above image as a guide. Now, under Library > Utilities, drag the Run Shell Script action below the Ask for Finder Items action and release it. Select and remove its contents. Change the Pass input to as Arguments. Copy/Paste the following shell script source into the now empty Run Shell Script window.

     

    You will need to insert the first line from the above Shell Script content back into the Workflow, as the community software won’t let me post a shell script with its invocation line in the code.

     

    Code:

    cnt=0

    # command-line argument or passed input as arguments

    gfolder="$@"

    garchive="$@/GerberFiles.zip"

    # keep one iteration of files in reserve

    glastfolder="$HOME/glast"

     

     

    shopt -s extglob

     

     

    function ASDialog () {

     

     

    `osascript <<-AppleScript

     

     

        tell application "System Events"

     

     

            set filecnt to "$1"

            set archive to "$2"

            set msg to "Zipped files: " & filecnt & return & return & "Archive moved to Desktop: " & archive

            display dialog msg with title "Processing Complete" giving up after 15

     

     

        end tell

        return quit

     

     

    AppleScript`

    }

    cd $gfolder

    for file in \( * \)

    do

        [[ $((++cnt)) > 9 ]] && break

     

     

        case "${file}" in

            *.+(GBL|GBO|GBS|GKO|GTL|GTO|GTS|TXT))

      # zip the files, but not the path or enclosing folder

            /usr/bin/zip -g ${garchive} `/usr/bin/basename "${file}" "${gfolder}"` -x \*.DS_Store &>/dev/null

            ;;

     

     

            *)

            ;;

        esac

    done

    shopt -u extglob

    # provide user completion dialog

    ASDialog $((cnt - 2)) "${garchive##*/}"

    /bin/mv ${garchive} ~/Desktop/${garchive##*/}

    # keep one iteration of Gerber files as backup

    /usr/bin/ditto "${gfolder}" "${glastfolder}"

    #/bin/rm -f "${gfolder}"/*

    exit 0

  • SouthernAtHeart Level 1 Level 1 (0 points)

    TThanks.  I will test it monday, but it looks great.  Is there a way, and is it allowed for me to tip you for your help and time?

  • VikingOSX Level 6 Level 6 (8,900 points)

    No tip process provided or expected. This is a community of international volunteers, not Apple employees.

     

    I decided to clean up my script code from the previous post. An array of the processed files that went into the archive, is now passed to AppleScript in the shell function. These files appear in the completion dialog as a reminder of what went into the Desktop archive. Do to the increased script size, I only screen captured the Run Shell Script portion of the original Automator workflow. The new script code follows this screen shot, and should replace what I posted earlier in the Do Shell Script Automator action.

     

    I am looking into changing this into a Folder Action, so that the act of your application saving into the Gerber folder, would automatically trigger processing, without the current interactive component requiring you to specify the folder.

     

    This script has some limitations. If you do not clear files from the folder that the application saves too, the script is unaware of processing history, and will grab the first eight documents found, which could include new and processed files in the resultant archive. This would be an issue with a folder action as well. May need more information about frequency and possibly checking creation dates.

     

    Here is today’s revision screen shot.

     

    Screen Shot 2014-07-20 at 3.45.48 PM.png

     

    Code

    # if testing in Terminal, supplied folder name must have relative "~" or

    # full path name. Automator will pass full path from folder chooser.

    # VikingOSX, July 2014, Apple Support Communities

     

    # array for processed files that also doubles as a counter

    Processed=()

    gfolder="$@"

    garchive="${gfolder}/GerberFiles.zip"

    # keep one iteration of files in reserve

    glastfolder="$HOME/glast"

     

     

    shopt -s extglob

     

     

    function ASDialog () {

     

     

    `osascript <<-AppleScript

     

     

            set filecnt to "${1}"

            set archive to "${2}"

            -- remaining arguments are passed processed array items

            set processed to "${@: 3}"

            set pcnt to count of words of processed

            set msg to ""

            set msg to msg & "Archive moved to Desktop: " & archive & return

            set msg to msg & "Processed Files: " & pcnt & return & return

            repeat with ndx from 1 to pcnt

                set msg to msg & tab & (word ndx of processed) & return

            end repeat

     

     

            tell application "System Events"

                display dialog msg with title "Processing Complete" giving up after 20

            end tell

        return quit

     

     

    AppleScript`

    }

     

     

    cd "$@"

    for file in \( * \)

    do

        [[ ${#Processed[@]} -gt 8 ]] && break

     

     

        case "${file}" in

            *.+(GBL|GBO|GBS|GKO|GTL|GTO|GTS|TXT))

            Processed+=( "${file}" )

            # do not zip the enclosing folder path, only the filename

            /usr/bin/zip -g ${garchive} `/usr/bin/basename "${file}" "${gfolder}"` -x \*.DS_Store &>/dev/null

            ;;

            *)

            ;;

        esac

    done

    shopt -u extglob

    # provide user completion dialog

    ASDialog "${#Processed[@]}" "${garchive##*/}" "${Processed[@]}"

    unset Processed

    /bin/mv ${garchive} ~/Desktop/${garchive##*/}

    # keep one iteration of Gerber files as backup

    /usr/bin/ditto "${gfolder}" "${glastfolder}"

    #/bin/rm -f \{ "${gfolder}"/* \)

    exit 0

  • VikingOSX Level 6 Level 6 (8,900 points)

    Errata: Last line of code. Should read:

     

    #/bin/rm -f \( “${gfolder}”/* \)

  • SouthernAtHeart Level 1 Level 1 (0 points)

    If you do not clear files from the folder that the application saves too, the script is unaware of processing history, and will grab the first eight documents found, which could include new and processed files in the resultant archive. This would be an issue with a folder action as well. May need more information about frequency and possibly checking creation dates.

    I'm not sure what you mean by this, but it doesn't have to save anything or make backups, either.

    I got it working. I notice it gives a message like "Processed 16 files".  With a little testing I've decided that's how many files were in the folder it looked through?  But the strange thing is, that the number of files in the last instance I ran the code, to the best that I can tell.  Anyway, it works fine for me.  Unless there's some temporary folder that will get filled up, I didn't know what you meant by that part.

  • VikingOSX Level 6 Level 6 (8,900 points)

    As long as you are removing folder files after they are archived, then new files written to the folder by your application will not be mixed with the old files. That was my concern regarding the script. The last line of the code is currently commented to prevent these removals. Remove the comment  “#” character from the line beginning to perform the removals.

     

    I wrote the script to process 8 files at a time per your original post. Unless you modified the code, it should not be reporting that it archived 16 files, as the loop is terminated after 8 have been processed. The following test in the code that I provided should be revised as an equal test, not greater than. This is comparing the number of items in the array to 8, and when 8 have been processed (zipped), it will stop and pop the dialog.

     

    [[ ${#Processed[@]} -eq 8 ]] && break

     

    Again, if you want to process more files at a time, the code is reasonably clear on where to make modifications.

     

    Glad it is working for you.

  • SouthernAtHeart Level 1 Level 1 (0 points)

    Screen Shot 2014-07-23 at 9.48.26 PM.png

    Thanks.  I uncommented that line, so it deletes to old gerber files.  I'm not sure why it gave me this message about 16 files, as these 8 files are the only files that were in that folder.  But I think the next time I use the utility, it will say "Processed Files: 8", regardless of how many files are in the folder I select.

  • SouthernAtHeart Level 1 Level 1 (0 points)

    Yikes!

    Not sure what happened, but I just tried this on a real folder (not a test folder/files), and it deleted EVERYTING out of that folder!

    And I don't see the files in the trash bin, either.

     

    I had uncommented the line at the very end like you said:

    /bin/rm -f \{ "${gfolder}"/* \)

     

    This is the code in my automator:

    #! /bin/bash

    # if testing in Terminal, supplied folder name must have relative "~" or

    # full path name. Automator will pass full path from folder chooser.

    # VikingOSX, July 2014, Apple Support Communities

     

    # array for processed files that also doubles as a counter

    Processed=()

    gfolder="$@"

    garchive="${gfolder}/GerberFiles.zip"

    # keep one iteration of files in reserve

    glastfolder="$HOME/glast"

     

     

    shopt -s extglob

     

     

    function ASDialog () {

     

     

    `osascript <<-AppleScript

     

     

            set filecnt to "${1}"

            set archive to "${2}"

            -- remaining arguments are passed processed array items

            set processed to "${@: 3}"

            set pcnt to count of words of processed

            set msg to ""

            set msg to msg & "Archive moved to Desktop: " & archive & return

            set msg to msg & "Processed Files: " & pcnt & return & return

            repeat with ndx from 1 to pcnt

                set msg to msg & tab & (word ndx of processed) & return

            end repeat

     

     

            tell application "System Events"

                display dialog msg with title "Processing Complete" giving up after 20

            end tell

        return quit

     

     

    AppleScript`

    }

     

     

    cd "$@"

    for file in \( * \)

    do

        [[ ${#Processed[@]} -gt 8 ]] && break

     

     

        case "${file}" in

            *.+(GBL|GBO|GBS|GKO|GTL|GTO|GTS|TXT))

            Processed+=( "${file}" )

            # do not zip the enclosing folder path, only the filename

            /usr/bin/zip -g ${garchive} `/usr/bin/basename "${file}" "${gfolder}"` -x \*.DS_Store &>/dev/null

            ;;

            *)

            ;;

        esac

    done

    shopt -u extglob

    # provide user completion dialog

    ASDialog "${#Processed[@]}" "${garchive##*/}" "${Processed[@]}"

    unset Processed

    /bin/mv ${garchive} ~/Desktop/${garchive##*/}

    # keep one iteration of Gerber files as backup

    /usr/bin/ditto "${gfolder}" "${glastfolder}"

    /bin/rm -f \{ "${gfolder}"/* \)

    exit 0

  • VikingOSX Level 6 Level 6 (8,900 points)

    There should be a glast folder in your home directory that contains the previous group of files, so nothing was lost. In the Terminal, put them back in the gfolder location:

     

    /usr/bin/ditto ~/glast ~/your-folder-used-for-the-gerber-files

     

    Change: [[ ${#Processed[@]} -gt 8 ]] && break to [[ ${#Processed[@]} -eq 8 ]] && break  as I suggested earlier. This will stop processing when it has found 8 files, not 9. Do not use a word processing program to make these changes. Use a programmer’s editor instead. Suggest the free TextWrangler from the OS X app store, if you don’t have one.

     

    If you are running this script from the Terminal in your login directory, do not forget to specify a relative, or full path to the folder you want it to process:

     

    ./gfolder.sh ~/gerberfolder

     

    ./gfolder.sh /Users/yourname/gerberfolder

     

    If you saved this as an Automator application to your Desktop, then the process of choosing the folder will pass the full path and folder name to the application.

  • SouthernAtHeart Level 1 Level 1 (0 points)

    VikingOSX wrote:

     

    There should be a glast folder in your home directory that contains the previous group of files, so nothing was lost. In the Terminal, put them back in the gfolder location:

     

    /usr/bin/ditto ~/glast ~/your-folder-used-for-the-gerber-files

     

    Change: [[ ${#Processed[@]} -gt 8 ]] && break to [[ ${#Processed[@]} -eq 8 ]] && break  as I suggested earlier. This will stop processing when it has found 8 files, not 9. Do not use a word processing program to make these changes. Use a programmer’s editor instead. Suggest the free TextWrangler from the OS X app store, if you don’t have one.

     

    If you are running this script from the Terminal in your login directory, do not forget to specify a relative, or full path to the folder you want it to process:

     

    ./gfolder.sh ~/gerberfolder

     

    ./gfolder.sh /Users/yourname/gerberfolder

     

    If you saved this as an Automator application to your Desktop, then the process of choosing the folder will pass the full path and folder name to the application.

    I made this change and tested it, but it still deleted ALL the files in the chosen folder.  I'd really like to try a different approach.  Just using applescript, since I understand it and can 'read' it better.  I've figured out this, which works partially, but not quite.  For some reason it doesn't do the actual moving of the files, I'm not sure why.  Once I get them moved, I should be able to zip them up...

  • SouthernAtHeart Level 1 Level 1 (0 points)
    set theFolder to choose folder with prompt "Please select a folder that contains the Gerber Files:"
    set GerberExtensions to {"GBL", "GBO", "GBS", "GKO", "GTL", "GTO", "GTS", "XLN"}
    tell application "Finder"
      set newFolder to make new folder at desktop with properties {name:"GerberFiles"}
      move (every file of theFolder whose name extension is in GerberExtensions) to newFolder
    
    end tell
    
    
    
  • VikingOSX Level 6 Level 6 (8,900 points)

    Based on your original discussion, I wrote a Bash script that was thoroughly tested on the command-line and as an Automator application. It works. I knew that the best use of my time was to do this in a Bash script, as I have all to often found a time sink differential between what AppleScript Editor compiles, and actual clean running code. Your AppleScript example is logical code that compiles ok, but doesn’t work — even when updated with simple examples from the Internet that are claimed to move files from one folder to another — and still fail. After an hour of this, I gave up on it. Life is too short.

     

    My understanding was that eight documents of specified extensions would be written into the source folder. If these same exact files are overwritten each time by the source application, then there is no reason to remove them. If additional files with different basename are added to this folder, then the script cannot differentiate old from new, and will process the first eight that it encounters and zip them. Possibly not what you want, which is why the remove option is at the end of the script.

     

    Today, I added some alphanumeric file names (echo1.*), and this messed up the AppleScript file count, and filename display in the dialog. I have fixed and tested these changes. Just replace the body of the AppleScript here document with the following content:

            set filecnt to "${1}"

            set archive to "${2}"

            -- remaining arguments are passed processed array items

            set processed to "${@: 3}"

            set tid to AppleScript's text item delimiters

            set AppleScript's text item delimiters to space

            set pcnt to count of (text items of processed)

            set msg to ""

            set msg to msg & "Archive moved to Desktop: " & archive & return

            set msg to msg & "Processed Files: " & pcnt & return & return

            repeat with ndx from 1 to pcnt

                set msg to msg & tab & (text item ndx of processed) & return

            end repeat

            set AppleScript's text item delimiters to tid

     

     

            tell application "System Events"

                display dialog msg with title "Processing Complete" giving up after 20

            end tell

        return quit

Previous 1 2 Next