Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to add an ImageMagick command to a script?

Hi

I have successfully added an ImageMagick command in a script by specifying the convertpath:

set convertPath to "Macintosh HD:opt:local:bin:convert"


and then including the command:

-- setup the image magick command
set convertCommand to quoted form of POSIX path of convertPath & " -define jpeg:size=840x500 -thumbnail '420x250>' -background '#24221F' -gravity center -extent 420x250 -quality 95 "


but, now I have another command, which works from terminal, but which is in a different format:

convert thumbnail.gif 
( +clone -alpha extract
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0'
( +clone -flip ) -compose Multiply -composite
( +clone -flop ) -compose Multiply -composite
) -alpha off -compose CopyOpacity -composite -compose over -background red -flatten thumbnailredcorners.gif


when I try to enter this command in my script in place of the command that works I get a syntax error:

Expected “"” but found unknown token.

So, I am wondering how I can incorporate the ImageMagick command in my script and would be grateful for any help,

Thanks,

Nick.

Macbook Pro, Mac OS X (10.6.6), 2.66 GHz Intel Core i7 8GB RAM

Posted on Feb 19, 2011 7:31 AM

Reply
Question marked as Best reply

Posted on Feb 19, 2011 8:20 AM

The backslash character in AppleScript is used to escape special characters (such as a backslash), so to use them in a text string you need to escape those as well, for example
set anotherCommand to "convert thumbnail.gif \
\( +clone -alpha extract \
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \
\( +clone -flip \) -compose Multiply -composite \
\( +clone -flop \) -compose Multiply -composite \
\) -alpha off -compose CopyOpacity -composite -compose over -background red -flatten thumbnailredcorners.gif"
7 replies
Question marked as Best reply

Feb 19, 2011 8:20 AM in response to nick_harambee

The backslash character in AppleScript is used to escape special characters (such as a backslash), so to use them in a text string you need to escape those as well, for example
set anotherCommand to "convert thumbnail.gif \
\( +clone -alpha extract \
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \
\( +clone -flip \) -compose Multiply -composite \
\( +clone -flop \) -compose Multiply -composite \
\) -alpha off -compose CopyOpacity -composite -compose over -background red -flatten thumbnailredcorners.gif"

Feb 19, 2011 11:16 AM in response to nick_harambee

Hello

Also you may as well specify full path of the command as follows :

set sh to "/opt/local/bin/convert thumbnail.gif \
\( +clone -alpha extract \
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \
\( +clone -flip \) -compose Multiply -composite \
\( +clone -flop \) -compose Multiply -composite \
\) -alpha off -compose CopyOpacity -composite -compose over -background red -flatten thumbnailredcorners.gif"
do shell script sh

Cheers,
H

P.S. You can easily get properly escaped string for AppleScript by the following procedure.

1) Copy the source string into clipboard. E.g. copy the following chunk of text:

convert thumbnail.gif
( +clone -alpha extract
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0'
( +clone -flip ) -compose Multiply -composite
( +clone -flop ) -compose Multiply -composite
) -alpha off -compose CopyOpacity -composite -compose over -background red -flatten thumbnailredcorners.gif


2) Run the script below:

return the clipboard


3) And the desired text is in the result window:

"convert thumbnail.gif \
\( +clone -alpha extract \
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \
\( +clone -flip \) -compose Multiply -composite \
\( +clone -flop \) -compose Multiply -composite \
\) -alpha off -compose CopyOpacity -composite -compose over -background red -flatten thumbnailredcorners.gif"


4) Copy and paste it where you want. Done.

Feb 19, 2011 12:03 PM in response to Hiroto

Thanks for this. I have now got past the initial error, but am getting another error:

convert: unable to open image `thumbnail.gif': No such file or directory @ error/blob.c/OpenBlob/2584.
convert: image sequence is required `+clone' @ error/convert.c/ConvertImageCommand/914.

Clearly this is related to the thumbnail.gif in the convert command:

