Monday, December 9, 2019

#1 2006-08-24 09:08:56 am

membranatus
Member
Registered: 2006-08-02
Posts: 116

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...:P
PS: is there an Applescript Dictionary to Imagemagick? that would be great...

Offline

 

#2 2006-08-24 09:16:58 am

Bruce Phillips
Administrator
Registered: 2004-07-16
Posts: 2649

Re: how do i use Imagemagick from Applescript?

membranatus wrote:

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`.

Offline

 

#3 2006-08-24 09:25:57 am

membranatus
Member
Registered: 2006-08-02
Posts: 116

Re: how do i use Imagemagick from Applescript?

ok...
so, i just can't write '

Applescript:

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.


Filed under: Imagemagick

Offline

 

#4 2006-08-24 09:49:32 am

James Nierodzik
Member
From:: Chicago
Registered: 2005-09-04
Posts: 1151

Re: how do i use Imagemagick from Applescript?

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..

Applescript:

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

Applescript:

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


Formerly known as myndcraft
(S+C) x (B+F)/T = V

Offline

 

#5 2006-08-24 01:41:44 pm

membranatus
Member
Registered: 2006-08-02
Posts: 116

Re: how do i use Imagemagick from Applescript?

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...)

Applescript:

   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!!!

Last edited by membranatus (2006-08-24 01:56:30 pm)

Offline

 

#6 2006-08-24 01:54:00 pm

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4666

Re: how do i use Imagemagick from Applescript?

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")


Mac mini running 10.14.6, 2011 27" iMac as display.

Offline

 

#7 2006-08-24 02:21:29 pm

membranatus
Member
Registered: 2006-08-02
Posts: 116

Re: how do i use Imagemagick from Applescript?

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...

Applescript:

   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!!!!


Filed under: terminal

Offline

 

#8 2006-08-24 02:28:15 pm

James Nierodzik
Member
From:: Chicago
Registered: 2005-09-04
Posts: 1151

Re: how do i use Imagemagick from Applescript?

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..

Applescript:

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


Formerly known as myndcraft
(S+C) x (B+F)/T = V

Offline

 

#9 2006-08-24 03:59:04 pm

membranatus
Member
Registered: 2006-08-02
Posts: 116

Re: how do i use Imagemagick from Applescript?

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.

Offline

 

#10 2006-08-24 04:12:19 pm

waltr
Member
Registered: 2005-09-13
Posts: 317

Re: how do i use Imagemagick from Applescript?

hi mem,

i believe a subroutine call would help you here:

Applescript:


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.


I use Google or any search engine to find out things I don't know!

Offline

 

#11 2006-08-25 07:59:35 am

membranatus
Member
Registered: 2006-08-02
Posts: 116

Re: how do i use Imagemagick from Applescript?

ok. i'm almost there... big_smile

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:

Applescript:

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.

Offline

 

#12 2011-06-05 03:23:52 am

Quentinking
Member
Registered: 2006-08-18
Posts: 2

Re: how do i use Imagemagick from Applescript?

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)

Offline

 

#13 2016-05-07 07:04:49 pm

acadie
Member
Registered: 2008-12-07
Posts: 2

Re: how do i use Imagemagick from Applescript?

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:

Applescript:

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


Filed under: Imagemagick

Offline

 

#14 2016-05-14 07:54:38 am

vince
Member
Registered: 2002-12-14
Posts: 64

Re: how do i use Imagemagick from Applescript?

Option B: 

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

examples:  Acorn, GraphicConverter.


vince angeloni

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)