There’s no need to involve Preview in this process, although if that’s really the route you wanted to go down, it’s perfectly possible. However, it doesn’t sound like Preview is intrinsic to your needs, and there’s a slightly slicker method of achieving the conversion:
property text item delimiters : "."
-----------------------------------------------------------
# Ask the user to select a .webp image file, from which
# we extract the path for its containing folder, and
# the name of the file
tell [choose file of type ("org.webmproject.webp") ¬
with prompt "Select .webp Image File", "::"] ¬
to tell {path:item 1's POSIX path, folder:¬
POSIX path of my alias named it} to set WebP ¬
to it & {name:text (1 + (folder's length)) ¬
thru -1 of the path}
# Create the JPEG's default file name by swapping
# out the ".webp" file extension for ".jpg", and
# use the WebP's source folder as the default JPEG
# destination folder
tell the text items of WebP's name to set [JPEG, ¬
last item] to [{path:[WebP's folder, it] ¬
, name:it}, "jpg"]
# Ask user to destination folder and file name,
# which defaults to the source folder and file name
# but with the ".jpg" file extension
tell (choose file name default name "" & JPEG's name ¬
default location WebP's folder with prompt ¬
"Save .jpg Image File As...") to set the path ¬
of the JPEG to the POSIX path
# Create the output file if it doesn't already exist
# and, in case it does, scrub its contents clean
close access (open for access JPEG's path)
set eof of the path of the JPEG to 0
# Read in the original .webp image file as PNG
# image data, and put this on the clipboard.
read WebP's path as «class PNGf»
set the clipboard to the result
# Read the clipboard contents, this time as JPEG
# image data, and write this out to the output
# file path
get the clipboard as JPEG picture
write the result to the JPEG's path as JPEG picture
# Reveal the .jpeg image file in Finder and
# simultaneously bring Finder to the foreground
tell application id "com.apple.finder" to activate ¬
(reveal JPEG's «class ppth» as POSIX file)
I’ve used comments in the script above to explain each step of the process. But, broadly speaking, it’s requests the user to select a .webp
image file, and then select a destination to save the converted .jpg
. The .webp
image is read into the system clipboard as data, which is then mutated into jpeg
format, written out to a new file, and revealed in Finder.
Tested on macOS Sonoma 14.6 running on an Apple M1 chip