" thumbnail.gif \
\( +clone -alpha extract \
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \
\( +clone -flip \) -compose Multiply -composite \
\( +clone -flop \) -compose Multiply -composite \
\) -alpha off -compose CopyOpacity -composite -compose over -background red -flatten thumbnailredcorners.gif"


as I am asking the script to cycle through a number of images and save them in a particular format, I probably don't need the thumbnail.gif or thumbnail redcorners.gif in the command.

Here is the full script as it currently stands:

set convertPath to "Macintosh HD:opt:local:bin:convert"
set outputFolder to (path to desktop folder as text) & "outputImages:"
tell application "iTunes"
set theFiles to location of every track of playlist "gigs"
set sel to every track of playlist "gigs"
set this_track to first track of playlist "gigs"
set artistName to this_track's artist
end tell
-- setup the image magick command
set convertCommand to quoted form of POSIX path of convertPath & " thumbnail.gif \
\( +clone -alpha extract \
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \
\( +clone -flip \) -compose Multiply -composite \
\( +clone -flop \) -compose Multiply -composite \
\) -alpha off -compose CopyOpacity -composite -compose over -background red -flatten thumbnailredcorners.gif"
set fileCounter to 0
set convertedFileList to {}
set the theImageList to {}
repeat with aFile in theFiles
tell application "Finder"
set thisFolder to container of aFile
set theImages to (files of entire contents of thisFolder whose name ends with ".jpg") as alias list
set theImageList to theImageList & theImages
end tell

-- convert the images

repeat with i from 1 to count of theImages
set thisImage to (item i of theImages) as text
if thisImage is not in convertedFileList then
set end of convertedFileList to thisImage
set fileCounter to fileCounter + 1
set outPath to outputFolder & fileCounter & ".jpg"

do shell script convertCommand & quoted form of POSIX path of thisImage & space & quoted form of POSIX path of outPath
end if
end repeat

end repeat
set xx to ""
set trackcount to 1
repeat with i from 1 to ((fileCounter) - 1)
set xy to " <img src="assets/" & artistName & "/" & trackcount & ".jpg">
"
set xx to (xx & xy & return)
set trackcount to (trackcount + 1)
end repeat
set xy to " <img src="assets/" & artistName & "/" & trackcount & ".jpg">"
set xx to (xx & xy & return)
set zz to ""
set trackcount2 to 1
repeat with i from 1 to count of sel
set zy to " {name:"" & name of item i of sel & "",mp3:"assets/" & artistName & "/" & trackcount2 & ".mp3",ogg:"assets/" & artistName & "/" & trackcount2 & ".ogg"},"
set zz to (zz & zy & return)
set trackcount2 to (trackcount2 + 1)
end repeat
tell application "TextEdit"
activate
make new document
set theDate to current date
set text of document 1 to xx & return & zz as text
end tell

Feb 19, 2011 3:09 PM in response to nick_harambee

Hello

I don't know ImageMagick. And worse, I don't know what you're trying to do.
I can only guess.

Perhaps the given chunk of code consists of :

convert <input_file> <options> <output_file>


If so, you'd need to build the command in such a way as follows :

set convert_ to "/opt/local/bin/convert"
set options_ to "\( +clone -alpha extract \
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \
\( +clone -flip \) -compose Multiply -composite \
\( +clone -flop \) -compose Multiply -composite \
\) -alpha off -compose CopyOpacity -composite -compose over -background red -flatten"
set infile_ to "/posix/path/of/input/file.jpg"'s quoted form
set outfile_ to "/posix/path/of/output/file.jpg"'s quoted form
set command_ to convert_ & space & infile_ & space & options_ & space & outfile_
do shell script command_


Hope this may be of some help,
H

May 12, 2016 10:29 AM in response to Hiroto

Hi thanks for your helpful work


I´m in similar situation and can´t pass to next step because i get error messages everytime

