Friday, October 23, 2020

#1 2020-01-25 05:07:27 pm

davec0864
Member
Registered: 2018-11-06
Posts: 37

Adjust an image size, resolution, exposure and save to the Desktop

I'm looking for a script that will take an image from the clipboard, adjust the image horizontal size to 3.33" keeping the same proportions, adjust the image resolution to 150dpi, adjust the image brightness to 15% brighter, and the save to image to the desktop using a fixed filename and image file type; example "clippedimage.png" as an example.  The image on the clipboard will be roughly 14"x10" at 72 dpi.

I don't need to select an image, don't need to save the file anywhere other than the desktop, and I don't need to change the filename or file type.  Thanks!

Model: Macbook Pro
AppleScript: 2.7
Browser: Firefox 72.0
Operating System: macOS 10.14

Offline

 

#2 2020-01-26 10:08:12 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 643

Re: Adjust an image size, resolution, exposure and save to the Desktop

If I understand the OP''s post correctly, he wants an AppleScript that will perform four tasks:

1) Paste an image file from the clipboard to the desktop. The only way I know to do this is GUI scripting.

2) Rename the image file once on the desktop--that's easy.

3) Change the width of the image file to 500 pixels (3.33" x 150 dpi). Both the sips command-line utility and image events will do this.

4) Change the exposure/brightness of the image file. I don't know of any way to do this with macOS utilities within an AppleScript, although ImageMagick can do this if installed.

So, no way to do this that I'm aware of.

Last edited by peavine (2020-01-26 11:55:08 am)


2018 Mac mini - macOS Catalina

Offline

 

#3 2020-01-26 12:29:04 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4570

Re: Adjust an image size, resolution, exposure and save to the Desktop

Hello peavine.

My understanding isn't that the OP want the image to be pasted to the Desktop but to be saved in a file named "clippedimage.png" on the desktop.
Doing that doesn't require GUI scripting.
At this time the unique remaining problem is the requirement upon brightness.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 26 janvier 2020  19:28:54

Offline

 

#4 2020-01-26 12:57:56 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 406

Re: Adjust an image size, resolution, exposure and save to the Desktop

If you didn't know Preview.app could do this for you.

I guess it all depends how many pictures you will convert.

1. Copy your picture...
2. Open Preview
3. File->New from Clipboard
4. Tools-> Adjust Colour
5. Tools-> Adjust Size

Peavine

-- To get content from the clipboard...
set this_clip to the clipboard

-- To set content to the clipboard
set the clipboard to "Hello World"

or

tell application "Finder"
    set this_file to POSIX path of (path to desktop as alias) & "puppy.jpg"
end tell

set that_file to ""
set the clipboard to this_file

copy (the clipboard) to that_file
return that_file

Last edited by Fredrik71 (2020-01-26 01:01:23 pm)


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#5 2020-01-26 02:18:15 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4570

Re: Adjust an image size, resolution, exposure and save to the Desktop

Here is a piece of code adapted from a Shane Stanley's script.

Applescript:

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"

# Adapted from a Shane Stanley's script

set pathToPng to (path to desktop as text) & "clippedimage.png"
my fileFromClipToPath:(pathToPng as «class furl»)


