I have a script to shrink pictures that stopped working under 10.15.

@peavine

As I already wrote, I am stubborn.

I want to get the log history returned by “three sizes.scpt”.
In fact, as I enhanced it, now it’s “three sizes 1.scpt”

-- three sizes 1.scpt
-- Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 24 mai 2020 22:33:47

property mode : 1 -- try with the values 1, 2, 3
-- 1 --> alias
-- 2 --> «class furl»
-- 3 --> POSIX Path
--on open draggeditems

set draggedItems to choose file of type {"public.jpeg"} with multiple selections allowed
tell application "Image Events" to launch

repeat with currentFile in draggedItems
	if mode = 1 then
		set thePicture to currentFile as text
	else if mode = 2 then
		set thePicture to currentFile as «class furl» --> «class furl»
	else if mode = 3 then
		set thePicture to POSIX path of currentFile --> text
	end if
	
	set origSize to size of (info for currentFile)
	tell application "Image Events"
		if mode = 1 then
			set openedFile to open file thePicture
		else
			set openedFile to open thePicture
		end if
		--set fileLocation to the location of openedFile
		--set fileName to the name of openedFile
		set wantedSize to 800
		set newPath to my stamp(currentFile, wantedSize)
		set origDimensions to dimensions of openedFile
		set origResolution to resolution of openedFile
		scale openedFile to size wantedSize
		save openedFile in newPath with icon
		close openedFile
		set openedFile to open file newPath
		set newDimensions to dimensions of openedFile
		set newResolution to resolution of openedFile
		close openedFile
	end tell
	set newSize800 to size of (info for file newPath)
	tell me to display dialog "origSize : " & origSize & " bytes" & linefeed & "origDimensions : " & my recolle(origDimensions, ", ") & linefeed & "origResolution : " & my recolle(origResolution, ", ") & linefeed & "newDimensions : " & my recolle(newDimensions, ", ") & linefeed & "newResolution : " & my recolle(newResolution, ", ") & linefeed & "newSize800 : " & newSize800 & " bytes" & linefeed
	
	tell application "Image Events"
		if mode = 1 then
			set openedFile to open file thePicture
		else
			set openedFile to open thePicture
		end if
		--set fileLocation to the location of openedFile
		--set fileName to the name of openedFile
		set wantedSize to 1600
		set newPath to my stamp(currentFile, wantedSize)
		scale openedFile to size wantedSize
		save openedFile in newPath with icon
		close openedFile
		set openedFile to open file newPath
		set newDimensions to dimensions of openedFile
		set newResolution to resolution of openedFile
		close openedFile
	end tell
	set newSize1600 to size of (info for file newPath)
	tell me to display dialog "origSize : " & origSize & " bytes" & linefeed & "origDimensions : " & my recolle(origDimensions, ", ") & linefeed & "origResolution : " & my recolle(origResolution, ", ") & linefeed & "newDimensions : " & my recolle(newDimensions, ", ") & linefeed & "newResolution : " & my recolle(newResolution, ", ") & linefeed & "newSize1600 : " & newSize1600 & " bytes" & linefeed
	
	tell application "Image Events"
		if mode = 1 then
			set openedFile to open file thePicture
		else
			set openedFile to open thePicture
		end if
		--set fileLocation to the location of openedFile
		--set fileName to the name of openedFile
		set wantedSize to 3200
		set newPath to my stamp(currentFile, wantedSize)
		scale openedFile to size wantedSize
		save openedFile in newPath with icon
		close openedFile
		set openedFile to open file newPath
		set newDimensions to dimensions of openedFile
		set newResolution to resolution of openedFile
		close openedFile
	end tell
	set newSize3200 to size of (info for file newPath)
	tell me to display dialog "origSize : " & origSize & " bytes" & linefeed & "origDimensions : " & my recolle(origDimensions, ", ") & linefeed & "origResolution : " & my recolle(origResolution, ", ") & linefeed & "newDimensions : " & my recolle(newDimensions, ", ") & linefeed & "newResolution : " & my recolle(newResolution, ", ") & linefeed & "newSize3200 : " & newSize3200 & " bytes" & linefeed
end repeat
--end open

