Convert To Rgb For Applescript

hi folks… i think i need some assistance.

i have enabled a folder action of converting any image to a JPG… and it works great but if the original image is CMYK, i.e. a .psd, .tif, etc., it doesn’t convert to RGB.

does anyone know the script to perform that action? And where does it go. Here is the current script. It’s the standard script that comes with OS X.

i really appreciate any assistance anyone can offer.

cheers,

thatguyjames

property done_foldername : “JPEG Images”
property originals_foldername : “Original Images”
property newimage_extension : “jpg”
– the list of file types which will be processed
– eg: {“PICT”, “JPEG”, “TIFF”, “GIFf”}
property type_list : {“TIFF”, “GIFf”, “PNGf”, “PICT”}
– since file types are optional in Mac OS X,
– check the name extension if there is no file type
– NOTE: do not use periods (.) with the items in the name extensions list
– eg: {“txt”, “text”, “jpg”, “jpeg”}, NOT: {“.txt”, “.text”, “.jpg”, “.jpeg”}
property extension_list : {“tif”, “tiff”, “gif”, “png”, “pict”, “pct”, “pdf”, “psd”}

on adding folder items to this_folder after receiving these_items
tell application “Finder”
if not (exists folder done_foldername of this_folder) then
make new folder at this_folder with properties {name:done_foldername}
end if
set the results_folder to (folder done_foldername of this_folder) as alias
if not (exists folder originals_foldername of this_folder) then
make new folder at this_folder with properties {name:originals_foldername}
set current view of container window of this_folder to list view
end if
set the originals_folder to folder originals_foldername of this_folder
end tell
try
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to the info for this_item
if (alias of the item_info is false and the file type of the item_info is in the type_list) or (the name extension of the item_info is in the extension_list) then
tell application “Finder”
my resolve_conflicts(this_item, originals_folder, “”)
set the new_name to my resolve_conflicts(this_item, results_folder, newimage_extension)
set the source_file to (move this_item to the originals_folder with replacing) as alias
end tell
process_item(source_file, new_name, results_folder)
end if
end repeat
on error error_message number error_number
if the error_number is not -128 then
tell application “Finder”
activate
display dialog error_message buttons {“Cancel”} default button 1 giving up after 120
end tell
end if
end try
end adding folder items to

on resolve_conflicts(this_item, target_folder, new_extension)
tell application “Finder”
set the file_name to the name of this_item
set file_extension to the name extension of this_item
if the file_extension is “” then
set the trimmed_name to the file_name
else
set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
end if
if the new_extension is “” then
set target_name to file_name
set target_extension to file_extension
else
set target_extension to new_extension
set target_name to (the trimmed_name & “.” & target_extension) as string
end if
if (exists document file target_name of target_folder) then
set the name_increment to 1
repeat
set the new_name to (the trimmed_name & “.” & (name_increment as string) & “.” & target_extension) as string
if not (exists document file new_name of the target_folder) then
– rename to conflicting file
set the name of document file target_name of the target_folder to the new_name
exit repeat
else
set the name_increment to the name_increment + 1
end if
end repeat
end if
end tell
return the target_name
end resolve_conflicts

– this sub-routine processes files
on process_item(source_file, new_name, results_folder)
– NOTE that the variable this_item is a file reference in alias format
– FILE PROCESSING STATEMENTS GOES HERE
try
– the target path is the destination folder and the new file name
set the target_path to ((results_folder as string) & new_name) as string
with timeout of 900 seconds
tell application “Image Events”
launch – always use with Folder Actions
set this_image to open file (source_file as string)
save this_image as JPEG in file target_path with icon
close this_image
end tell
end timeout
on error error_message
tell application “Finder”
activate
display dialog error_message buttons {“Cancel”} default button 1 giving up after 120
end tell
end try
end process_item

Model: MacBook Pro
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi James,

Image Events can’t convert the color space of an image.
so a graphic application like PhotoShop or Graphic Converter should do it

thanks but do you know the script to make that happen using Graphic Converter?

Unfortunately not. I don’t use Graphic Converter.
But certainly here are some graphic aces who know that

i guess i could use PS CS1… i might have an extra license for it.

are you familiar with scripting for PS to get that done. I’m pretty well versed in PS… just not applescripting for it.
PS Actions are easy.

We could do it in teamwork:
Define an action, which converts to RGB (and maybe saves and closes) the (active) document
then run this script

tell application "Adobe Photoshop CS"
	open file "Path:to:my.Pic.jpg"
	do action "myRGBconversion" from "standardactions.atn"
end tell