on fileFromClipToPath:thePath
   set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
   -- (pb's types as list) -->    {"public.tiff", "NeXT TIFF v4.0 pasteboard type", "dyn.ah62d4rv4gu8zazwuqm10c6xemf1gq54uqm10c6xenv61a3k", "PVPboardInfoPboardType"}
   set theData to pb's dataForType:(current application's NSPasteboardTypeTIFF) -- get tiff data off pasteboard
   if theData = missing value then error "No tiff data found on clipboard"
   # Convert it into png as asked by the OP
   set newRep to current application's NSBitmapImageRep's imageRepWithData:theData
   set theData to (newRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSImageInterlaced:false})
   set theResult to (theData's writeToURL:thePath atomically:true)
end fileFromClipToPath:

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 26 janvier 2020  21:18:10

Offline

 

#6 2020-01-26 04:34:51 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 406

Re: Adjust an image size, resolution, exposure and save to the Desktop

Thanks Yvan

My ShaneLib is getting bigger wink

Maybe in 1 year I will only do AppleScriptObjC of everything.

Thanks, again for you kindness to share.


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#7 2020-01-26 04:43:47 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 406

Re: Adjust an image size, resolution, exposure and save to the Desktop

peavine wrote:


To not make your code break: quoted form of (filePosixName)

Applescript:

do shell script "sips --resampleWidth 500 " & filePosixName & " --out " & filePosixName


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#8 2020-01-26 05:16:21 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6470

Re: Adjust an image size, resolution, exposure and save to the Desktop

Yvan Koenig wrote:

At this time the unique remaining problem is the requirement upon brightness.



That can be done using CoreImage. Here's your script, suitably adapted:

Applescript:

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "CoreImage"

# Adapted from a Shane Stanley's script

set pathToPng to (path to desktop as text) & "clippedimage.png"
my fileFromClipToPath:(pathToPng as «class furl»)


on fileFromClipToPath:thePath
   set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
   set theData to pb's dataForType:(current application's NSPasteboardTypeTIFF) -- get tiff data off pasteboard
   if theData = missing value then error "No tiff data found on clipboard"
   set theCIImage to current application's CIImage's imageWithData:theData
   set theCIFilter to (current application's CIFilter's filterWithName:"CIColorControls" withInputParameters:{inputBrightness:0.15, inputImage:theCIImage}) -- change brightness to suit
   set theCIImage to (theCIFilter's valueForKey:(current application's kCIOutputImageKey))
   set newRep to (current application's NSBitmapImageRep's alloc()'s initWithCIImage:theCIImage)
   set theData to (newRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSImageInterlaced:false})
   set theResult to (theData's writeToURL:thePath atomically:true)
end fileFromClipToPath:


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#9 2020-01-27 05:18:00 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4570

Re: Adjust an image size, resolution, exposure and save to the Desktop

Thank you Shane.

I tried to add code to change the size as required by the OP.

Here is what I got which is awful but does the job.

Applescript:

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "CoreImage"

# Adapted from a Shane Stanley's script

set pathToPng to (path to desktop as text) & "clippedimage.png"
my fileFromClipToPath:(pathToPng as «class furl»)


on fileFromClipToPath:thePath
   set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
   set theData to pb's dataForType:(current application's NSPasteboardTypeTIFF) -- get tiff data off pasteboard
   if theData = missing value then error "No tiff data found on clipboard"
   set theCIImage to current application's CIImage's imageWithData:theData
   
   -- grab the rectangle containing the image
   set {{x, y}, {oldWidth, oldHeight}} to theCIImage's extent()
   -- define the new size
   set theWidth to 500
   set theHeight to theWidth / oldWidth * oldHeight
   
   set theCIFilter to (current application's CIFilter's filterWithName:"CIColorControls" withInputParameters:{inputBrightness:0.15, inputImage:theCIImage}) -- change brightness to suit
   set theCIImage to (theCIFilter's valueForKey:(current application's kCIOutputImageKey))
   set newRep to (current application's NSBitmapImageRep's alloc()'s initWithCIImage:theCIImage)
   set theData to (newRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSImageInterlaced:false})
   set theResult to (theData's writeToURL:thePath atomically:true)
   -- Reload the image to change its size
   set theImage to current application's NSImage's alloc()'s initWithContentsOfURL:thePath -- load the file as an NSImage
   set newImage to current application's NSImage's alloc()'s initWithSize:(current application's NSMakeSize(theWidth, theHeight)) -- make new blank image
   -- draw from original to new
   newImage's lockFocus()
   theImage's drawInRect:{origin:{x:0, y:0}, |size|:{width:theWidth, height:theHeight}} fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeSourceOver) fraction:1.0
   newImage's unlockFocus()
   set theData to newImage's TIFFRepresentation() -- get bitmap as data
   set newRep to current application's NSBitmapImageRep's imageRepWithData:theData -- make bitmap from data
   set theData to (newRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSImageInterlaced:false})
   set theResult to (theData's writeToURL:thePath atomically:true)
   
end fileFromClipToPath:

I'm sure that changing the size may be done before the first save action but I failed to do it.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 27 janvier 2020  12:17:43

Offline

 

#10 2020-01-27 06:13:30 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6470

Re: Adjust an image size, resolution, exposure and save to the Desktop

I missed the need to scale. It's probably more efficient to do it first, and it can be done in CoreImage too:

Applescript:

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "CoreImage"

set pathToPng to (path to desktop as text) & "clippedimage.png"
my fileFromClipToPath:(pathToPng as «class furl»)


on fileFromClipToPath:thePath
   set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
   set theData to pb's dataForType:(current application's NSPasteboardTypeTIFF) -- get tiff data off pasteboard
   if theData = missing value then error "No tiff data found on clipboard"
   set theCIImage to current application's CIImage's imageWithData:theData
   
   -- grab the rectangle containing the image
   set {{x, y}, {oldWidth, oldHeight}} to theCIImage's extent()
   -- define the new size
   set theWidth to 500 -- points
   set theScale to theWidth / oldWidth
   
   set theCIFilter to (current application's CIFilter's filterWithName:"CILanczosScaleTransform" withInputParameters:{inputScale:theScale, inputImage:theCIImage})
   set theCIImage to (theCIFilter's valueForKey:(current application's kCIOutputImageKey))
   set theCIFilter to (current application's CIFilter's filterWithName:"CIColorControls" withInputParameters:{inputBrightness:0.15, inputImage:theCIImage}) -- change brightness to suit
   set theCIImage to (theCIFilter's valueForKey:(current application's kCIOutputImageKey))
   set newRep to (current application's NSBitmapImageRep's alloc()'s initWithCIImage:theCIImage)
   set theData to (newRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSImageInterlaced:false})
   set theResult to (theData's writeToURL:thePath atomically:true)
end fileFromClipToPath:


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#11 2020-01-27 06:55:55 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4570

Re: Adjust an image size, resolution, exposure and save to the Desktop

Thank you Shane.
Your code is really neater than mine.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 27 janvier 2020  13:55:49

Offline

 

#12 2020-01-27 08:02:50 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 643

Re: Adjust an image size, resolution, exposure and save to the Desktop

Fredrik71 wrote:

Peavine

-- To get content from the clipboard...
set this_clip to the clipboard

-- To set content to the clipboard
set the clipboard to "Hello World"

or

tell application "Finder"
    set this_file to POSIX path of (path to desktop as alias) & "puppy.jpg"
end tell

set that_file to ""
set the clipboard to this_file

copy (the clipboard) to that_file
return that_file



Fredrik71. The goal was to save an image, which is on the clipboard, to the desktop. Your suggestion only appears to copy the POSIX path to an image file to the clipboard.

Last edited by peavine (2020-01-27 12:20:35 pm)


2018 Mac mini - macOS Catalina

Offline

 

#13 2020-01-27 08:07:37 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 643

Re: Adjust an image size, resolution, exposure and save to the Desktop

Yvan Koenig wrote:

Hello peavine.

My understanding isn't that the OP want the image to be pasted to the Desktop but to be saved in a file named "clippedimage.png" on the desktop.



Yvan. My suggestion was to paste (i.e. save) the image file to the desktop and then immediately to rename it to "clippedimage.png". Shane's solution does that in one step, which is certainly better, but both accomplish the same thing.

Last edited by peavine (2020-01-27 10:22:18 am)


2018 Mac mini - macOS Catalina

Offline

 

#14 2020-01-27 09:43:53 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 643

Re: Adjust an image size, resolution, exposure and save to the Desktop

Shane. I opened your script in post 10 in Script Editor and then attempted to use it with a PNG file created with the screencapture utility and with a JPG image from a digital camera. In both cases, I verified that the image files were in the clipboard, and files named clippedimage.png were created on the desktop. I then opened the new desktop image file in Preview and there was no image (just a large icon with "JPEG" and "PNG"). Your script seems to work for other forum members and so I assume this is an issue only for me.

BTW, after copying the PNG image to the clipboard, I ran "clipboard info", which returned:

{{«class furl», 54}, {«class icns», 935693}, {«class ut16», 40}, {«class utf8», 19}, {«class 8BPS», 1774090}, {GIF picture, 124244}, {«class jp2 », 213437}, {JPEG picture, 98488}, {TIFF picture, 4198020}, {«class PNGf», 533225}, {«class BMP », 4194358}, {«class TPIC», 1133665}, {Unicode text, 38}, {string, 19}}



I also ran "the clipboard" and it returned the name of the image file.

Last edited by peavine (2020-01-27 10:19:26 am)


2018 Mac mini - macOS Catalina

Offline

 

#15 2020-01-27 10:34:11 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4570

Re: Adjust an image size, resolution, exposure and save to the Desktop

@peavine.

I tested after copying in the clipboard the contents of : a jpeg, a tiff and a png.
In every cases, the file created on the desktop behaved flawlessly.

My understanding is that you didn't copied the contents of your files but the files themselves.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 27 janvier 2020  17:31:16

Offline

 

#16 2020-01-27 10:46:14 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 643

Re: Adjust an image size, resolution, exposure and save to the Desktop

Yvan Koenig wrote:

@peavine.

I tested after copying in the clipboard the contents of : a jpeg, a tiff and a png.
In every cases, the file created on the desktop behaved flawlessly.

My understanding is that you didn't copied the contents of your files but the files themselves.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 27 janvier 2020  17:31:16



Thanks Yvan. You are absolutely correct. I copied an image to the clipboard and Shane's script worked as expected. This also explains your statement I mention in Post 13.


2018 Mac mini - macOS Catalina

Offline

 

#17 2020-01-27 11:38:43 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4570

Re: Adjust an image size, resolution, exposure and save to the Desktop

Here is a script which treat both cases:
-- the clipboard contain a file image descriptor (what peavine did)
-- the clipboard contain the image contents (what Fredrik71, Shane and me did)

Applescript:

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "CoreImage"

set pathToPng to ((path to desktop as text) & "clippedimage.png") as «class furl»

try
   clipboard info for {«class furl»}
   set sourceFile to the clipboard as «class furl»
   my fileFromFile:sourceFile toPath:pathToPng
on error
   my fileFromClipToPath:pathToPng
end try

on fileFromClipToPath:thePath
   set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
   set theData to pb's dataForType:(current application's NSPasteboardTypeTIFF) -- get tiff data off pasteboard
   if theData = missing value then error "No tiff data found on clipboard"
   set theCIImage to current application's CIImage's imageWithData:theData
   my shared(theCIImage, thePath)
