I'm using Automator to resize a bunch of images. It works great apart from the way it adds a black background to PNG images. This is a real issue if the image was a black logo with a transparent background. Is there any way to change the background colour?
I'm using the Crop Images action.
Cheers!
Automator is very easy to use but for more finite control, you can use AppleScript (even as part of Automator) with Image Events to do some basic image editing including resizing using the pad command with specific dimensions and background color. Here's a basic script that you can save as an application in Script Editor and then, to use it, either double-click the app icon in the Finder or, better, drop the image files to process on the application icon:
property _width : 400
property _height : 200
property _color : {65528, 65535, 65525} --white
on run
open (choose file with multiple selections allowed)
end run
on open the_items
set output_folder to (((path to desktop) as string) & "Output")
try
get output_folder as alias
on error
tell application "Finder" to make new folder at desktop with properties {name:"Output"}
end try
repeat with this_item in the_items
set this_item to (this_item as alias)
set _info to info for this_item
set _extension to _info's name extension
if (_extension is in {"jpg", "jpeg", "tif", "tiff", "png"}) then
set _name to name of _info
set _name to (text 1 thru -((count _extension) + 1) of _name)
set output_path to (output_folder & ":" & _name & "jpg")
my resize_image(this_item, output_path, _width, _height)
end if
end repeat
end open
on resize_image(original_path, output_path, _width, _height)
tell application "Image Events"
launch
set _image to (open file (original_path as string))
pad _image to dimensions {_width, _height} with pad color _color -- use crop to dimensions to decrease the size
save _image as JPEG in file output_path with icon
close _image
end tell
end resize_image
This will create a folder on your Desktop named Output (if one does not already exist) and save the images as JPGs there using the dimensions at the top of the script (you can modify the dimensions, color, output location, etc., this is just an example).
Related
Is there any way to display Display Dialog icons like macOS does, I tried looking for the icon below in /System/Library/CoreServices/CoreTypes.bundle
but I didn't find it.
set myIcon to (path to resource "myIcon.icns")
display dialog "this is my icon" buttons {"OK"} default button "OK" with icon myIcon
All of the standard Apple icons are stored here:
`/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources`
You can access them directly, as in the following.
set iconFIle to choose file "Choose an icon file" of type {"com.apple.icns"} ¬
default location POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources"
display dialog "This is a test" with icon iconFIle
Note that the Finder sometimes does composite icons by using one icon as an overlay or badge applied to another icon. For instance, I believe the image you posted above is the file "KEXT.icns" overlaid with the file "AlertCautionBadgeIcon.icns." You can mimic that behavior with the following script.
set basePath to POSIX path of (path to library folder from system domain) & "CoreServices/CoreTypes.bundle/Contents/Resources/"
set tempStoragePath to POSIX path of (path to temporary items from user domain)
-- set the name of the main idon, and the overlay icon
set mainName to "KEXT"
set overlayName to "AlertCautionBadgeIcon"
set kextImgPath to basePath & mainName & ".icns"
set alertImgPath to basePath & overlayName & ".icns"
set tempSetPath to quoted form of (tempStoragePath & mainName & ".iconset" as text)
set tempOverlayPath to quoted form of (tempStoragePath & overlayName & ".iconset" as text)
-- decompose the icns into iconsets, so that we can do the overlays by hand
do shell script "iconutil -c iconset -o " & tempSetPath & " " & kextImgPath
do shell script "iconutil -c iconset -o " & tempOverlayPath & " " & alertImgPath
tell application "System Events"
set mainFolder to folder (mainName & ".iconset") of folder tempStoragePath
set overlayFolder to folder (overlayName & ".iconset") of folder tempStoragePath
set theFiles to (files of mainFolder whose name extension is "png")
(*
iconsets are folders with files named (e.g.) 'icon_16x16.png', 'icon_128x128#2x.png'
this loop runs through and finds matching sized elements of the set, then sends
them to the script object's handler for processing with ASOC
*)
repeat with thisFile in theFiles
set fileName to name of thisFile
if exists file (name of thisFile) of overlayFolder then
asocBits's mungePNGs(POSIX path of thisFile, POSIX path of (file fileName of overlayFolder))
end if
end repeat
end tell
-- convert the iconset folders back to icns files, then send the file to display dialog
do shell script "iconutil -c icns " & tempSetPath
display dialog "This is a test" with icon POSIX file (tempStoragePath & mainName & ".icns")
script asocBits
use framework "Foundation"
use framework "AppKit"
property NSImage : class "NSImage"
property NSBitmapImageRep : class "NSBitmapImageRep"
on mungePNGs(mainImg, overlay)
-- get respective images
set mainImgObj to NSImage's alloc's initWithContentsOfFile:mainImg
set overlayObj to NSImage's alloc's initWithContentsOfFile:overlay
-- create a destination image
set newImage to NSImage's alloc's initWithSize:(mainImgObj's |size|)
newImage's lockFocus()
-- set up apprpriate drawing rects, then draw the two images into the new image.
set newRect to current application's CGRectZero as list
set imgSize to mainImgObj's |size| as list
set item 2 of newRect to item 1 of imgSize
set h to (height of item 1 of imgSize)
(*
these were trial and error. remember that (0,0) is the bottom left,
not the top left (y goes from bottom to top).
placementRect is supposed to be where the overlay is placed.
cropRect is the part of the overlay that's pasted.
different badges have their effective images in different quadrants.
*)
set placementRect to current application's CGRectMake(0.5 * h, 0.0, h, h)
set cropRect to current application's CGRectMake(0.5 * h, 0.5 * h, h, h)
mainImgObj's drawInRect:newRect
set op to current application's NSCompositeSourceOver
overlayObj's drawInRect:placementRect fromRect:cropRect operation:op fraction:1.0
-- create a bitmap representation and save it back to the main iconset file
set bitmapRep to NSBitmapImageRep's alloc's initWithFocusedViewRect:newRect
newImage's unlockFocus()
set PNGType to current application's NSBitmapImageFileTypePNG
set imgData to (bitmapRep's representationUsingType:PNGType |properties|:(missing value))
imgData's writeToFile:mainImg atomically:false
end mungePNGs
end script
The script is on the slow side, but it gets the job done. In the long run, it might be more efficient simply to use the script object to create icns files that you can store in the bundle and call at need.
Technical detail: I've used a script object to isolate the ASOC routines from the rest of the script. ASOC and osax commands don't always play well together. In this case, display dialog with icon always seems to throw an error if any frameworks have been invoked. I suppose I could have used tell framework "..." blocks instead, but the script object appealed to me...
As Ted correctly answered you can't display compound icons.
But you can do the opposite, displaying your application icon with a caution or stop badge.
To do so omit the reference to the icon and use the other – enumerated – with icon parameter
display dialog "this is my icon" buttons {"OK"} default button "OK" with icon caution
display dialog "this is my icon" buttons {"OK"} default button "OK" with icon stop
I always design my presentation slides in keynote (because I find it easier and more pleasant to work with), though they often need to be presented on a windows machine running PowerPoint.
In order to avoid issues with fonts, formatting, etc., I always use the following effective workflow:
Design the slides in keynote, often using images and text.
Export the slides as jpg files to a folder on the desktop.
Open a new keynote presentation.
Drag the jpg files into the slide navigator. This creates an image slide of each jpg.
export the new presentation to a .ppt file.
Is there a way I can automate this workflow? I'd love to collapse steps 2-5 into a single step!
Here's the AppleScript that does this (work on Keynote version 6.2, not on version 5):
tell application "Finder" to set f to (make new folder) as text -- create a temp folder to export images
tell application "Keynote"
tell front document
export to (file f) as slide images with properties {image format:JPEG, compression factor:95}
set {h, w, fPath} to {height, width, file of it}
end tell
tell (fPath as string) to if it ends with ".key:" then
set newFile to (text 1 thru -6) & ".ppt"
else
set newFile to it & ".ppt"
end if
set jpegs to my getImages(f)
set newDoc to make new document with properties {width:w, height:h}
tell newDoc
set mSlide to last master slide -- blank
repeat with thisJPEG in jpegs
set s to make new slide with properties {base slide:mSlide}
tell s to make new image with properties {file:thisJPEG}
end repeat
delete slide 1
export to (file newFile) as Microsoft PowerPoint
close saving no
end tell
end tell
tell application "Finder" to delete folder f -- delete the temp folder
on getImages(f)
tell application "Finder" to return (files of folder f) as alias list
end getImages
Important:
the slideshow must be already open in Keynote before running the script.
And the slideshow must be already saved, because the script use the path of the front document to save the PPT file in the same folder.
--
Updated: to choose location of the new file
set v to ("Volumes" as POSIX file) as alias
tell application "Finder" to set f to (make new folder) as text -- create a temp folder to export images
tell application "Keynote"
tell front document
export to (file f) as slide images with properties {image format:JPEG, compression factor:95}
set {h, w, tName} to {height, width, name of it}
end tell
tell tName to if it ends with ".key" then
set newName to (text 1 thru -5) & ".ppt"
else
set newName to it & ".ppt"
end if
set jpegs to my getImages(f)
activate
set newFile to choose file name default name newName default location v with prompt "Select the folder to save the PPT file"
set newDoc to make new document with properties {width:w, height:h}
tell newDoc
set mSlide to last master slide -- blank
repeat with thisJPEG in jpegs
set s to make new slide with properties {base slide:mSlide}
tell s to make new image with properties {file:thisJPEG}
end repeat
delete slide 1
export to (newFile) as Microsoft PowerPoint
close saving no
end tell
end tell
tell application "Finder" to delete folder f -- delete the temp folder
on getImages(f)
tell application "Finder" to return (files of folder f) as alias list
end getImages
The following code has two lists: myURLs and myIMGs. I want to loop through the sites listed in myURLs, take a screenshot, then save them in my Shared folder as the file name listed in myIMGs. So, google.com's image will be saved as "google.jpg", and so forth. The list in myIMGs corresponds to the list in myURLs.
This is what I have, but it doesn't quite work. It loads the first URL from myURLs, takes a screenshot of it, but then loops through each file name in myIMGs and saves that one screenshot with all those various file names. Then, it loads the next URL in myURLs and does the same thing. How do I set the file names in the myIMGs list to correspond to the URLs being loaded from myURLs? I'm having trouble with the nested loops.
set myURLs to {"https://google.com", "https://wikipedia.org", "https://bing.com", "https://apple.com"}
set myIMGs to {"google.jpg", "wikipedia.jpg", "bing.jpg", "apple.jpg"}
-- Sets settings of Safari without interfering with user's settings.
repeat with myURL in myURLs
tell application "Safari"
set myDoc to front document
set windowID to id of window 1
do JavaScript ("window.open('" & myURL & "','_blank','titlebar=0');") in document 1
close window id windowID
set the bounds of the front window to {0, 20, 915, 812}
delay 5
end tell
-- Take screenshot, crop and save to Shared folder
repeat with myIMG in myIMGs
do shell script "screencapture -o -l$(osascript -e 'tell app \"Safari\" to id of window 1') /Users/Shared/" & myIMG
set this_file to "Macintosh HD:Users:Shared:" & myIMG
try
tell application "Image Events"
-- start the Image Events application
launch
-- open the image file
set this_image to open this_file
-- get dimensions of the image
copy dimensions of this_image to {W, H}
-- Crops off the Safari header
crop this_image to dimensions {W, H - 50}
-- save the changes
save this_image with icon
-- purge the open image data
close this_image
end tell
end try
end repeat
end repeat
You don't want to use two nested Repeat loops... you want to repeat and increment the variable n and then use item n of each list.
Here's the changed code... tested and works as expected:
set myURLs to {"https://google.com", "https://wikipedia.org", "https://bing.com", "https://apple.com"}
set myIMGs to {"google.jpg", "wikipedia.jpg", "bing.jpg", "apple.jpg"}
-- Sets settings of Safari without interfering with user's settings.
repeat with n from 1 to (count myURLs)
set myURL to item n in myURLs
tell application "Safari"
set myDoc to front document
set windowID to id of window 1
do JavaScript ("window.open('" & myURL & "','_blank','titlebar=0');") in document 1
close window id windowID
set the bounds of the front window to {0, 20, 915, 812}
delay 5
end tell
-- Take screenshot, crop and save to Shared folder
set myIMG to item n in myIMGs
do shell script "screencapture -o -l$(osascript -e 'tell app \"Safari\" to id of window 1') /Users/Shared/" & myIMG
set this_file to "Macintosh HD:Users:Shared:" & myIMG
try
tell application "Image Events"
-- start the Image Events application
launch
-- open the image file
set this_image to open this_file
-- get dimensions of the image
copy dimensions of this_image to {W, H}
-- Crops off the Safari header
crop this_image to dimensions {W, H - 50}
-- save the changes
save this_image with icon
-- purge the open image data
close this_image
end tell
end try
end repeat
You could simplify more by doing things like programmatically setting the file name based off of the URL string, and then you'd only have to maintain one list.
I'm brand new to AppleScript and I'm trying to write a basic script that does the following:
Finds images (PNGs) in the folder ~/Dropbox/Camera Uploads that are exactly 640x1136 (iPhone 5 screenshots) and moves them to ~/Dropbox/Camera Uploads/Screenshots.
This seems pretty straightforward, but so far I haven't been able to figure it out.
Here's how I would do it. I wouldn't worry about performance. I ran the Image Events section on 200 files, and it only took 1 second.
set picFolder to alias "Path:to:Dropbox:Camera Uploads:"
set screenshotFolder to alias "Path:to:Dropbox:Camera Uploads:screenshots:"
tell application "System Events"
set photos to path of files of picFolder whose kind is "Portable Network Graphics image"
end tell
set screenshots to {}
repeat with imgPath in photos
set imgAlias to alias imgPath
tell application "Image Events"
set img to open imgPath
if dimensions of img = {640, 1136} then
set end of screenshots to imgAlias
end if
close img
end tell
end repeat
tell application "Finder"
move screenshots to screenshotFolder
end tell
You need to have an AppleScript-aware application that can act based on the dimensions of an image file. I don’t think the Finder can do this, despite its ability to show the dimensions of images in Finder views.
iPhoto should be able to do this. The iPhoto dictionary indicates that “photos” have both the width and height of images. So you should be able to write an AppleScript that imports them into iPhoto first, then selects those that match your criteria, and then saves them to the appropriate Dropbox folder.
Depending on your needs, you might also look at Automator. It contains iPhoto actions as well, including one to “Filter iPhoto items”. If you create a Folder Action you should be able to create an Automator script that starts up whenever something new is added to your Camera Uploads folder, adds them to iPhoto, and then copies them to your Screenshots folder.
If nothing else, you should be able to use Image Events to get all images in the folder, and then act only on the ones that match your criteria. Something like:
tell application "Image Events"
tell folder "Macintosh HD:Users:colin:Dropbox:Camera Uploads"
copy (files where kind is "JPEG image") to potentialScreenshots
repeat with potentialFile in potentialScreenshots
set potentialScreenshot to open potentialFile
set imageDimensions to dimensions of potentialScreenshot
if item 1 of imageDimensions is 640 then
set fileName to name of potentialFile
tell me to display dialog fileName
end if
end repeat
end tell
end tell
There ought to be a way to tell Image Events to only look at files whose dimensions match what you want, but I can’t see it.
Try:
set folderPath to POSIX path of (path to home folder) & "Dropbox/Camera Uploads"
set screenshotsPath to POSIX path of (path to home folder) & "Dropbox/Camera Uploads/Screenshots"
try
do shell script "mdfind -0 -onlyin " & quoted form of folderPath & " \"kMDItemPixelWidth == 640 && kMDItemPixelHeight == 1136\" | xargs -0 -I {} mv {} " & quoted form of screenshotsPath
end try
I have a simple Applescript to resize photos with Image Events. The photos are all of football players so they are all named by their number as in "1.jpg", "4.jpg" and so on. The issue I run into is when I do multiple batches of players in different directories the script will overwrite a photo with one from another team that has the same filename and was previously done. Again, these photos were all placed in different directories. The end result is after running successfully two or three times the reformatted photos of the players will get confused.
Here's what I have in the script to call Image Events.
on open some_items
repeat with this_item in some_items
try
rescale_and_save(this_item)
end try
end repeat
end open
to rescale_and_save(this_item)
tell application "Image Events"
launch
set the target_width to 290
-- open the image file
set this_image to open this_item
set typ to this_image's file type
copy dimensions of this_image to {current_width, current_height}
if current_width is greater than current_height then
scale this_image to size target_width
else
-- figure out new height
-- y2 = (y1 * x2) / x1
set the new_height to (current_height * target_width) / current_width
scale this_image to size new_height
end if
tell application "Finder" to set new_item to ¬
(container of this_item as string) & "" & (name of this_item)
save this_image in new_item as typ
end tell
end rescale_and_save
You seem to have triggered a bug in Image Events processing multiple items with the same name. I'm not seeing the exact behavior you describe, but I am seeing similar behavior.
I'd suggest you simply tell Image Events to quit after processing each folder; that way it won't get confused. (You don't need the launch, either; the only reason to use launch is if you want a non-background application to open without presenting an untitled document window.)
Incidentally, if you want to overwrite the existing image with the scaled version, all you need is save this_image. Image Events behaves much like any other application if you were to open a document, modify it, and save it.