(f.ex. : Finder error: convert: unable to open image `*.jpg': No such file or directory @ error/blob.c/OpenBlob/2702.

convert: no images defined `{name:collection}.pdf' @ error/convert.c/ConvertImageCommand/3252. )


The ImageMagick comand that works fine in terminal is so simple as: convert *.jpg filename.pdf



set baseFolder to choose folder

set{ATID, AppleScript'stext item delimiters} to {"", "-"}

tell application "Finder"

set theFiles to every file of entire contents of baseFolder as alias list


sets_c to text returned of (display dialog "Please enter the STARTING character:" default answer "")

sete_c to text returned of (display dialog "Please enter the ENDING character:" default answer "")

repeatwithaFileintheFiles

set fileName to name of aFile

set collection to text s_c through e_c of fileName

set cont to container of aFile as string

if folder (cont & collection) exists then

else


makenewfolderatcontwith properties {name:collection}

end if

set convert_ to "/opt/local/bin/convert"

-- set convertPath to "iMac24_HD:opt:local:bin:convert"

-- set convertCommand to quoted form of POSIX path of convertPath & space & "*.jpg" & {collection} & ".pdf "

-- do shell scriptconvertCommand

set infile_ to "*.jpg"

set outfile_ to "{collection}" & ".pdf"

set command_ to convert_ & space & infile_ & space & outfile_

do shell scriptcommand_

movefileaFileto (cont & collection & ":")

endrepeat

endtell

set AppleScript'stext item delimiters to ATID


Any help?


Thank you very very much

May 12, 2016 11:00 PM in response to nagahi

Simple, really - your convert command can't find the image(s) in question.


That's because your convert command looks like:


/opt/local/bin/convert *.jpg {collection}.pdf

(although I suspect you want something different as the output filename, but I'll go with this for now..


Specifically, convert is told to take all the *.jpg files in the current directory. At no point in your shell command do you link this to the baseFolder. As a result, convert is looking in the default directory (typically / ), and there are no images there.


Instead, you need to do something like:


set infile_ to quoted form of (POSIX path of cont & "*.jpg")


Now your infile_ variable will include the path to cont (assuming that's the appropriate path).

May 14, 2016 7:05 AM in response to nagahi

Hello


I'm not sure I understand your intention. Are you trying to do something like the following?


Given input state:



/path/to/base/d1/d11/abc1.jpg /path/to/base/d1/d11/abc2.jpg /path/to/base/d1/d11/abc3.jpg /path/to/base/d2/def1.jpg /path/to/base/d2/def2.jpg /path/to/base/d2/ghi1.jpg /path/to/base/d2/ghi2.jpg




and {start index, end index} of text range of file name for collection = {1, 3};



the expected output state:



/path/to/base/d1/d11/abc.pdf /path/to/base/d1/d11/abc/abc1.jpg /path/to/base/d1/d11/abc/abc2.jpg /path/to/base/d1/d11/abc/abc3.jpg /path/to/base/d2/def.pdf /path/to/base/d2/def/def1.jpg /path/to/base/d2/def/def2.jpg /path/to/base/d2/ghi.pdf /path/to/base/d2/ghi/ghi1.jpg /path/to/base/d2/ghi/ghi2.jpg




where:



{ /path/to/base/d1/d11/abc1.jpg /path/to/base/d1/d11/abc2.jpg /path/to/base/d1/d11/abc3.jpg } => /path/to/base/d1/d11/abc.pdf { /path/to/base/d2/def1.jpg /path/to/base/d2/def2.jpg } => /path/to/base/d2/def.pdf { /path/to/base/d2/ghi1.jpg /path/to/base/d2/ghi2.jpg } => /path/to/base/d2/ghi.pdf




?


If so, your script will NOT do it even after correcting the commands so as to set appropriate working directory.


Please start new thread for your question with detailed descriptions on your input and output states, for it is very hard to guess the original intention from incomplete code.


Regards,

H

How to add an ImageMagick command to a script?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.