Has anyone else noticed that the file sizes created by Image Events JPEG compression medium and high are the same?
Here’s the code I am using. Save it as an application and drop a PNG file on it.
on open myFiles
tell application "Image Events" to launch
repeat with myFile in myFiles
tell application "Image Events"
set myItem to open myFile
set myFileName to the name of myFile
end tell
tell application "Finder"
set newItem to (container of myFile as string) & "JPG-low " & myFileName
end tell
tell application "Image Events"
save myItem in newItem as JPEG with compression level low
end tell
tell application "Finder"
set newItem to (container of myFile as string) & "JPG-medium " & myFileName
end tell
tell application "Image Events"
save myItem in newItem as JPEG with compression level medium
end tell
tell application "Finder"
set newItem to (container of myFile as string) & "JPG-high " & myFileName
end tell
tell application "Image Events"
save myItem in newItem as JPEG with compression level high
end tell
end repeat
end open
Using your script I made the same experience. But with sips I get much better results (= different results for low, normal, high, best, etc.).
sips -s format jpeg -s formatOptions normal /Users/martin/Desktop/test.png --out /Users/martin/Desktop/test-normal.jpg
sips -s format jpeg -s formatOptions high /Users/martin/Desktop/test.png --out /Users/martin/Desktop/test-high
sips -s format jpeg -s formatOptions best /Users/martin/Desktop/test.png --out /Users/martin/Desktop/test-best
As you might already know, Image Events is just a wrapper for sips. And you can easily access sips with the «do shell script» command from within AppleScript. There are a lot of examples here on the forums.
Here’s some working code to create a droplet app to take images and spit them out at varying JPEG levels. I’m sure someone more experienced with AppleScript could have done this cleaner so any touch ups are welcome. Save this code as a droplet and drag one or more PNG files onto the applet. Thanks to Martin for the SIPS pointers.
-- Alex Zavatone
on xrun()
set myFile to "Macintosh HD:01.jpg"
Process(myFile)
end xrun
on open myFiles
set oldDelimiter to AppleScript's text item delimiters
repeat with myFile in myFiles
set ItemInfo to the info for myFile
if not folder of ItemInfo then
Process(myFile)
end if
end repeat
set AppleScript's text item delimiters to oldDelimiter
-- set myFile to "Macintosh HD:01.jpg"
end open
on Process(myFileAlias)
set myFile to myFileAlias as string
set AppleScript's text item delimiters to ":"
set myItemCount to the number of text items in myFile
set myFileName to the last text item of myFile
set myFilePath to text items 1 through (myItemCount - 1) of myFile
set AppleScript's text item delimiters to "."
set myItemCount to the number of text items in myFileName
set myExtension to the last text item of myFile
set myShortFilename to text items 1 through (myItemCount - 1) of myFileName
set AppleScript's text item delimiters to ""
set myPosixFile to the POSIX path of myFile
-- display dialog "myPosixFile " & myPosixFile
set myCompressionList to [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
repeat with myCompressionRate in myCompressionList
set AppleScript's text item delimiters to ":"
set newItem to myFilePath & myShortFilename as string
set AppleScript's text item delimiters to ""
set newItem to newItem & " -" & myCompressionRate & ".jpg" as string
-- display dialog "newItem " & newItem
set myPosixNewItem to the POSIX path of newItem
-- display dialog "myPosixNewItem " & myPosixNewItem
set myShellString to "sips -s format jpeg -s formatOptions " & myCompressionRate & " '" & myPosixFile & "' --out '" & myPosixNewItem & "'"
-- do it
do shell script myShellString
end repeat
end Process
For what it’s worth, here’s an optimisation of your path handling and shell scripting:
on Process(myFileAlias)
set myPosixFile to quoted form of POSIX path of myFileAlias
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set myRootPath to text 1 thru text item -2 of myPosixFile & " -"
set AppleScript's text item delimiters to astid
set myShellString to ""
repeat with myCompressionRate from 10 to 100 by 10
set myPosixNewItem to myRootPath & myCompressionRate & ".jpg'"
-- display dialog "myPosixNewItem " & myPosixNewItem
set myShellString to myShellString & ("sips -s format jpeg -s formatOptions " & myCompressionRate & " " & myPosixFile & " --out " & myPosixNewItem & " ; ")
end repeat
-- do it
do shell script myShellString
end Process
This is great. Nigel, you are the best. As always.
My slightly stripped down droplet version, which just converts dropped files to jpeg with a fixed compression rate:
on open (theseFiles)
repeat with thisFile in theseFiles
set myPosixFile to quoted form of POSIX path of thisFile
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set myRootPath to text 1 thru text item -2 of myPosixFile & " -"
set AppleScript's text item delimiters to astid
set myShellString to ""
set myCompressionRate to 100
set myPosixNewItem to myRootPath & myCompressionRate & ".jpg'"
set myShellString to myShellString & ("sips -s format jpeg -s formatOptions " & myCompressionRate & " " & myPosixFile & " --out " & myPosixNewItem & " ; ")
do shell script myShellString
end repeat
end open