end fileFromClipToPath:

on shared(theCIImage, thePath)
   -- grab the rectangle containing the image
   set {{x, y}, {oldWidth, oldHeight}} to theCIImage's extent()
   -- define the new size
   set theWidth to 500 -- points
   set theScale to theWidth / oldWidth
   set theCIFilter to (current application's CIFilter's filterWithName:"CILanczosScaleTransform" withInputParameters:{inputScale:theScale, inputImage:theCIImage})
   set theCIImage to (theCIFilter's valueForKey:(current application's kCIOutputImageKey))
   set theCIFilter to (current application's CIFilter's filterWithName:"CIColorControls" withInputParameters:{inputBrightness:0.15, inputImage:theCIImage}) -- change brightness to suit
   set theCIImage to (theCIFilter's valueForKey:(current application's kCIOutputImageKey))
   set newRep to (current application's NSBitmapImageRep's alloc()'s initWithCIImage:theCIImage)
   set theData to (newRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSImageInterlaced:false})
   set theResult to (theData's writeToURL:thePath atomically:true)
end shared

on fileFromFile:imagePath toPath:thePath
   set theImage to current application's NSImage's alloc()'s initWithContentsOfURL:imagePath -- load the file as an NSImage
   set theData to theImage's TIFFRepresentation() -- get bitmap as data
   set theCIImage to current application's CIImage's imageWithData:theData
   my shared(theCIImage, thePath)
end fileFromFile:toPath:

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 27 janvier 2020  18:38:19

Offline

 

#18 2020-01-27 11:53:56 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 643

Re: Adjust an image size, resolution, exposure and save to the Desktop

Yvan Koenig wrote:

Here is a script which treat both cases:
-- the clipboard contain a file image descriptor (what peavine did)
-- the clipboard contain the image contents (what Fredrik71, Shane and me did)



Thanks Yvan. I tested your script under both situations and it worked great.


2018 Mac mini - macOS Catalina

Offline

 

#19 2020-01-27 01:23:45 pm

davec0864
Member
Registered: 2018-11-06
Posts: 37

Re: Adjust an image size, resolution, exposure and save to the Desktop

What Fredrik71 described in post #4 is exactly what I'm doing manually and that is the process I would like to automate.  Using Preview to create the file from the clipboard and then manually adjusting the image.

I tried running the last script provided and it worked great.  Thanks for all your help!

Offline

 

#20 2020-01-27 01:52:02 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 643

Re: Adjust an image size, resolution, exposure and save to the Desktop

The OP has a great solution from Shane and Yvan.

A question that remains unanswered for me is whether basic AppleScript can be used to save an image on the clipboard as a file. Preview does this but, as the OP notes, it's convenient to automate this process.

After some Google research I found a procedure which I've modified as follows:

Applescript:

try
   set theImage to the clipboard as GIF picture
on error
   display dialog "Image not found on clipboard." buttons "OK" cancel button 1 default button 1
end try

set theFile to ((path to desktop) as text) & "Saved Image.gif"

try
   set openedFile to open for access theFile with write permission
   write theImage to openedFile
   close access openedFile
on error
   try
       close access openedFile
   end try
end try

Is there a better way to do this? Thanks.

Source:
https://discussions.apple.com/thread/2379870

Last edited by peavine (2020-01-27 05:39:21 pm)


2018 Mac mini - macOS Catalina

Offline

 

#21 2020-01-27 05:36:55 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6470

Re: Adjust an image size, resolution, exposure and save to the Desktop

Yvan Koenig wrote:

Here is a script which treat both cases:



This is the start of a slightly more direct approach:

Applescript:

set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
set theTypes to pb's |types|()
if theTypes's containsObject:(current application's NSPasteboardTypeFileURL) then
   set theURL to (pb's readObjectsForClasses:{current application's NSURL} options:{NSPasteboardURLReadingFileURLsOnlyKey:true})'s firstObject()
   set theCIImage to current application's CIImage's imageWithContentsOfURL:theURL
