Im a novice:
I have a .jpg file that is a solid color (rgb). I want to provide the applescript with a new rgb color and the file/path name and have applescript change the color. I looked at the app image events. But I could not see how it would work.
Any help would be appreciated.
Todd Dignan
Model: 10.8 and 10.9
AppleScript: 2.5.1
Browser: Safari 537.36
Operating System: Mac OS X (10.8)
HI. I ‘m not aware of color assignment being within the scope of Image Events’ ability; you probably need a real image editor, such as Photoshop.
Hi,
Here’s a AppleScriptObjC solution
on changeSolidColorOfImage(imagePath, {redValue, greenValue, blueValue, alphaValue})
set filePath to POSIX path of imagePath
set image to current application's NSImage's alloc()'s initWithContentsOfFile:filePath
set imageSize to image's |size|()
image's lockFocus()
set newColor to current application's NSColor's colorWithRed:redValue green:greenValue blue:blueValue alpha:alphaValue
newColor's setFill()
current application's NSBezierPath's fillRect:(current application's NSMakeRect(0, 0, imageSize's width, imageSize's height))
image's unlockFocus()
set imageTIFFData to image's TIFFRepresentation()
set imageRep to current application's NSBitmapImageRep's imageRepWithData:imageTIFFData
set imageProperties to {NSImageCompressionFactor:1.0}
set imageData to imageRep's representationUsingType:(current application's NSJPEGFileType) |properties|:imageProperties
imageData's writeToFile:filePath atomically:false
end changeSolidColorOfImage
Copy the code in AppleScript Editor and save it as script bundle with name ChangeColor Library.scptd in ~/Library/Script Libraries/. If the folder Script Libraries does not exist, create it.
Open the Bundle Contents drawer in AppleScript Editor and check the checkbox AppleScript/Objective-C library.
Press again ⌘S
Call the handler with
set theLibrary to script "ChangeColor Library"
theLibrary's changeSolidColorOfImage((path to desktop as text) & "image.jpg", {1.0, 0.0, 1.0, 1.0})
the first handler parameter is the path to the image (HFS path or alias specifier)
the second handler parameter is a list of 4 values {redValue, greenValue, blueValue, alphaValue} all a real number between 0.0 and 1.0.
There is no error handling at all, so make sure that the parameters match the requirements
you cannot run the library directly in AppleScript Editor