how do i use Imagemagick from Applescript?

Hi,
I read on the imagemagick site its capabilities and it seems to be a lot faster than photoshop.
the only thing i need Imagemagick to do is

  1. open a file,
  2. convert it to RGB,
  3. scale it to 400px,
  4. save it in another file,
  5. scale it again to 120px,
  6. save it in another file,
  7. close it.

the thing is i’m a total n00b to imagemagick.
i’ve installed it with fink ( i think ), but i don’t know how to use it!!!
i’ve lookded in google for some kind of guide to imagemagick on applescript but i found nothing.
and since i’m in argentina, the books are not only VERY expensive, but also imposible to get.
can anyone tell me where can i find a guide to IM on applescript, or tell me how to use IM from applescript?

thank you very much.

PS: i’ve seen that there is something called “do shell”… but, as you can guess, i have NO idea what this is…:stuck_out_tongue:
PS: is there an Applescript Dictionary to Imagemagick? that would be great…

ImageMagick isn’t scriptable directly through AppleScript; You have to use do shell script.

ok…
so, i just can’t write ’

tell application "Imagemagick"

'… ok.
i have to use this “do shell script”
what is that?
how do i talk to imagemagick?
isn’t there a guide or anything for me to read?

thanks.

what you need to do it mess around in the terminal and learn the format for using imagemagick. Once you figure out the command out you enclose it in a do shell script… so for example say you wanted to take images and rotate them 90 degress and then save them with a modified name… you could do something like this…

repeat with aImage in theImages
	do shell script "convert " & aImage & " -rotate 90 rotated_" & aImage
end repeat

To clarify a bit… ImageMagick is a purely command line tool that has no direct AppleScript support. Using ImageMagick generally follows the format of

command souceFile [options]

So after playing around in the terminal and figuring out your commands you then can tell applescript to the command using the do shell script command.

So as another example say you have on your desktop a file called test.jpg and you want to make it into a RGB image named testRGB.jpg your command would be

convert ~/Desktop/test.jpg -colorspace RGB ~/Desktop/testRGB.jpg

So to put that into AppleScript would be as simple as

do shell script "convert ~/Desktop/test.jpg -colorspace RGB ~/Desktop/testRGB.jpg"

ok.
thanks!!!
i’ve been playing with Terminal for an hour and got eeverything i need to do. it works very smooth!!!
but…
when i try to make the script, i get an error :

sh: line1: convert: command not found

i don’t know why. but i belive that the paths are giving me some trouble…
is it posible to get the path with “/” instead of “:”???
any ideas?
thanks guys!!!

this is my test code ( i know it overwrites, it doesn’t matter for now…)

	try
		repeat with thisItem in these_items
			do shell script "convert " & thisItem & "-resize 400x400 " & "-colorspace RGB " & thisItem
		end repeat
	on error errormsg
		display dialog errormsg buttons {"ERROR"}
	end try

EDIT:
i fixed that, replacing the “do shell script” for “tell app “terminal””, but the thing its that it opens a window console for each file… in my final script, it would be used for thounsands of files… how do i tell it to run ‘at the back’ with no graphic interface?
but i still get the erro for the “:”… any ideas on that one???

THANKS!!!

The easiest ways are:

  1. drag the file to the Script Editor window.

  2. set f to POSIX path of (choose file)

  3. set f to POSIX path of ((path to desktop as text) & “TheFileName.jpg”)

i’m repeating this because I don’t think anyone is going to read it on my EDIT…
sorry for insisting…

every time the script runs, a windows of the terminal is opened.
how do i tell the Terminal to operate ‘silently’, without a window?
because when my script is finished, it will process thousands of files, wich would mean thousands of Terminal windows, wich will probably crash the system…

this is my test so far…

	try
		repeat with thisItem in these_items
			set itemPath to POSIX path of thisItem
			tell application "Terminal"
				do script "convert " & itemPath & " -resize 400x400 -colorspace RGB " & itemPath
			end tell
		end repeat
	on error errormsg
		display dialog errormsg buttons {"ERROR"}
	end try

THANKS to EVERYONE!!!

okay you want to go back to your original command, but you need to determine the location of convert and specify it so try this… if memoery serves me this is the location…

try
	repeat with thisItem in these_items
		set itemPath to POSIX path of thisItem
		do shell script "/usr/local/bin/convert " & itemPath & " -resize 400x400 -colorspace RGB " & itemPath
	end repeat
on error errormsg
	display dialog errormsg buttons {"ERROR"}
end try

well… i keep getting stuck with this script…
the convert thingie works great. and it doesn’t open a window for each file. but… let’s get a little more specific.
i have to make a preview, and a thumbnail from a hi ress file. so, i have a ‘process’ function in my script wich is supossed to create this files…
i have to convert the hi-ress, and save it as the preview, and then, open the preview, convert and save it as the thumbnail.
thats to optimize the script because the hi-ress files are of a minimum of 15mb…
so, i can’t do this because the Terminal doesn’t wait for the first convertion to end in order to start the second one…
(and if i don’t optimize the script, i would have the same file, opened two times, making it a lot slower…)