on stamp(anAlias, theSize)
	set theExt to name extension of (info for anAlias)
	tell (current date) to set theStamp to (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
	set newPath to text 1 thru -(2 + (count theExt)) of (anAlias as string) & "_" & theStamp & "_" & theSize & "." & theExt
	return newPath
end stamp

#=====
on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle
#=====

What is really puzzling me is the fact that changeAgent sent me a video in which I see the droplet whose every instructions are disabled which execute the instruction triggering choose file.
I sent it to Shane Stanley hoping that he may explain that.
I hope that changeAgent did not made a mix of different videos to fool me but honestly I’m wondering.

Since the beginning I’m puzzled by what is described so, when I saw that the original file has a large block of null bytes at its end, I thought that it was the explanation. But some times after, when I saw that the size was correctly changed on the original file, I assumed that the supposed oddity was specific to Catalina, an OS with so many anomalies that a new one would not be surprising (in French we say “On ne prête qu’aux riches”).

Time was not completely wasted.
Now, everybody here know how to remove easily the extraneous null bytes from the end of jpeg files :wink:

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 24 mai 2020 22:48:24

Given the OP is primarily interested in file size, here’s another approach. In this case you pass not only a maximum height/width, but also a target file size in bytes. The script will rescale the pic and try with a low compression factor of 0.8 (scale is 0.0 to 1.0). If the result was too big, it will reduce the compression factor by 10% and try again, repeating until the file size is below the limit, or the compression factor hits 0.2 (point of diminishing returns).

use scripting additions
use framework "Foundation"
use framework "AppKit"

on resizedJpegFromPath:imagePath maxHeightOrWidth:maxDim targetBytes:maxBytes
	set imagePath to current application's NSString's stringWithString:imagePath
	set outPath to (imagePath's stringByDeletingPathExtension()'s stringByAppendingString:"-out")
	set outPath to outPath's stringByAppendingPathExtension:"jpg"
	-- Get the contents of the passed picture
	set theImageRep to current application's NSBitmapImageRep's imageRepWithContentsOfFile:imagePath -- load the file 
	set theClip to current application's NSPasteboard's generalPasteboard()
	-- Extract its original size
	set oldHeight to theImageRep's pixelsHigh()
	set oldWidth to theImageRep's pixelsWide()
	if oldWidth > oldHeight then
		set theWidth to maxDim
		set theHeight to oldHeight * maxDim / oldWidth
	else
		set theHeight to maxDim
		set theWidth to oldWidth * maxDim / oldHeight
	end if
	set newRep to (current application's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:theWidth pixelsHigh:theHeight bitsPerSample:8 samplesPerPixel:4 hasAlpha:yes isPlanar:false colorSpaceName:(current application's NSDeviceRGBColorSpace) bytesPerRow:0 bitsPerPixel:32)
	-- store the existing graphics context
	current application's NSGraphicsContext's saveGraphicsState()
	-- set graphics context to new context based on the new bitmapImageRep
	current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:newRep)
	theImageRep'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 respectFlipped:false hints:(missing value)
	-- restore state
	current application's NSGraphicsContext's restoreGraphicsState()
	set compFactor to 0.8 -- starting compression value
	set compMax to 0.2 -- compression limit
	repeat
		set theData to newRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:compFactor}
		if compFactor > compMax and theData's |length|() > maxBytes then
			-- too big, so try with more compression
			set compFactor to compFactor * 0.9
		else -- give up
			exit repeat
		end if
	end repeat
	theData's writeToFile:outPath atomically:true
end resizedJpegFromPath:maxHeightOrWidth:targetBytes:

set anImage to choose file of type {"public.png", "public.tiff", "public.jpeg"}
my resizedJpegFromPath:(POSIX path of anImage) maxHeightOrWidth:800 targetBytes:100000

good morning I am back on line.

Yvan if I was that clever, to rig the video, I did not need help with this script. :-)) Indeed, it is unedited footage, only shrunk to a smaller size to be able to send it in an email. Scouts honour.

Shane your script works. It shrinks a picture from 2.2MB to 86KB.

Is there a way to make it in to a droplet? And in what line do I change the size it shrinks the picture in to?

Yvan good morning.

Your script makes an error.

Can’t get file “Seva Server:Users:ganesh:Desktop:3_20200525_101434_800.jpg” of application “Image Events”.

The log said:

tell application “Script Editor”
choose file of type {“public.jpeg”} with multiple selections allowed
→ {alias “Seva Server:Users:ganesh:Desktop:3.jpg”}
end tell
tell application “Image Events”
launch
end tell
tell current application
info for alias “Seva Server:Users:ganesh:Desktop:3.jpg”
→ {name:“3.jpg”, creation date:date “Monday, 25 May 2020 at 09:15:20”, modification date:date “Monday, 25 May 2020 at 09:15:20”, size:2218263, folder:false, alias:false, package folder:false, visible:true, extension hidden:false, name extension:“jpg”, displayed name:“3.jpg”, default application:alias “Seva Server:System:Applications:Preview.app:”, kind:“JPEG image”, file type:“file creator:“type identifier:“public.jpeg”, locked:false, busy status:false, short version:””, long version:“”}
end tell
tell application “Image Events”
open file “Seva Server:Users:ganesh:Desktop:3.jpg”
→ image “3.jpg”
end tell
tell current application
info for alias “Seva Server:Users:ganesh:Desktop:3.jpg”
→ {name:“3.jpg”, creation date:date “Monday, 25 May 2020 at 09:15:20”, modification date:date “Monday, 25 May 2020 at 09:15:20”, size:2218263, folder:false, alias:false, package folder:false, visible:true, extension hidden:false, name extension:“jpg”, displayed name:“3.jpg”, default application:alias “Seva Server:System:Applications:Preview.app:”, kind:“JPEG image”, file type:“file creator:“type identifier:“public.jpeg”, locked:false, busy status:false, short version:””, long version:“”}
current date
→ date “Monday, 25 May 2020 at 10:15:18”
end tell
tell application “Image Events”
get dimensions of image “3.jpg”
→ {}
get resolution of image “3.jpg”
→ {}
scale image “3.jpg” to size 800
save image “3.jpg” in “Seva Server:Users:ganesh:Desktop:3_20200525_101518_800.jpg” with icon
→ missing value
close image “3.jpg”
open file “Seva Server:Users:ganesh:Desktop:3_20200525_101518_800.jpg”
→ error number -1728 from file “Seva Server:Users:ganesh:Desktop:3_20200525_101518_800.jpg”
Result:
error “Image Events got an error: Can’t get file "Seva Server:Users:ganesh:Desktop:3_20200525_101518_800.jpg".” number -1728 from file “Seva Server:Users:ganesh:Desktop:3_20200525_101518_800.jpg”

PS I am testing presently on a different 10.15 machine.

Thank you

Sorry, what you report does not match what other users get under 10.15 with the same script.
As I am not a diviner it is not possible for me to guess what exotic adjustment was made on your machine.
This is the first time this has happened but I have to give up.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 25 mai 2020 11:33:38

Yvan, I know you have no crystal ball. I am using a Mac mini 2020 to test on this morning, that has a fresh 10.15 install (2 weeks ago) and the latest update to 10.15.4.

There are non what so ever exotic adjustments on it. Zero. There are no applications running on it that are not Mac Native and of those only a few. It is my blind server and mainly used for backup storage and sending out automatic mails via the native app Mail.

I switched to the MM as I have been testing so far on an external HD running 10.15.4 on my MBP. I thought this might influence stuff negatively.

Could the error be in the fact that I run an Englisch OS version and you a French?

PS.
I can still test later today if the same error happens on the other machine.
Would this be helpful?

Yvan, I have fired up the MBP with the external HD.

Your script runs there and makes 3 pictures.

The original is 1.8 M and your script makes 3 pictures of 2.2 – 2.9 – 3.7 MB. As well as larger icons.

The script doesn’t rely upon localized features.
What I know is that Catalina requires some specific settings to be able do achieve some tasks.
As I can’t install this system, I am unable to list those settings. I just know that they mainly are supposed to give some permissions to the scripts.

Back to the applet which you sent me.
I really don’t understand how you were able to create such a mess.
Your package contain three copies of the script.
It’s normal for a droplet created with Script Debugger.
What is odd is that these versions don’t match.
What appears when we ask Script Editor to open the script is the content of the embedded file named “main.rtf” but, what is executed in the content of the embedded file named “main.scpt” (which is identical to what is in the embedded file named “main.recover.rtf”).

Please, open your applet in Script Editor
select all
create a new empty script
paste in it
save as “4Yvan.app”
then drag and drop a jpeg upon it.
With this clean app you will see that what I wrote is the normal behavior.
As I can’t explain what you did to build the foolish applet I think that the only thing to do is to trash it and quickly empty the trash.

If somebody may describe the settings required by Catalina, maybe you will solve your problem.
I apologize but for me, GAME OVER.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 25 mai 2020 13:46:21

So Yvan sent me a copy of the script. The explanation is that the script you saved is not commented out, but the version that Script Editor auto-saved was made after the script was commented out. So when you opened the script again you saw the last auto-saved version, but the document was still dirty (a black dot in the red button in the title bar). However, that version isn’t the version that runs when you double-click.I suspect that somehow something has got damaged along the line after that.

The moral of the story: always save, never rely on auto-save.

Add this to the script:

on open draggedItems
	repeat with currentFile in draggedItems
		-- change targetBytes value to max file size in bytes
		(my resizedJpegFromPath:(POSIX path of currentFile) maxHeightOrWidth:800 targetBytes:100000)
	end repeat
end open

Change that 100000 to however many bytes is your limit.

