applescriptjpegconvertersdroplet

AppleScript Droplet to Convert PSD and TIF to JPG


There are a lot of examples of converters to JPG and i am trying to change one for my needs but i need little help please. The requirements are:

  1. It should be an AppleScript Droplet. I am using Script Editor ( for some reason Automator can't run a simple droplet drag and drop function for me ).
  2. The output folder for the JPGs should not be prompted by the user .. but set as a variable in the code permanently and easly changed.
  3. The quality ( compression ) of the converted JPG should also have to be easly customisable in the code.
  4. The converted JPG files have to be converted to Color profile Adobe RGB 1998 if necessary.

I know Image Events allow us to set the JPG compression like :

save openedFile as JPEG with compression level (low|medium|high)

but unfortunately i need more customisation.

A shell script will help me to set the level from 10 to 100 but unfortunately i can't implement the shell script properly. Little help please about points 3 and 4. Thank you !

on run
    display dialog "Please drag image files to this script to turn them into JPEGs"
end run
on open draggeditems
    set End_Folder to "Macintosh HD:Users:zzz:Desktop:End"
    
    repeat with currentFile in draggeditems
        tell application "Image Events"
            set openedFile to open (currentFile as alias)
            set fileLocation to the location of openedFile
            set fileName to the name of openedFile
            set Path_to_Converted_File to (End_Folder & ":" & text 1 thru -5 of fileName & ".jpg")

            do shell script "sips --setProperty formatOptions 10 " & openedFile
            
    save openedFile as JPEG in Path_to_Converted_File
            --save openedFile as JPEG  with compression level low  in Path_to_Converted_File (low|medium|high)
            
            close openedFile
        end tell
    end repeat
end open

Solution

  • Mixing up Image Events and sips is more confusing than useful, and since sips can perform the various options you are looking for (such as 3 & 4), it makes more sense to use it for everything. Setting a few variables for the various options will let you change them as needed, or if adding preferences or whatever. The sips man page will give you more details about the various options; I’ve added comments for the ones used in the following script:

    on run
        open (choose file with prompt "Select image files to turn into JPEGs:" with multiple selections allowed)
    end run
    
    on open draggeditems
        set destination to (((path to desktop folder) as text) & "End:") -- folder path (trailing delimiter)
        set format to "jpeg" -- jpeg | tiff | png | gif | jp2 | pict | bmp | qtif | psd | sgi | tga
        set extension to ".jpg" -- extension to match format
        set compression to 10 -- low | normal | high | best | <percent>
        set profile to quoted form of "/System/Library/ColorSync/Profiles/AdobeRGB1998.icc" -- POSIX path to color profile
        repeat with thisFile in draggeditems
            set theName to justTheName for thisFile
            set originalFile to quoted form of POSIX path of thisFile
            set convertedFile to quoted form of POSIX path of (destination & theName & extension)
            do shell script "sips -s format " & format & " -s formatOptions " & compression & " -m " & profile & space & originalFile & " --out " & convertedFile
        end repeat
    end open
    
    on justTheName for filePath
        tell application "System Events" to tell disk item (filePath as text)
            set {fileName, extension} to {name, name extension}
        end tell
        if extension is not "" then set fileName to text 1 thru -((count extension) + 2) of fileName -- just the name part
        return fileName
    end justTheName
    

    Edited to add: The shell script is expecting POSIX paths, so the aliases passed to the open handler are coerced and quoted in the event they contain spaces.