Easy methods to output outcomes of an AppleScript picture resizing script and show in automator

0
18
Easy methods to output outcomes of an AppleScript picture resizing script and show in automator


Give this a attempt:

use scripting additions

international target_width
international selImgs, chgList -- listing of chosen photos, listing of scaled photos
international ctr, imgCt -- processed picture counter, progress bar picture whole

set selImgs to {}
set target_width to 2000
set chgList to {}

show dialog "Enter most width" default reply "2000"
set target_width to the textual content returned of consequence as integer

-- Shut scaled listing (if open from earlier run)
inform software "TextEdit"
    if exists doc "scaled.txt" then
        shut doc "scaled.txt"
    finish if
finish inform

-- Chosen photos > listing of alias
inform software "Finder"
    set selImgs to choice as alias listing
finish inform

-- assemble progress bar
set imgCt to size of selImgs
set my progress whole steps to imgCt
set my progress accomplished steps to 0
set ctr to 0
set my progress description to "Scaling photos..."
set my progress extra description to "Making ready to course of."

-- the horror…
repeat with ei in selImgs
    rescale_and_save(ei)
finish repeat

-- Notification of modifications (filenames if < 4, in any other case rely)
contemplating software responses
    set AppleScript's textual content merchandise delimiters to area
    if size of chgList > 4 then
        set imChg to size of chgList as textual content
    else
        set imChg to chgList as textual content
    finish if
    show notification imChg with title "Pictures scaled"
finish contemplating

-- Publish listing of scaled photos
set AppleScript's textual content merchandise delimiters to return
set chText to chgList as textual content
set scFile to (((path to desktop) as textual content) & "scaled.txt") as «class furl»
shut entry (open for entry scFile)
write chText to scFile as textual content
inform software "TextEdit"
    open scFile
    set bounds of entrance window to {30, 30, 300, 240}
finish inform

-- the horror….
to rescale_and_save(this_file)
    inform software "Picture Occasions"
        launch
        -- earlier than every take a look at run, I duplicate the pictures, so I will not must re-fetch the originals
        if title of this_file accommodates "copy" then
            
            -- open the picture file, increment progress bar
            set this_image to open this_file
            set ctr to (my progress accomplished steps) + 1
            set my progress extra description to "Scaling picture " & ctr & " of " & imgCt
            set my progress accomplished steps to ctr
            
            -- course of picture (measure, scale, save, acquire title)
            copy dimensions of this_image to {current_width, current_height}
            
            if current_width is larger than target_width then -- huge sufficient to course of
                set txName to call of this_image as textual content
                
                if current_width is larger than current_height then -- when panorama
                    scale this_image to measurement target_width
                    save this_image with icon
                    copy txName to finish of chgList
                    
                else -- when portrait
                    -- determine new peak
                    -- y2 = (y1 * x2) / x1
                    
                    -- NB as 'scale to measurement' requires integer, spherical up
                    set new_height to spherical (current_height * target_width / current_width) rounding up
                    scale this_image to measurement new_height
                    save this_image with icon
                    copy txName to finish of chgList
                finish if
            finish if
            shut this_image
        finish if
    finish inform
    
finish rescale_and_save

Notes:

  • For looping via the recordsdata, it’s less complicated to make some_items an inventory of aliases

  • When scaling with measurement quite than issue, new_height ought to be integer

  • Save inside if…then block, in any other case each chosen file might be re-saved, even when not scaled

  • Use Picture Occasions to save lots of the recordsdata

  • In case your folder is a subfolder of Photos, you’ll be able to allow ‘Dimensions’ in listing view, which shows one thing like ‘2097 ×
    3014’, letting you see which photos might be scaled. It does not
    replace quickly so it will not sometimes show the modified dimension,
    as a substitute, it shows ‘–‘, which not less than has the advantage of
    figuring out which recordsdata have been modified.

  • Progress bar

  • Observe scaled photos and lift a notification when carried out, in addition to create a textual content file go browsing desktop

  • Non-obligatory: Have a dry run script — checks every picture’s dimensions and stories again (e.g. rely of photos to be scaled, filenames of
    photos to be scaled). You possibly can even have it change the choice to solely these recordsdata within the Finder when full. Then while you run the
    ultimate script, it is going to be clear which recordsdata will change.

FWIW, I attempted utilizing issue to scale however I discovered that after working for some time, it grew to become erratic, and would usually lock up the script (and overheat my mac, and require me to manually stop sips). I grew bored of that and used measurement (however with forcing new_height to be an integer). After that change, it grew to become extra dependable. Chances are you’ll discover the monitoring pointless.

LEAVE A REPLY

Please enter your comment!
Please enter your name here