Saturday, March 14, 2009

Revisiting the Juice Box

During my Action Research project for graduate school I got my hands on a lot of six Mattel Juice Box media players. I used these handy little devices, which run an embedded version of uClinux, with the Tech Club I worked with. I loaded photos they took using digital cameras onto them. I also loaded the podcasts they created onto them: students could plug in headphones and listen to each of the episodes they produced or use the built-in speakers and share it with a group.

A sizable hacker collective has managed to get custom code to boot from the thing and are experimenting with connecting it to a Canon CueCat to make a homebrew bar code reader. I'm a nerd but I cannot yet solder, so I haven't taken that path. However, back when I first got the device I became a bit obsessed with being able to create an OS X Folder Action so the laptops I used with Tech Club could automagically convert the jpegs the students exported from iPhoto into the proprietary, 12-bit .jbp graphic file, 240 X 160. There exists a PC version of software that came with the "mp3 Kit" to convert jpegs into .jbp format, but that doesn't do anything for those of us with Macs. Fortunately, ImageMagick can convert the jpegs to rgb format, and a perl script exists to convert the 24-bit rgb file to a 12-bit pixel file.

I finally got the AppleScript put together to perform all these conversions and to make it as simple for the students as dropping their images onto a folder in the Dock and copying the files from the "Converted" folder onto an SD memory card. I felt the automation minimized the technical steps of the process so the students would have an easier time seeing their images on the Juice Box, which they enjoyed because they could hand it around to their friends and share the images they created.

This week I saw an Instructables where somebody took apart a Juice Box and assembled it into a digital picture frame, another project I used with the Tech Club to display their digital work. I got to thinking about the Folder Action and decided to get it going on my PowerBook again, since I had upgraded to Tiger. It turns out the script did not work!

First, I found out you need to store Folder Actions in /Library/Scripts/Folder Actions Scripts or ~/Library/Scripts/Folder Actions Scripts if you want the system to run them. Once I figured that out and successfully attached the Folder Action Script to my conversion folder, I got an error about an ImageMagick library not being loaded. I was finally able to find a page that helped me solve this issue.

It turns out that in OS X 10.5.6 in order for the AppleScript to successfully use the "do shell script" to export a variable to define the location of the library you need to make that the first command in the script. Here is my corrected script:

-- OS X AppleScript Folder Action
-- Converts various graphic formats to the .jbp format for use with Mattel's Juice Box
-- 2006, 2009 Josh Burker

property done_foldername : "Converted"
property originals_foldername : "Original Images"
property newimage_extension : "gif"
-- the list of file types which will be processed
-- eg: {"PICT", "JPEG", "TIFF", "GIFf"}
property type_list : {"JPEG", "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 : {"jpg", "jpeg", "GIF", "png", "pict", "pct", "rgb", "jbp"}

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
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"
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
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
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
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
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
-- 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)
scale this_image to size 240
-- scale the image
crop this_image to dimensions {240, 160}
-- save the image
save this_image as GIF in file target_path with icon
close this_image
end tell
end timeout

set target_path to POSIX path of target_path

-- display dialog POSIX path of target_path

-- load the DYLD_LIBRARY_PATH first

do shell script "export DYLD_LIBRARY_PATH='/Applications/ImageMagick-6.5.0/lib'; export MAGICK_HOME='/Applications/ImageMagick-6.5.0'; /Applications/ImageMagick-6.5.0/bin/convert '" & target_path & "' 'rgb:" & target_path & ".rgb'"

do shell script "/usr/local/bin/ '" & target_path & ".rgb' '" & target_path & ".jbp'"

on error error_message
tell application "Finder"
display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
end tell
end try

end process_item

Now I have it working again I loaded up an SD card with different images than I already had loaded and watched the Juice Box for a bit. It got to be a little frustrating troubleshooting the AppleScript and the ImageMagic shell script contained within. I wasn't sure if it were all the apostrophes and quotation marks that the parent helped me figure out or if it had to do with my installation of the ImageMagick binaries. I added the exports to my bash profile but that did not help, either. Finally, I decided to load the library first in the shell script and it all worked again. Whew!


cs said...

Thanks for sharing a lot of great information!

I discovered the Juice Box a few weeks ago and am really impressed with the platform. I just got an "mp3 kit" and am on the hunt for SD cards - do you have any comments on the "best" brand and/or sizes?


Josh Burker said...

I'm glad you found it informative.

If memory serves, the Juice Box won't recognize cards bigger than 512 MB. I use 64 MB cards with these Juice Boxes, and they could hold quite a few images and podcasts in that space. I don't think the brand matters at all: whatever is least expensive.

Have fun!

cs said...

I grabbed a few real SanDisk 512mb cards and tried a few things. I'm posting here in case it helps you later, or anyone else who happens to find this via Bing/Google/etc:

I am using Vista x86.

Formatted as FAT32. Juice Box acts like the "default chip" is installed and just plays the built-in video demo.

Formatted as FAT16 with default allocation block size. There is evidently a root-directory limit of 125 entries. Copied 125 mp3 and jbp files, things worked fine. Total of about 220mb of files.

Formatted as FAT16 with 16k allocation block size. Juice Box hung.

Took a new SD card from the package and copied 140 files to the card. Juice Box hung.

I'm still trying to understand how the JuiceBox uses the mp3 files, it looks like for some files it uses the file name and for others it uses the embedded Song Name. I'm still working through this, but it's not obvious (or I need more sleep).


Josh Burker said...

Awesome: thanks for all the information! Happy hacking!