The only thing about this little scriplet is that the files i will be dropping in the folder won’t have the same filename. Unless there’s something i’m not understanding about this script. but it looks like i have to put the path and filename in here. But the script above is a folder action that executes anytime you drop a file in it.
The path to my “hot folder” is on my Deskop/CONVERT (folder name)

I wrote the action to do the conversion… so that’s all set.

but i get this error when i try and run the script… choosing a path/filename that i know is valid.

Adobe Photoshop CS2 got an error: General Photoshop error occurred.

  • The object “action “ConverttoRGB” of set “SPIROproduction.atn”” is not currently available.

But it is available. weird.

thanks for your assistance StefanK

James,

try this process_item routine with PhotoShop.
You have to adjust the do action line with your parameters

on process_item(source_file, new_name, results_folder)
	-- NOTE that the variable this_item is a file reference in alias format 
	-- FILE PROCESSING STATEMENTS GOES HERE 
	try
		-- the target path is the destination folder and the new file name
		set the target_path to ((results_folder as string) & new_name) as string
		set source_name to name of (info for source_file)
		with timeout of 900 seconds
			tell application "Adobe Photoshop CS"
				launch -- always use with Folder Actions
				open file (source_file as string)
				do action "myRGBconversion" from "standardactions.atn"
				save document source_name as JPEG in file target_path
				close document new_name
			end tell
		end timeout
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end try
end process_item

i gave it a try… but still doesn’t work. I am using CS2… so maybe the dictionary changed. but basically nothing happens if i run that script.

I like how the folder action i pasted above converts to JPG from any format… all i want is it to make the jpeg an RGB colorspace as well. Seems like that wouldn’t be this difficult… heh. But whatta i know about scripting these days.
I can’t believe someone has needed a script like this before. Not everyone has PS to do simple conversions like this.

thanks for your help StefanK. much appreciated.

The problem using folder actions is, if an error occurs, nothing happens :confused:
You can debug a folder action script commenting out the

on adding folder items to this_folder after receiving these_items - end adding ...

lines, and add two lines at the beginning

set this_folder to choose folder 
set these_items to {choose file}

also comment out the try - on error – end try lines
and see what happens

PS: I’m using CS(1), have you changed the app’s name?

Yup… i changed the app to CS2. the script launched it no problem.

I’ll give this other stuff a try tomorrow… my day is over for today.

thanks for your help… good teamwork.

Hi

I’m not sure of the other problems your having in your script but i use this part of a PS script to convert RGB Docs to CMYK (and it does a bunch of other stuff as well just ignore)
You could just change it round to convert to RGB:

tell application "Adobe Photoshop CS2"
				activate
				open theFile
				set display dialogs to never
				set thisdoc to current document
				if (mode of thisdoc is RGB) then
					change mode thisdoc to CMYK
					set myoptions to {class:EPS save options, encoding:maximum quality JPEG, preview type:JPEG Mac OS}
					save thisdoc in myfile1 as Photoshop EPS with options myoptions appending lowercase extension without copying
					close current document saving no
				else
					if (mode of thisdoc is CMYK) then
						set myoptions to {class:EPS save options, encoding:maximum quality JPEG, preview type:JPEG Mac OS}
						save thisdoc in myfile1 as Photoshop EPS with options myoptions appending lowercase extension without copying
						close current document saving no
					else
						if (mode of thisdoc is grayscale) then
							set myoptions to {class:EPS save options, encoding:maximum quality JPEG, preview type:JPEG Mac OS}
							save thisdoc in myfile1 as Photoshop EPS with options myoptions appending lowercase extension without copying
							close current document saving no
						else
							if (mode of thisdoc is bitmap) then
								close current document saving no
							end if
						end if
					end if
				end if
			end tell

thanks for the reply and the script help guys… but it doesn’t work.
i copy/pasted just like you have it… attached it to a folder action and dropped a .tif in there.

nothing happens.

i just want a script to open a file i drop on the folder… convert to RGB and save it as a JPEG.

that’s it. Seems like the conversion could be added in here somehow… but not sure where it would go.

This script already does the whole “duplicate as JPEG” and all that.

property done_foldername : “JPEG Images”
property originals_foldername : “Original Images”
property newimage_extension : “jpg”
– the list of file types which will be processed
– eg: {“PICT”, “JPEG”, “TIFF”, “GIFf”}
property type_list : {“TIFF”, “GIFf”, “PNGf”, “PICT”}
– since file types are optional in Mac OS X,
– check the name extension if there is no file type
– NOTE: do not use periods (.) with the items in the name extensions list
– eg: {“txt”, “text”, “jpg”, “jpeg”}, NOT: {“.txt”, “.text”, “.jpg”, “.jpeg”}
property extension_list : {“tif”, “tiff”, “gif”, “png”, “pict”, “pct”, “pdf”, “psd”}

