Scripting repeat argument to convert files using batch in GraphicConve

Hi

I am wondering if someone could help with an applescript I am working on to convert all JPEG files in the album folders of the currently selected tracks in iTunes. The script successfully selects all the correct files (I tested this by just trying to open the files with GraphicConverter rather than converting them).

The trouble I am having is with the GraphicConverter part of the script. I would like to use the “convert file using batch x to folder y” argument to convert all the files returned using a batch I have created (called “640square”) and save the resulting files to a folder (called “temp”) on my desktop, but I am not sure how to set up the repeat argument to apply the conversion to each file.

The script as it stands is as follows:

property Batch_file : "640square"
property Prog_Folder : "temp"

tell application "iTunes"
	set sel to selection -- every track of playlist "to comments"
	repeat with this_track in sel
		
		set comp to this_track's composer
		set alb to this_track's album
		
		tell application "Finder"
			set myfolder to folder alb of folder comp of folder "Music"
			set myfiles to (every file of myfolder whose file type is "JPEG") as alias list
		end tell
		
		set progdest to ((path to desktop folder) as string) & Prog_Folder
		set progsbatch to ((path to application support folder from user domain) as string) & "GraphicConverter:Actions:" & Batch_file
		
		tell application "GraphicConverter"
			activate
			--repeat with.....???				
			convert file using batch progsbatch to folder progdest
			--end repeat
		end tell
		
	end repeat
end tell

Thanks

Nick

Whilst ideally I would like to be able to run GraphicConverter batches on a set of files using AppleScript, if there is another method of scripting what I am trying to do for this project then it would be good to know about it.

Essentially all I want to do is to convert the JPEG files returned by the script to maximum width of 420px and maximum height of 250px, whilst maintaining proportions, and have them saved in a particular folder as JPEGs (i.e. not changing the original files, just saving a new version), ideally with the ability to set the quality of the JPEGs as well.

I would use image events. Try this. Note you can change the compression level.

set batchFolder to (path to desktop folder as text) & "temp:"
set maxWidth to 420
set maxHeight to 250

tell application "iTunes"
	set theFiles to location of selection -- file paths for the selected files
end tell

repeat with aFile in theFiles
	tell application "Finder"
		set thisFolder to container of aFile
		set theImages to (files of entire contents of thisFolder whose file type is "JPEG") as alias list
	end tell
	
	if theImages is not {} then
		tell application "Image Events"
			launch
			repeat with anImage in theImages
				set thisImage to open anImage
				set {theWidth, theHeight} to dimensions of thisImage
				
				set theFactor to maxHeight / theHeight
				if (theFactor * theWidth) is greater than maxWidth then set theFactor to maxWidth / theWidth
				
				scale thisImage by factor theFactor
				save thisImage in folder batchFolder as JPEG with compression level high -- high/low/medium
				close thisImage
			end repeat
			quit
		end tell
	end if
end repeat

Thanks, that works fine. I just had to change the line “save theImage in folder batchFolder as JPEG with compression level high” to “save thisImage in folder batchFolder as JPEG with compression level high” to get this working.

I have tried adding a count argument to rename the resulting files as 1.jpg, 2.jpg, etc, but can’t get this working. Would you be able to help out with this?

Also, since my last post I have realised that all the images need to be the same size (they are being generated for a slideshow script that requires all files to be the same size). GraphicConverter has a batch feature called ‘Bring to Size’, that makes all files the same size, centering the image along the horizontal or vertical axis and filling in the space with a custom colour, as you can see in the following two examples:

http://emberapp.com/nickharambee/images/2/sizes/m.png
http://emberapp.com/nickharambee/images/5/sizes/m.png

Is there a similar way of scripting this with image events?

If not I am back to trying to get scripting working with GraphicConverter. Here is a folder action script that was posted on this forum some while back. I can’t get it to work (and wouldn’t want it as a folder action, but rather part of an ordinary script), but it gives an idea of what I’m aiming at:

property Batch_file : "Traitement"
property Prog_Folder : "Progs tr"
on adding folder items to this_folder after receiving these_items
   set progdest to ((path to desktop folder) as string) & Prog_Folder
   set progsbatch to ((path to application support folder from user domain) as string) & "GraphicConverter:Actions:" & Batch_file
   tell application "GraphicConverter"
       repeat with this_item in these_items
           convert file (this_item as file specification) using batch (progsbatch as file specification) to folder progdest
       end repeat
   end tell
