Jump to content

Resize and Compress Images workflow


Recommended Posts

Hi Guys,

I'm quite new to alfred and automation in general, so I hope you'll bear with me.

I'm trying to create a workflow, where a hotkey triggers an image (or selection of images) to be resized and then compressed either through the mac app "JPEGmini" or through "tinyPNG" online. I've divided this up into 4 steps that I'm trying to work on:

 

  1. Resize selection
  2. Determine whether it is a jpg/png
  3. If JPG compress on mac through JPEGmini
  4. If PNG compress online through tinyPNG

 

So this is what I have so far:

 

1. Resize Image (code by Acidham, altered by ilium007)

on alfred_script(target_width)
  
  set theCount to 0
  set currSelection to {}

  tell application "Finder"
    set theSelection to selection
  end tell
    
  repeat with this_file in theSelection
    -- try to get a jpg or png
    try
      tell application "Finder"
        set myPath to POSIX path of (this_file as text)
      end tell
    
      tell application "Image Events"
        -- open the image file
        set this_image to open myPath
        -- get dimensions of the image
        copy dimensions of this_image to {W, H}
        -- calculate scale factor with given width and scale the image
        set scale_factor to target_width / W
        scale this_image by factor scale_factor
        save this_image in myPath
        close this_image
        -- get new image width and height for output
        set new_W to round (W * scale_factor) rounding as taught in school
        set new_H to round (H * scale_factor) rounding as taught in school
        set new_size to new_W & " x " & new_H
      end tell
        -- set the_return to (W & " x " & H & " → " & new_size) as text
        -- return the_return
        set theCount to theCount + 1
    on error errStr number errorNumber
      return "No image file found!"
    end try
  end repeat
  return ("Processed " & theCount & " images") as text
end alfred_script


2. Haven't worked on this yet.

3. JPEG compress with JPEGmini

- Notification and set filePath

on alfred_script(q)
 display notification with title "Optimising JPEG ..." subtitle "Path: " & (the clipboard as text)
  tell application "Finder"
	set theItems to selection
	set filePath to (POSIX path of (the selection as alias))
  end tell
end alfred_script

- Copy file path to clipboard action through Alfred

 

- Activate JPEGmini and use system events to open file through paste from clipboard

on alfred_script(q)
tell application "JPEGmini" to activate
delay 2
tell application "System Events"
	keystroke "o" using command down
	keystroke "G" using {command down, shift down}
	keystroke "v" using {command down}
	keystroke return
	keystroke return
end tell
end alfred_script

This is my first go with apple script and I don't think this is quite the right way to execute this. Namely, system events seems to be quite inefficient as I can only run a single image at a time. Ideally, I would like this part to run hidden and be able to compress all selected JPEGs once they have been resized. 

 

4. PNG Compress online

 

- Using carlosNZ's fantastic workflow for TinyPNG with custom API.

 

CarlosNZ's Tiny PNG Workflow

 

Ideally I would like to get this workflow to the point where, either a hotkey or keyword could be used on a selection of images that would be resized and run through either JPEGmini or TinyPNG depending on the file type. The resize and tiny png parts work, so I'm hoping to improve the JPEGmini flow and have a conditional if for jpg/png. Alternatively, just a better workflow for JPEGmini would be fine as I could run the resize/jpg/png workflows separately.

 

The use case would be to quickly run web images through the process that are 1920 wide. I'm aware that with poor coding skill, this is an uphill battle, so all help is really appreciated. If anyone is interested in assiting, I would reimburse you for your time as well.

Thanks alot :)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...