automatorsips

Apples Automator to make JPEG, asking for compression level and dimensions


This is a followup to Apple's Automator: compression settings for jpg?

It works. However I am failing at modifying it to make it more flexible.

I am incorporating Sips into Automator to try to create a droplet that changes an image file to a jpeg, of a particular quality and dimensions. The automator app asks for the compression level and pixel width, then spits out the requested file. Except... mine doesn't. The scripting (my lack of programming knowledge) is my weak link.

This is what I've done that's not working... Please see: http://i.imgur.com/6cofqi7.png


Solution

  • There are two mistakes in the code that the poster who wrote the code made.

    When calling a variable in shell. You must prepend it with "$"

    so where the have missed this out is what is stopping the code to work as it should.

    The lines without the $ are: compressionLevel=file

    and

    sips -s format jpeg -s formatOptions compressionLevel $file --out ${filename%.*}.jpg

    The corrected code: should be: compressionLevel=$file

    and

    sips -s format jpeg -s formatOptions $compressionLevel $file --out ${filename%.*}.jpg


    UPDATED ANSWER* I noticed you have pixel width.

    So I have change the code to accommodate it.

    I have also added a "_" to the end of the out put file which you can remove if you want. The reason I put it there is so I do not overwrite originals and create in effect copies.

    enter image description here

    compressionLevel=$1
    pixalWidth=$2
    
    
    i=1 # index of item
    
    for item    # A for loop by default loop through $1, $2, ...
    do
    
    if [ $i -gt 2 ]; then # start at index 3  #-- array indexes start at 0. 0 is just a "-" in this case so we totally ignor it. we are using items 1 & 2 for the sip options, the rest for file paths. the index "i" is used to keep track of the array item indexes.
    
         echo "Processing $item"
    
     sips -s format jpeg -s formatOptions $compressionLevel --resampleWidth $pixalWidth $item --out ${item%.*}_.jpg
    
    fi
    
        ((i++))
    done
    
    osascript -e 'tell app "Automator" to display dialog "Done." buttons {"OK"}'
    

    I would suggest you do some reading on shell scripting to get some basics down.

    there are plant of references on the web. And Apple have this.

    I am sure if you ask the question others can give you some good starting points first search this site for similar question as I am sure it base been asked a thousand times.