so, how can i tell the Terminal to wait until it finishes, and then to continue with the other?

thanks.

hi mem,

i believe a subroutine call would help you here:


try
	repeat with thisItem in these_items
		set itemPath to POSIX path of thisItem
		myProcess(itemPath)
	end repeat
on error errormsg
	display dialog errormsg buttons {"ERROR"}
end try

on myProcess(itemPath)
	do shell script "/usr/local/bin/convert " & itemPath & " -resize 400x400 -colorspace RGB " & itemPath
	return 0
end myProcess

that way AppleScript will wait for ‘myProcess’ to return before moving to the next command. i believe this is correct, but i don’t have ImageMagick to test.

cheers.

ok. i’m almost there… :smiley:

i’ve finished the script. it runs silently and very smooth. and, best of all, very fast.
it waits when it needs to, it renames the files if they are already in use, it keeps a log of errors, and does the resizing beautifully, but…

i’m on MacOSx 10.4.7, with the latest versions of Imagemagick and Ghostscript (acording to fink).
when i was messing around in the Terminal, trying to understand how to tell Imagemagick to resize and everything, it could process any kind of file i wanted. but when applescript does de “do shell script” it cannot process EPS (nor illustrator, nor photoshop)…
i typed the exact string in the script in the Terminal, and the Terminal processes the images without a problem.

-this is the error message:

sh: line 1: gs: command not found
sh: line 1: gs: command not found
convert: no decode delegate for this image format `/files/EPSi.eps’.

-this is the processing part of the code:

on process_files(source_file, file_name, pwg_folder, tn_folder)
	try
		set the pwg_path to ((pwg_folder as string) & file_name) as string
		set the tn_path to ((tn_folder as string) & "tn_" & file_name) as string
		set original to POSIX path of source_file
		set pwg to POSIX path of pwg_path
		set tn to POSIX path of tn_path
		with timeout of 180 seconds
			do shell script "/sw/bin/convert " & original & " -resize 400x400 -colorspace RGB " & pwg & " && /sw/bin/convert " & pwg & " -resize 120x120 " & tn
		end timeout
	on error the error_message number the error_number
		set the error_text to "Error: " & the error_number & ". " & the error_message
		my write_error_log(the error_text)
	end try
	return 0
end process_files

i belive that for some odd reason, applescript isn’t using the ‘codec’ from Ghostscript (thats the GS?), so it cannot read nor write EPSs… but why?
and, more important, how do i make applescript to read and write EPSs???

this one really left me stupid…

thanks to everyone, you’ve been too much helpfull.

membranatus I am working on a similar Applescript as yourself, and although I have managed to get my script to work, my shell script will not run unless I use the following code

set MyPicConvert to “/usr/bin/imagemagick/bin/convert -charcoal 1 /Users/quentinking/Pictures/Test.jpg /Users/quentinking/Pictures/Result.jpg”
do shell script “MAGICK_HOME="/usr/bin/imagemagick";export PATH=$MAGICK_HOME/bin:$PATH; export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib";”& MyPicConvert

As you will notice I am having to add

"MAGICK_HOME="/usr/bin/imagemagick";export PATH=$MAGICK_HOME/bin:$PATH; export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib";

before my convert command.

But if I run the convert command via Terminal it works without the added Imagemagick path setup. Does anyone know why? Is it something to do with my Imagemagic installation?
QK thanks in advance

Model: Macbook pro
AppleScript: Applescript via Filemaker
Browser: Safari 533.20.27
Operating System: Mac OS X (10.6)

running the command from it’s explicit directory fixes the problem.

to find the path to use:

in Finder go to menu:
Go/Go to Folder…

type in:
/usr/local/

{depending of your brew or cellar install the dir will be different, but similar - I got Cellar for this example}

  • go into “Cellar” folder
  • go into “imagemagick” folder
  • go into version folder (I have 6.9.1-1)
  • go into “bin” folder

if you drag the command file in Script Editor → choose import as “Paste POSIX Path(s)” you will get the dir to use.

ex:
/usr/local/Cellar/imagemagick/6.9.1-1/bin/convert {for “convert” function}
/usr/local/Cellar/imagemagick/6.9.1-1/bin/identify {for “identify” function}

Applescript might look like this:

do shell script "/usr/local/Cellar/imagemagick/6.9.1-1/bin/convert -charcoal 1 " & fileToProcess & " " & SaveAs

Option B:

Buy an inexpensive Mac graphics program which is applescriptable and had documentation for applescripting which you can understand.

examples: Acorn, GraphicConverter.