end adding folder items to

Nick

Yep, I noticed that mistake too and fixed it a little while ago in my script.

There’s no way to set the output file name from the image events save command. As such you might want to do the following. Here’s a general outline of what would work…

  1. from the image events save command save the file to a different folder first
  2. using the Finder, get files of that folder. The first item should be the newly saved file since that folder was initially empty
  3. using the Finder, rename item 1 of the returned files (setup a counter in the repeat loop so you know the number to rename it to)
  4. move the renamed file to the standard output folder

Image events can’t do that as far as I know. I tried using the “convert file” command of GraphicConverter and couldn’t get it to work either. I think the “file specification” file type has been mostly deprecated in applescript for a long time. I think in 10.6 Apple finally got rid of it. Since GraphicConverter requires this and 10.6 doesn’t have it then it seems you won’t be able to get that command working. I would sugest 1) write the author of GraphicConverter and ask if he can fix that command to use something other than file specifications and 2) try a different (more powerful) image tool to perform your tasks. One such tool would be ImageMagick which is a freely available command line tool.

Thanks very much. I have decided to go down the ImageMagick route. I have it installed, and got it working from terminal. I have been using the following command in terminal to do what I want (mogrify instead of convert to overwrite the current files, presumably I will need to use convert in the applescript as I don’t want to do any overwriting)

  mogrify -define jpeg:size=840x500 -thumbnail '420x250>' \
          -background '#24221F' -gravity center -extent 420x250 -quality 90 *.jpg

Now I just need to find a way of including it in your script so that it processes the JPEG files returned from iTunes and saves the new files in a specified directory. I am presuming that I could also specify the output directory with ImageMagick and perhaps also rename the files as 1.jpg, 2.jpg etc,

Would you be able to help with the ImageMagick part of the script?

Nick

Hey, that command you put together works perfect with “convert”. That made it easy. Here’s the script. I didn’t set it up to work with iTunes. I just ran a quick test on a folder of images I had. In any case you just need to get a list of alias-paths to the images you want to convert… whether it be from iTunes or wherever.

So setup the top 2 variables and get your list of alias’s, then everything below “-- setup the image magick commands” in the script should just work. Notice the top 2 variables must end in a colon because they’re paths to folders, and your output images will be numbered as 001.jpg, 002.jpg etc. I hope that helps! :cool:

set imagemagickFolder to (path to home folder as text) & "UnixBins:ImageMagick-6.6.3:"
set outputFolder to (path to desktop folder as text) & "outputImages:"

-- get the input images as a list of alias's
set inputFolder to (path to desktop folder as text) & "inputImages"
tell application "Finder"
	set inputImages to (files of folder inputFolder) as alias list
end tell

-- setup the image magick commands
set convertPath to imagemagickFolder & "bin:convert"
set dyldPath to imagemagickFolder & "lib:"
set convertCommand to quoted form of POSIX path of convertPath & " -define jpeg:size=840x500 -thumbnail '420x250>' -background '#24221F' -gravity center -extent 420x250 -quality 90 "
set dyldCommand to "export DYLD_LIBRARY_PATH=" & quoted form of POSIX path of dyldPath & ";"

-- convert the images
repeat with i from 1 to count of inputImages
	set threeDigitNumber to text -3 thru -1 of ("000" & (i as text))
	set outPath to outputFolder & threeDigitNumber & ".jpg"
	
	do shell script dyldCommand & convertCommand & quoted form of POSIX path of (item i of inputImages) & space & quoted form of POSIX path of outPath
end repeat

Thanks. I worked out where macports installed ImageMagick, so changed the variable to:

set imagemagickFolder to “Macintosh HD:opt:local:”

I kept the input and output folders the same as yours, but when I ran the script I get the following error for the do shell script command:

error “dyld: Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /opt/local/lib//libjpeg.8.dylib
in /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/ImageIO
sh: line 1: 7821 Trace/BPT trap ‘/opt/local/bin/convert’ -define jpeg:size=840x500 -thumbnail ‘420x250>’ -background ‘#24221F’ -gravity center -extent 420x250 -quality 90 ‘/Users/nick/Desktop/inputImages/4 congo_prophet 07_collection.jpg’ ‘/Users/nick/Desktop/outputImages/001.jpg’” number 133

All the necessary files seem to be in the right place, i.e. in the bin and lib folders. Any ideas?

Nick

I didn’t install it with MacPorts. I installed it by hand. As such I had to tell convert where its libraries were by including “export DYLD_LIBRARY_PATH=” in the command. I’m not sure, but you probably don’t have to do that. As such try this…

set convertPath to "Macintosh HD:opt:local:bin:convert"
set outputFolder to (path to desktop folder as text) & "outputImages:"

-- get the input images as a list of alias's
set inputFolder to (path to desktop folder as text) & "inputImages"
tell application "Finder"
	set inputImages to (files of folder inputFolder) as alias list
end tell

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

-- convert the images
repeat with i from 1 to count of inputImages
	set threeDigitNumber to text -3 thru -1 of ("000" & (i as text))
	set outPath to outputFolder & threeDigitNumber & ".jpg"
	
	do shell script convertCommand & quoted form of POSIX path of (item i of inputImages) & space & quoted form of POSIX path of outPath
end repeat

Thanks. The conversion works fine now. However, I now need to work out how to set the count so that files from different album folders aren’t overwritten with the repeat argument. Here’s the script as it 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 selection -- file paths for the selected files
end tell

repeat with aFile in theFiles
	tell application "Finder"
		set thisFolder to container of aFile
		set theImages to (files of entire contents of thisFolder whose file type is "JPEG") as alias list
	end tell
	
	-- 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 90 "
	
	-- convert the images
	repeat with i from 1 to count of theImages
		set threeDigitNumber to text -3 thru -1 of ("000" & (i as text))
		set outPath to outputFolder & threeDigitNumber & ".jpg"
		
		do shell script convertCommand & quoted form of POSIX path of (item i of theImages) & space & quoted form of POSIX path of outPath
	end repeat
end repeat

Glad that worked. I added a fileCounter variable to use for the file names…

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 selection -- file paths for the selected files
end tell

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

set fileCounter to 0
repeat with aFile in theFiles
	tell application "Finder"
		set thisFolder to container of aFile
		set theImages to (files of entire contents of thisFolder whose file type is "JPEG") as alias list
	end tell
	
	-- convert the images
	repeat with i from 1 to count of theImages
		set fileCounter to fileCounter + 1
		set threeDigitNumber to text -3 thru -1 of ("000" & (fileCounter as text))
		set outPath to outputFolder & threeDigitNumber & ".jpg"
		
		do shell script convertCommand & quoted form of POSIX path of (item i of theImages) & space & quoted form of POSIX path of outPath
	end repeat
end repeat

Well, we are getting closer!

The renumbering works, but there are two outstanding issues:

(i) some JPEG files in the album folders are missed. I’m not sure why this is. I will look into this some more.

(ii) where there are multiple items in my selection in iTunes from the same album folder the script is generating multiple copies of the artwork found in this folder where I only want one version of each image from each album folder. I am hoping you can help with this.

That’s easy enough. You just keep track of the image paths that have been converted and make sure the same one isn’t converted again.

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 selection -- file paths for the selected files
end tell

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

set fileCounter to 0
set convertedFileList 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 file type is "JPEG") as alias list
	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 threeDigitNumber to text -3 thru -1 of ("000" & (fileCounter as text))
			set outPath to outputFolder & threeDigitNumber & ".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

many thanks Hank aka regulus6633

The script now works just as I want it to. For some reason the argument:

set theImages to (files of entire contents of thisFolder whose file type is “JPEG”) as alias list

wasn’t picking up all the JPEGs in my album folders, but when I changed it to this line:

set theImages to (files of entire contents of thisFolder whose name ends with “.jpg”) as alias list

all files are processed.

I’m not sure why this is, but its working, so that’s the main thing.

I appreciate you taking the time to help me with this.

Enjoy the day,

Nick

Glad I could help Nick. I never mind helping someone who is putting in the effort them self. And once again you solved the “jpg” problem without any assistance, so good job and good luck.