Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Optimize images for Desktop - improved script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Optimize images for Desktop - improved script
Authored by: tedw on Oct 28, '10 12:26:16AM
Just on a lark, I twiddled with this script. changes are as follows:
  • script now scales the image so that it fills the screen on the "best" dimension (whichever is largest proportionally)
  • added a choose file command for when there is no selection
  • added the fill color as a global variable
  • tightened up and streamlined the code
--WARNING: This script overwrites original files, work on copies.

-- Change these variables to your screen dimensions…
set _screenWidth to 1280
set _screenHeight to 800
set _fillColor to {0, 0, 0} -- black

global _screenWidth, _screenHeight, _fillColor

tell application "Finder" to set _fileNames to selection as alias list
if _fileNames = {} then
	set _fileNames to choose file with prompt "Choose desktop images to pad" of type "public.image" with multiple selections allowed
end if

if _fileNames = {} then error "No filenames selected" number 1

display alert "Pad Images for the Desktop" message "Warning: This script will overwrite selected files. Please work on duplicates." buttons {"Cancel", "Continue"} default button "Cancel"
if button returned of the result is "Cancel" then
	return
end if

repeat with _fileName in _fileNames
	try
		tell application "Image Events"
			set _image to open _fileName
			set {_width, _height} to dimensions of _image
		end tell
		process_image(_width, _height, _image)
	on error a number b
		try
			tell application "Image Events" to close _image
		end try
		log a
	end try
end repeat

to process_image(_width, _height, _image)
	-- image aspect ratio, rounded to 2 decimal places
	set _ratio to (round (100 * _width / _height)) / 100
	
	-- image height/width to screen height/width ratios, for determining proper scaling
	set _heightRatio to (round (100 * _height / _screenHeight)) / 100
	set _widthRatio to (round (100 * _width / _screenWidth)) / 100
	
	if _heightRatio ≥ _widthRatio then
		--scale to height
		set _scalingFactor to 1 / _heightRatio
	else
		--scale to width
		set _scalingFactor to 1 / _widthRatio
	end if
	
	tell application "Image Events"
		scale _image by factor _scalingFactor
		pad _image to dimensions {_screenWidth, _screenHeight} with pad color _fillColor
		save _image
		close _image
	end tell
end process_image


[ Reply to This | # ]