Really simple.
Your jpeg is one of the ‘corrupted’ ones which are available since a few months.
When I looked at its contents with an hexadecimal editor I found a large number of null bytes at its end.
These bytes prevent SIPS or Image Events to achieve their job.
I removed these extraneous bytes so, the original file (1754025 bytes) became a 1560661 bytes one.
When I treated it with SIPS I got a 52949 bytes one.
Some days ago I wrote a script designed to clean such ‘corrupted’ files.
Shane Stanley gave me a more efficient one.
Here it is.
Yvan,
Here’s another approach. It’s still a bit on the dirty side, but I see the first file I tried contained FFD900 more than once. The problem is to avoid accidentally running the code on a file that has already been trimmed. There’s probably a more efficient way, but it shouldn’t be too bad:
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions
set theFile to choose file
--search for FFD900
set b64String to "/9kA" -- FFD900 in Base64
set b64Data to current application's NSData's alloc()'s initWithBase64EncodedString:b64String options:0
set theData to current application's NSData's dataWithContentsOfURL:theFile
set dataLen to theData's |length|()
set theRange to theData's rangeOfData:b64Data options:(current application's NSDataSearchBackwards) range:{0, dataLen}
if |length| of theRange = 3 then
-- make sure only nulls after it by making base64 string of the rest, then looking for other than A or = in it
set maxRange to current application's NSMaxRange(theRange)
set endData to theData's subdataWithRange:{maxRange + 1, dataLen - maxRange - 1}
set endB64 to (endData's base64EncodedStringWithOptions:0)
set newRange to endB64's rangeOfString:"[^A=]" options:(current application's NSRegularExpressionSearch)
if |length| of newRange is 0 then
-- we know it's only nulls
set eof theFile to ((location of theRange) + 2)
end if
end if
On my side I encapsulated it in a script which filter Jpegs in every cases in droplet as well as in applet.
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions
on run
set theFiles to choose file with prompt "Sélectionner un ou plusieurs fichiers jpg" of type {"public.jpeg"} with multiple selections allowed
repeat with aFile in theFiles
my removeGarbage(aFile)
end repeat
end run
on open theFiles -- alas list of drag & dropped items
tell application "System Events"
repeat with aFile in theFiles
if type identifier of aFile is "public.jpeg" then my removeGarbage(aFile)
end repeat
end tell
end open
on removeGarbage(theFile)
-- handler written by Shane Stanley on 2020/04/30
--search for FFD900
set b64String to "/9kA" -- FFD900 in Base64
set b64Data to current application's NSData's alloc()'s initWithBase64EncodedString:b64String options:0
set theData to current application's NSData's dataWithContentsOfURL:theFile
set dataLen to theData's |length|()
set theRange to theData's rangeOfData:b64Data options:(current application's NSDataSearchBackwards) range:{0, dataLen}
if |length| of theRange = 3 then
-- make sure only nulls after it by making base64 string of the rest, then looking for other than A or = in it
set maxRange to current application's NSMaxRange(theRange)
set endData to theData's subdataWithRange:{maxRange + 1, dataLen - maxRange - 1}
set endB64 to (endData's base64EncodedStringWithOptions:0)
set newRange to endB64's rangeOfString:"[^A=]" options:(current application's NSRegularExpressionSearch)
if |length| of newRange is 0 then
-- we know it's only nulls
set eof theFile to ((location of theRange) + 2)
end if
end if
end removeGarbage
In an other message, Shane wrote : If you save it from Script Debugger, you will see in the Resources pane that you can restrict drag-and-drop to just jpeg files.
Try it and let all of us know if this time you get the wanted result.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 24 mai 2020 10:08:38
PS: not sure that the problem is solved for you. Here, when I apply SIPs on your original file, it’s correctly reduced.
I am not able to make it work. I am now testing under 10.13, like you.
Yes the script shrinks the picture and always to 229MB.
I cannot control the size in any way, I tried numbers from 1 to 50000 and the output size is always the same.
So I started testing.
As you see in the script below I can disable almost of all of it and it still works.
on open draggeditems
--set draggeditems to choose file of type {"public.jpeg"} with multiple selections allowed
--set text1 to my recolle(draggeditems, linefeed)
--repeat with aFile in draggeditems
--set contents of aFile to quoted form of POSIX path of aFile & space
--end repeat
--set text2 to my recolle(draggeditems, linefeed)
-- display dialog text1 & linefeed & text2
-- do shell script "sips --resampleHeightWidthMax 50000 " & draggeditems
end open
#=====
--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
#=====
This is not possible. So I pasted the clean text in a new one and it does not work.
on open draggeditems
end open
Now, I am not an expert here, but something is funny. I would guess that there is code that keeps ‘hanging’ in the script. Or?
What do you think and could this be the cause of the challenges I have with the script?
Now my original script:
tell application "Image Events"
launch
end tell
on open draggeditems
repeat with currentFile in draggeditems
tell application "Image Events"
set openedFile to open (currentFile as alias)
--Part 3:
set fileLocation to the location of openedFile
set fileName to the name of openedFile
--Part 4:
scale openedFile to size 800
save openedFile with icon
close openedFile
end tell
--Part 5:
tell application "Finder"
set the name of file fileName of fileLocation to ¬
(fileName)
end tell
end repeat
end open
It still works but I can not influence the size (like in the other proposed script here) anymore. I had 3 scripts, where I had different numbers (here 800) like 700 and 100. This would influence the size. And it still does if I use the originals. But as soon as I copy to a new file or change the number it goes to 54 mb.
Please click the button [Open this Scriplet in your Editor:] which appears below:
and save this script as “resize to 800.app” with absolutely no change !
-- save this one as "resize to 800.app"
-- I disabled useless instructions
on open draggeditems
tell application "Image Events" to launch
repeat with currentFile in draggeditems
tell application "Image Events"
set openedFile to open file (currentFile as string)
--Part 3:
--set fileLocation to the location of openedFile
--set fileName to the name of openedFile
--Part 4:
scale openedFile to size 800
save openedFile with icon
close openedFile
end tell
(*
--Part 5:
tell application "Finder"
set the name of file fileName of fileLocation to ¬
(fileName)
end tell
*)
end repeat
end open
Please click the button [Open this Scriplet in your Editor:] which appears below:
and save this script as “resize to 1200.app” with absolutely no change !
-- save this one as "resize to 1200.app"
-- I disabled useless instructions
on open draggeditems
tell application "Image Events" to launch
repeat with currentFile in draggeditems
tell application "Image Events"
set openedFile to open file (currentFile as string)
--Part 3:
--set fileLocation to the location of openedFile
--set fileName to the name of openedFile
--Part 4:
scale openedFile to size 1200
save openedFile with icon
close openedFile
end tell
(*
--Part 5:
tell application "Finder"
set the name of file fileName of fileLocation to ¬
(fileName)
end tell
*)
end repeat
end open
EDITED FROM HERE
Please click the button [Open this Scriplet in your Editor:] which appears below:
and save this script as “resize to 1600.app” with absolutely no change !
-- save this one as "resize to 1600.app"
-- I disabled useless instructions
on open draggeditems
tell application "Image Events" to launch
repeat with currentFile in draggeditems
tell application "Image Events"
set openedFile to open file (currentFile as string)
--Part 3:
--set fileLocation to the location of openedFile
--set fileName to the name of openedFile
--Part 4:
scale openedFile to size 1600
save openedFile with icon
close openedFile
end tell
(*
--Part 5:
tell application "Finder"
set the name of file fileName of fileLocation to ¬
(fileName)
end tell
*)
end repeat
end open
Please click the button [Open this Scriplet in your Editor:] which appears below:
and save this script as “three sizes.scpt” with absolutely no change !
UNTIL HERE
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 origSizeXXX 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 origDimensions to dimensions of openedFile
set origResolution to resolution of openedFile
scale openedFile to size 800
save openedFile with icon
set newDimensions to dimensions of openedFile
set newResolution to resolution of openedFile
close openedFile
end tell
set newSize800 to size of (info for currentFile)
tell me to display dialog "origSizeXXX : " & origSizeXXX & " 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
set origSize800 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 origDimensions to dimensions of openedFile
set origResolution to resolution of openedFile
scale openedFile to size 1600
save openedFile with icon
set newDimensions to dimensions of openedFile
set newResolution to resolution of openedFile
close openedFile
end tell
set newSize1600 to size of (info for currentFile)
tell me to display dialog "origSize800 : " & origSize800 & " 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
set origSize1600 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 origDimensions to dimensions of openedFile
set origResolution to resolution of openedFile
scale openedFile to size 3200
save openedFile with icon
set newDimensions to dimensions of openedFile
set newResolution to resolution of openedFile
close openedFile
end tell
set newSize3200 to size of (info for currentFile)
tell me to display dialog "origSize1600 : " & origSize1600 & " 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 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
#=====
It’s a slightly edited version of the one which I sent to your mailbox.
Run it from the Script Editor without any change.
You are supposed to get this log history:
Please don't click the button [Open this Scriplet in your Editor:],
this is not a script but a log history !
tell application "Script Editor"
choose file of type {"public.jpeg"} with multiple selections allowed
--> {alias "SSD 1000:Users:**********:Desktop:2 copy 2.jpg"}
end tell
tell application "Image Events"
launch
end tell
tell current application
info for alias "SSD 1000:Users:**********:Desktop:2 copy 2.jpg"
--> {name:"2 copy 2.jpg", creation date:date "mercredi 6 mai 2020 à 17:16:56", modification date:date "mercredi 6 mai 2020 à 17:16:56", size:1754025, folder:false, alias:false, package folder:false, visible:true, extension hidden:false, name extension:"jpg", displayed name:"2 copy 2.jpg", default application:alias "SSD 1000:Applications:Preview.app:", kind:"Image JPEG", 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 "SSD 1000:Users:**********:Desktop:2 copy 2.jpg"
--> image "2 copy 2.jpg"
get dimensions of image "2 copy 2.jpg"
--> {3264, 1836}
get resolution of image "2 copy 2.jpg"
--> {72.0, 72.0}
scale image "2 copy 2.jpg" to size 800
save image "2 copy 2.jpg" with icon
--> file "SSD 1000:Users:**********:Desktop:2 copy 2.jpg"
get dimensions of image "2 copy 2.jpg"
--> {800, 450}
get resolution of image "2 copy 2.jpg"
--> {72.0, 72.0}
close image "2 copy 2.jpg"
end tell
tell current application
info for alias "SSD 1000:Users:**********:Desktop:2 copy 2.jpg"
--> {name:"2 copy 2.jpg", creation date:date "dimanche 24 mai 2020 à 13:46:38", modification date:date "dimanche 24 mai 2020 à 13:46:38", size:52949, folder:false, alias:false, package folder:false, visible:true, extension hidden:false, name extension:"jpg", displayed name:"2 copy 2.jpg", default application:alias "SSD 1000:Applications:Preview.app:", kind:"Image JPEG", file type:", file creator:", type identifier:"public.jpeg", locked:false, busy status:false, short version:"", long version:""}
end tell
tell application "Script Editor"
display dialog "origSizeXXX : 1754025 bytes
origDimensions : 3264, 1836
origResolution : 72,0, 72,0
newDimensions : 800, 450
newResolution : 72,0, 72,0
newSize800 : 52949 bytes
"
--> {button returned:"OK"}
end tell
tell current application
info for alias "SSD 1000:Users:**********:Desktop:2 copy 2.jpg"
--> {name:"2 copy 2.jpg", creation date:date "dimanche 24 mai 2020 à 13:46:38", modification date:date "dimanche 24 mai 2020 à 13:46:38", size:52949, folder:false, alias:false, package folder:false, visible:true, extension hidden:false, name extension:"jpg", displayed name:"2 copy 2.jpg", default application:alias "SSD 1000:Applications:Preview.app:", kind:"Image JPEG", 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 "SSD 1000:Users:**********:Desktop:2 copy 2.jpg"
--> image "2 copy 2.jpg"
get dimensions of image "2 copy 2.jpg"
--> {800, 450}
get resolution of image "2 copy 2.jpg"
--> {72.0, 72.0}
scale image "2 copy 2.jpg" to size 1600
save image "2 copy 2.jpg" with icon
--> file "SSD 1000:Users:**********:Desktop:2 copy 2.jpg"
get dimensions of image "2 copy 2.jpg"
--> {1600, 900}
get resolution of image "2 copy 2.jpg"
--> {72.0, 72.0}
close image "2 copy 2.jpg"
end tell
tell current application
info for alias "SSD 1000:Users:**********:Desktop:2 copy 2.jpg"
--> {name:"2 copy 2.jpg", creation date:date "dimanche 24 mai 2020 à 13:46:42", modification date:date "dimanche 24 mai 2020 à 13:46:42", size:126642, folder:false, alias:false, package folder:false, visible:true, extension hidden:false, name extension:"jpg", displayed name:"2 copy 2.jpg", default application:alias "SSD 1000:Applications:Preview.app:", kind:"Image JPEG", file type:", file creator:", type identifier:"public.jpeg", locked:false, busy status:false, short version:"", long version:""}
end tell
tell application "Script Editor"
display dialog "origSize800 : 52949 bytes
origDimensions : 800, 450
origResolution : 72,0, 72,0
newDimensions : 1600, 900
newResolution : 72,0, 72,0
newSize1600 : 126642 bytes
"
--> {button returned:"OK"}
end tell
tell current application
info for alias "SSD 1000:Users:**********:Desktop:2 copy 2.jpg"
--> {name:"2 copy 2.jpg", creation date:date "dimanche 24 mai 2020 à 13:46:42", modification date:date "dimanche 24 mai 2020 à 13:46:42", size:126642, folder:false, alias:false, package folder:false, visible:true, extension hidden:false, name extension:"jpg", displayed name:"2 copy 2.jpg", default application:alias "SSD 1000:Applications:Preview.app:", kind:"Image JPEG", 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 "SSD 1000:Users:**********:Desktop:2 copy 2.jpg"
--> image "2 copy 2.jpg"
get name of image "2 copy 2.jpg"
--> "2 copy 2.jpg"
get dimensions of image "2 copy 2.jpg"
--> {1600, 900}
get resolution of image "2 copy 2.jpg"
--> {72.0, 72.0}
scale image "2 copy 2.jpg" to size 3200
save image "2 copy 2.jpg" with icon
--> file "SSD 1000:Users:**********:Desktop:2 copy 2.jpg"
get dimensions of image "2 copy 2.jpg"
--> {3200, 1800}
get resolution of image "2 copy 2.jpg"
--> {72.0, 72.0}
close image "2 copy 2.jpg"
end tell
tell current application
info for alias "SSD 1000:Users:**********:Desktop:2 copy 2.jpg"
--> {name:"2 copy 2.jpg", creation date:date "dimanche 24 mai 2020 à 13:46:44", modification date:date "dimanche 24 mai 2020 à 13:46:44", size:320141, folder:false, alias:false, package folder:false, visible:true, extension hidden:false, name extension:"jpg", displayed name:"2 copy 2.jpg", default application:alias "SSD 1000:Applications:Preview.app:", kind:"Image JPEG", file type:", file creator:", type identifier:"public.jpeg", locked:false, busy status:false, short version:"", long version:""}
end tell
tell application "Script Editor"
display dialog "origSize1600 : 126642 bytes
origDimensions : 1600, 900
origResolution : 72,0, 72,0
newDimensions : 3200, 1800
newResolution : 72,0, 72,0
newSize3200 : 320141 bytes
"
--> {button returned:"OK"}
end tell
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 24 mai 2020 14:20:46
The script above does absolutely nothing !
So I don’t understand what means your statement : and it still works.
The script above, which is in fact exactly the same than the first one, does absolutely nothing !
So I don’t understand what means your statement : and it does not work.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 24 mai 2020 14:30:28
I can’t guess what you really did but I repeat: I’m sure that the script listed in this message didn’t shrink the picture, it does ABSOLUTELY NOTHING".
The main problem here is that you don’t understand what the instructions are supposed to achieve.
I will try to explain them one by one.
on open draggeditems Is the entry point used when something is dropped upon the app’s icon
--set draggeditems to choose file of type {"public.jpeg"} with multiple selections allowed
Is a disabled instruction which I enable when I test the script in its applet structure (in such case, on open draggeditems is disabled)
--set text1 to my recolle(draggeditems, linefeed)
When it’s enabled, this instruction change the list of files dragged on the icon into a block of text like :
SSD 1000:Users:DSC_0257 - opposite.jpg
SSD 1000:Users:DSC_0257 - rotated.jpg
SSD 1000:Users:DSC_0257 - upDown.jpg
SSD 1000:Users:DSC_0257.JPG
--repeat with aFile in draggeditems
--set contents of aFile to quoted form of POSIX path of aFile & space
--end repeat
When it’s enabled, this loop replace the list of dropped aliases by a list of quoted POSIX paths
like :
{“/Users//Desktop/DSC_0257 - opposite.jpg", "/Users//Desktop/DSC_0257 - rotated.jpg”, “/Users//Desktop/DSC_0257 - upDown.jpg", "/Users//Desktop/DSC_0257.JPG”}
--set text2 to my recolle(draggeditems, linefeed)
When it’s enabled, this instruction change the list of POSIX Paths into a block of text like :
“‘/Users//Desktop/DSC_0257 - opposite.jpg’
'/Users//Desktop/DSC_0257 - rotated.jpg’
‘/Users//Desktop/DSC_0257 - upDown.jpg’
'/Users//Desktop/DSC_0257.JPG’”
-- display dialog text1 & linefeed & text2
When it’s enabled, this instruction display the block of text built just before.
-- do shell script "sips --resampleHeightWidthMax 50000 " & draggeditems
When it’s enabled, this instruction apply the command supposed to shrink the files described by the list of quoted POSIX Paths.
end open
This instruction is the the end point of the handler.
In the posted script, all these instructions, except on open draggeditems an end open, are disabled so they do nothing.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 24 mai 2020 15:31:37
I guess that it’s the one entitled “three sizes.scpt”
May you, at last, post what you are asked to do:
I want to see the log history.
I carefully sent you a copy of it showing in huge 24 points size the different descriptions of the file.
With the original file which you sent to me, you were supposed to see three dialogs,
dialog #1 "origSizeXXX : 1754025 bytes
origDimensions : 3264, 1836
origResolution : 72,0, 72,0
newDimensions : 800, 450
newResolution : 72,0, 72,0 newSize800 : 52949 bytes
I don’t understand how you may have missed that because you were urged three times to click the button [OK] in the dialogs displaying the values listed above.
This said, if you save the script below as an application (a droplet)
on open draggeditems
tell application "Image Events" to launch
repeat with currentFile in draggeditems
tell application "Image Events"
set openedFile to open file (currentFile as string)
scale openedFile to size 678 --<<<< here we define the wanted greater dimension
save openedFile with icon
close openedFile
end tell
end repeat
end open
and drag and drop a jpeg file on its icon, you will get a file whose greater dimension is 678 pixels.
If you save the script below as an application (a droplet)
on open draggeditems
tell application "Image Events" to launch
repeat with currentFile in draggeditems
tell application "Image Events"
set openedFile to open file (currentFile as string)
scale openedFile to size 800 --<<<< here we define the wanted greater dimension
save openedFile with icon
close openedFile
end tell
end repeat
end open
and drag and drop a jpeg file on its icon, you will get a file whose greater dimension is 800 pixels.
If you save the script below as an application (a droplet)
on open draggeditems
tell application "Image Events" to launch
repeat with currentFile in draggeditems
tell application "Image Events"
set openedFile to open file (currentFile as string)
scale openedFile to size 1200 --<<<< here we define the wanted greater dimension
save openedFile with icon
close openedFile
end tell
end repeat
end open
and drag and drop a jpeg file on its icon, you will get a file whose greater dimension is 1200 pixels.
Must I continue ?
As I already wrote, don’t edit one to change the wanted size, apply copy from a script, paste in a blank one, edit the size to fit your needs, save it as an application with a new name.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 24 mai 2020 16:00:05
I tested all of these script, did not change anything, copied straight from forum in apple script, running under 10.15. no file change is happening - as in getting smaller.
Script one increases the file by 0.4MB
Script two increases the file by 0.4MB
Script three increases the file by 0.9MB
Yvan. This seems a lost cause but, FWIW, I tested your first script from post 52 with PNG and JPEG files. I tested each file twice–once with “with icon” enabled and once with “with icon” disabled. All of the files were located on my boot drive and all of the files after processing by the script contained a width of 678 pixels.
The file sizes were:
PNG Original - 226 KB
PNG with Icon - 385 KB
PNG without Icon - 56 KB
JPEG Original - 3.9 MB
JPEG with Icon - 3 MB
JPEG without Icon - 89 KB
The increased file size with icons may not be surprising. In a different context Shane explained this as follows:
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
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
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.
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.
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