on adding folder items to this_folder after receiving these_items
tell application “Finder”
if not (exists folder done_foldername of this_folder) then
make new folder at this_folder with properties {name:done_foldername}
end if
set the results_folder to (folder done_foldername of this_folder) as alias
if not (exists folder originals_foldername of this_folder) then
make new folder at this_folder with properties {name:originals_foldername}
set current view of container window of this_folder to list view
end if
set the originals_folder to folder originals_foldername of this_folder
end tell
try
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to the info for this_item
if (alias of the item_info is false and the file type of the item_info is in the type_list) or (the name extension of the item_info is in the extension_list) then
tell application “Finder”
my resolve_conflicts(this_item, originals_folder, “”)
set the new_name to my resolve_conflicts(this_item, results_folder, newimage_extension)
set the source_file to (move this_item to the originals_folder with replacing) as alias
end tell
process_item(source_file, new_name, results_folder)
end if
end repeat
on error error_message number error_number
if the error_number is not -128 then
tell application “Finder”
activate
display dialog error_message buttons {“Cancel”} default button 1 giving up after 120
end tell
end if
end try
end adding folder items to

on resolve_conflicts(this_item, target_folder, new_extension)
tell application “Finder”
set the file_name to the name of this_item
set file_extension to the name extension of this_item
if the file_extension is “” then
set the trimmed_name to the file_name
else
set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
end if
if the new_extension is “” then
set target_name to file_name
set target_extension to file_extension
else
set target_extension to new_extension
set target_name to (the trimmed_name & “.” & target_extension) as string
end if
if (exists document file target_name of target_folder) then
set the name_increment to 1
repeat
set the new_name to (the trimmed_name & “.” & (name_increment as string) & “.” & target_extension) as string
if not (exists document file new_name of the target_folder) then
– rename to conflicting file
set the name of document file target_name of the target_folder to the new_name
exit repeat
else
set the name_increment to the name_increment + 1
end if
end repeat
end if
end tell
return the target_name
end resolve_conflicts

– this sub-routine processes files
on process_item(source_file, new_name, results_folder)
– NOTE that the variable this_item is a file reference in alias format
– FILE PROCESSING STATEMENTS GOES HERE
try
– the target path is the destination folder and the new file name
set the target_path to ((results_folder as string) & new_name) as string
with timeout of 900 seconds
tell application “Image Events”
launch – always use with Folder Actions
set this_image to open file (source_file as string)
save this_image as JPEG in file target_path with icon
close this_image
end tell
end timeout
on error error_message
tell application “Finder”
activate
display dialog error_message buttons {“Cancel”} default button 1 giving up after 120
end tell
end try
end process_item

I can not do this here at home, But at work I made two droplets for CS2, out of the CS2 Batch actions.

open and save as jpeg
Convert to RGB.

Both Actions could be put together in CS2, and Made into a CS2 droplet.
Then all you do is drag and drop onto it.

I also made a RGB converter using Automator, (Apply ColorSync Profile to images)Preview Actions) which does not need to open the file/s

I note that there is also a 'Change image type action in the Preview Actions
And GraphicConverter Actions have the convert to Jpeg

Which ever ones you use, you can save them as an Automator app which will act like a Droplet.

hmm… thanks. that sounds like a much easier solution. There’s a big learning curve using Applescript.
i appreciate your assistance.

Hi

Try this:

on adding folder items to this_folder after receiving these_items
	repeat with theFile in these_items
		tell application "Adobe Photoshop CS2"
			activate
			open theFile
			set display dialogs to never
			set thisdoc to current document
			if (mode of thisdoc is not RGB) then
				change mode thisdoc to RGB
				set myoptions to {class:JPEG save options, quality:8}
				save thisdoc in (path to desktop folder) as JPEG with options myoptions appending lowercase extension without copying
				close current document saving no
			end if
		end tell
	end repeat
end adding folder items to

Works for me in PS CS2 had to have the file save out of the folder to stop the folder action
running again.

I do many image processing routines using a mixture of CS2 and Image Events. For whats its worth I have a folder in my home directory called “process folder”. If I create an action in photoshop I can save the image to the process folder. The applescript will call the images and evoke whatever CS2 action. It will then look inside the process folder and then process and move them to wherever. It means I don’t have to hard code CS2 for any action that does not need to parse a variable.
I know its not a folder action but might start a new idea…