else if theTypes's containsObject:(current application's NSPasteboardTypeTIFF) then
   set theData to pb's dataForType:(current application's NSPasteboardTypeTIFF) -- get tiff data off pasteboard
   set theCIImage to current application's CIImage's imageWithData:theData
else
   error "Neither file nor image found on clipboard"
end if


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#22 2020-01-27 08:22:49 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 406

Re: Adjust an image size, resolution, exposure and save to the Desktop

peavine wrote:


Is there a better way to do this? Thanks.




If this is better you decide...

The reason I made this script was because the application was limited to export PNG file
but it was possible to copy. I use the clipboard info to know the class of the image.

1. I copy the image from the application.
2. Select the folder I like to have the copy.
3. Run the Script.

Applescript:

(*
   Write clipboard image as PNG to selected directory
*)


on run
   set thisClip to the clipboard as «class PNGf»
   tell application "Finder"
       set thisFolder to POSIX path of (the first item of (get selection as alias list) as alias)
       tell application (path to frontmost application as Unicode text)
           set thisPath to choose file name with prompt "Save this clipboard image as PNG:" default name "poster.png" default location thisFolder
           set thisDest to open for access thisPath with write permission
           write thisClip to thisDest
           close access thisDest
       end tell
   end tell
end run


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#23 2020-01-28 05:21:28 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4570