Thanks Shane learning all the time! much appreciated.

Yes, I know this and here I am complete sure all is well. All my other scripts run flawlessly and all permissions in ‘Privacy’ tab are given.

Neither do I but I think Shane expained how it possibly could have happened.

I did that, and hate to say it does not work.

I think my settings are correct in the system. However should anybody have a tip as to what to check I love to hear.

I sincerely apologise if I upset you. I might not be the best AS scripter (I know I am not) but at no time did I intently try to upset the system. I am enormously grateful for the help you and others gave and have given me in the past solving AS challenges. Once again very sorry for the mess.

If I remember well, you were already urged to turn on Full Disk Access for Image Events.

I can’t do that here but I will send you a screenshot in a personal message.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 25 mai 2020 15:52:52

Shane thanks for this.

I now composed the script like this:


on open draggedItems
	repeat with currentFile in draggedItems
		-- change targetBytes value to max file size in bytes
		(my resizedJpegFromPath:(POSIX path of currentFile) maxHeightOrWidth:800 targetBytes:100000)
	end repeat
end open

use scripting additions
use framework "Foundation"
use framework "AppKit"

on resizedJpegFromPath:imagePath maxHeightOrWidth:maxDim targetBytes:maxBytes
	set imagePath to current application's NSString's stringWithString:imagePath
	set outPath to (imagePath's stringByDeletingPathExtension()'s stringByAppendingString:"-out")
	set outPath to outPath's stringByAppendingPathExtension:"jpg"
	-- Get the contents of the passed picture
	set theImageRep to current application's NSBitmapImageRep's imageRepWithContentsOfFile:imagePath -- load the file 
	set theClip to current application's NSPasteboard's generalPasteboard()
	-- Extract its original size
	set oldHeight to theImageRep's pixelsHigh()
	set oldWidth to theImageRep's pixelsWide()
	if oldWidth > oldHeight then
		set theWidth to maxDim
		set theHeight to oldHeight * maxDim / oldWidth
	else
		set theHeight to maxDim
		set theWidth to oldWidth * maxDim / oldHeight
	end if
	set newRep to (current application's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:theWidth pixelsHigh:theHeight bitsPerSample:8 samplesPerPixel:4 hasAlpha:yes isPlanar:false colorSpaceName:(current application's NSDeviceRGBColorSpace) bytesPerRow:0 bitsPerPixel:32)
	-- store the existing graphics context
	current application's NSGraphicsContext's saveGraphicsState()
	-- set graphics context to new context based on the new bitmapImageRep
	current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:newRep)
	theImageRep'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 respectFlipped:false hints:(missing value)
	-- restore state
	current application's NSGraphicsContext's restoreGraphicsState()
	set compFactor to 0.8 -- starting compression value
	set compMax to 0.2 -- compression limit
	repeat
		set theData to newRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:compFactor}
		if compFactor > compMax and theData's |length|() > maxBytes then
			-- too big, so try with more compression
			set compFactor to compFactor * 0.9
		else -- give up
			exit repeat
		end if
	end repeat
	theData's writeToFile:outPath atomically:true
end resizedJpegFromPath:maxHeightOrWidth:targetBytes:

set anImage to choose file of type {"public.png", "public.tiff", "public.jpeg"}
my resizedJpegFromPath:(POSIX path of anImage) maxHeightOrWidth:800 targetBytes:100000

in your post you said to chance the 100000 to change the output size of the file.

Is this in this line (top)?


		(my resizedJpegFromPath:(POSIX path of currentFile) maxHeightOrWidth:800 targetBytes:100000)

Or this line (bottom)?


my resizedJpegFromPath:(POSIX path of anImage) maxHeightOrWidth:800 targetBytes:100000

Or both?

I ask as any changes made in each of these lines or in both at the same time, make no difference. The output is always a 48KB file. For example I tried 1000000000 and 100 no difference.

Or did I not understand you correctly?

PS
I make a new AS doc and paste the whole text in every time. Next save is as an application. No save as or duplicate.

Indeed and I confirmed it was on.

I hate to write that it’s the kind of sentence which really doesn’t help.
Are you writing about the applet with every instructions disabled ?
If it’s that, what’s the meaning of your statement ?

I will deliberately shout.
YOU WERE WARNED SEVERAL TIMES THAT WHEN IT’S SAVED CORRECTLY, SUCH SCRIPT DOES ABSOLUTELY NOTHING.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 25 mai 2020 16:04:54

It sounds like you’ve modified the document, but not saved it. Also, extreme values are a bad test; it will give up before it gets anywhere near 100, and it will start off under 100000000 anyway.

OK got it to work under 10.15 Shane. Thanks for that.