I’m trying to write a script that will convert all image files in a certain directory. Apple has a great script for converting single images using image event. Is there a way this can be modified to work on an entire directory?
Please help!
Apple’s default script:
set this_file to choose file without invisibles
try
tell application "Image Events"
launch
-- get the parent folder of the image file
set the parent_folder to the container of this_file
-- derive new name for the new image file
set the new_name to my add_extension(this_file, "tif")
-- look for an existing file
if (exists file new_name of the parent_folder) then
error "A file named \"" & new_name & "\" already exists."
end if
-- open the image file
set this_image to open this_file
-- save in new file. The result is a file ref to the new file
set the new_image to save this_image as TIFF in file new_name of the parent_folder with icon
-- purge the open image data
close this_image
end tell
tell application "Finder"
-- delete the original file
delete this_file
end tell
on error error_message
display dialog error_message
end try
on add_extension(this_file, new_extension)
set this_info to the info for this_file
set this_name to the name of this_info
set this_extension to the name extension of this_info
if this_extension is missing value then
set the default_name to this_name
else
set the default_name to text 1 thru -((length of this_extension) + 2) of this_name
end if
return (the default_name & "." & the new_extension)
end add_extension
set image_folder to choose folder with prompt "Choose folder of images." without invisibles
tell application "Finder" to set images_files to every file of folder image_folder
repeat with i from 1 to count of images_files
set this_image to item i of images_files as alias
my convert_images(this_image)
end repeat
on convert_images(this_file)
try
tell application "Image Events"
launch
-- get the parent folder of the image file
set the parent_folder to the container of this_file
-- derive new name for the new image file
set the new_name to my add_extension(this_file, "tif")
-- look for an existing file
if (exists file new_name of the parent_folder) then
error "A file named \"" & new_name & "\" already exists."
end if
-- open the image file
set this_image to open this_file
-- save in new file. The result is a file ref to the new file
set the new_image to save this_image as TIFF in file new_name of the parent_folder with icon
-- purge the open image data
close this_image
end tell
tell application "Finder"
-- delete the original file
delete this_file
end tell
on error error_message
display dialog error_message
end try
end convert_images
on add_extension(this_file, new_extension)
set this_info to the info for this_file
set this_name to the name of this_info
set this_extension to the name extension of this_info
if this_extension is missing value then
set the default_name to this_name
else
set the default_name to text 1 thru -((length of this_extension) + 2) of this_name
end if
return (the default_name & "." & the new_extension)
end add_extension
Thank you so much!
I’m trying now to set the destination that all converted files will be placed in a choosen folder. I hope that I’m not being a pain, I’m new to all this.
Here’s the script I have now. The destination folder is set at “destination”. But I keep getting the error “The variable destination is not defined.”
What am I doing wrong? I have the variable being set in the third line of the script. I’m confused. Any help is appreciated.
JPEG CONVERT SCRIPT:
set image_folder to "export"
tell application "Finder" to set images_files to every file of folder image_folder
set destination to choose folder without invisibles
repeat with i from 1 to count of images_files
set this_image to item i of images_files as alias
my convert_images(this_image)
end repeat
on convert_images(this_file)
try
tell application "Image Events"
launch
-- get the parent folder of the image file
set the parent_folder to the container of this_file
-- derive new name for the new image file
set the new_name to my add_extension(this_file, "jpeg")
-- look for an existing file
if (exists file new_name of destination) then
error "A file named \"" & new_name & "\" already exists."
end if
-- open the image file
set this_image to open this_file
-- save in new file. The result is a file ref to the new file
set the new_image to save this_image as JPEG in file new_name of destination with icon
-- purge the open image data
close this_image
end tell
tell application "Finder"
-- delete the original file
delete this_file
end tell
on error error_message
display dialog error_message
end try
end convert_images
on add_extension(this_file, new_extension)
set this_info to the info for this_file
set this_name to the name of this_info
set this_extension to the name extension of this_info
if this_extension is missing value then
set the default_name to this_name
else
set the default_name to text 1 thru -((length of this_extension) + 2) of this_name
end if
return (the default_name & "." & the new_extension)
end add_extension
When you are using a handler you have to supply it the variables you want it to know about.
Make these changes in the following code.
repeat with i from 1 to count of images_files
set this_image to item i of images_files as alias
--This is where the handler is called
my convert_images(this_image, destination) -- HERE WE ARE ADDING THE *destination* variable
end repeat
--This is a handler
on convert_images(this_file, destination) -- HERE WE ARE ADDING THE *destination* variable