Re: Adjust an image size, resolution, exposure and save to the Desktop

Shane Stanley wrote:
Yvan Koenig wrote:

Here is a script which treat both cases:



This is the start of a slightly more direct approach:


Thank you Shane.

Here is the complete script using your proposal.

Applescript:

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "CoreImage"

set thePath to ((path to desktop as text) & "clippedimage.png") as «class furl»

set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
set theTypes to pb's |types|() -- the pipes are required with Script Debugger but will be removed by Script Editor!
if theTypes's containsObject:(current application's NSPasteboardTypeFileURL) then
   set theURL to (pb's readObjectsForClasses:{current application's NSURL} options:{NSPasteboardURLReadingFileURLsOnlyKey:true})'s firstObject()
   set theCIImage to current application's CIImage's imageWithContentsOfURL:theURL
else if theTypes's containsObject:(current application's NSPasteboardTypeTIFF) then
   set theData to pb's dataForType:(current application's NSPasteboardTypeTIFF) -- get tiff data off pasteboard
   set theCIImage to current application's CIImage's imageWithData:theData
else
   error "Neither file nor image found on clipboard"
end if

set {{x, y}, {oldWidth, oldHeight}} to theCIImage's extent()
-- define the new size
set theWidth to 500 -- points
set theScale to theWidth / oldWidth
set theCIFilter to (current application's CIFilter's filterWithName:"CILanczosScaleTransform" withInputParameters:{inputScale:theScale, inputImage:theCIImage})
set theCIImage to (theCIFilter's valueForKey:(current application's kCIOutputImageKey))
set theCIFilter to (current application's CIFilter's filterWithName:"CIColorControls" withInputParameters:{inputBrightness:0.15, inputImage:theCIImage}) -- change brightness to suit
set theCIImage to (theCIFilter's valueForKey:(current application's kCIOutputImageKey))
set newRep to (current application's NSBitmapImageRep's alloc()'s initWithCIImage:theCIImage)
set theData to (newRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSImageInterlaced:false})
set theResult to (theData's writeToURL:thePath atomically:true)

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 28 janvier 2020  12:21:17

Last edited by Yvan Koenig (2020-01-29 03:46:56 am)

Offline

 

#24 2020-01-28 04:45:59 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6470

Re: Adjust an image size, resolution, exposure and save to the Desktop

KniazidisR wrote:

the above code line should be as proposed by Shane - using pipes



The term type (and hence types) is defined in Script Debugger's scripting dictionary. So you need pipes in Script Debugger, but not in Script Editor.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#25 2020-01-28 10:42:41 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6470

Re: Adjust an image size, resolution, exposure and save to the Desktop

Actually, the term is defined by AppleScript itself, as a synonym for type class. Script Debugger uses it in its dictionary, whereas Script Editor doesn't. Quite why AppleScript doesn't recognise it in Script Editor is one of those AppleScript mysteries.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)