0

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

Community
  • 1
  • 1

1 Answers1

0

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.

markhunte
  • 6,805
  • 2
  • 25
  • 44
  • That's terrific, Mark, thank you. I don't understand it, but it works. For instance, the Automation prior to the custom script sets pixelWidth, while compressionLevel is never set (or get) -- custom script still works. So simply each "Ask for text" sets up $1, $2, $3 etc? Then why do we need to set up pixelWidth while not compressionLevel in the Automation? I may be too far behind and you may not want to get into that, that's okay. I will read the links you provided; and appreciate the commented notation. Thank you for this helpful solution. -B – Bill Rudolph Yaber Russell Sep 19 '13 at 22:32
  • I have updated the image. (not sure if it helps) But basically the compression level IS picked up. The `set` and `get Variable` actions will accumulate any info passed to them into an array. So I did not bother saving the compression level into a `set variable` action because I could just pass it on. After a major slap to my forehead. Due to the fact that the `set` and `get Variable` actions had this behaviour is one I only remembered when I saw your example. So Thank You. – markhunte Sep 19 '13 at 23:59
  • It is helpful. And the array arrives in the order of the input compression level value, the input value set pixelWidth, then the string of fileURL? If so, I've got a foothold... – Bill Rudolph Yaber Russell Sep 20 '13 at 21:31