SC, Image Files and MPI

Click on an image to open a fullsized version in another window.

Add Folder... Button
Add Folder... Button 166 by 236

Extend Project installing a external
Extend Project installing a external 509 by 231

Adding a Folder of Files

It might be nice to add a folder full of images to our field.
We can drag another button from the MPI tools palette and use the PI to name it, Add Folder...
Give it a script:
Edit the script of the button (Command-E or Script from the Edit... popUp on the PI)

on mouseUp
answer folder "Choose a folder of images:"
if it is "" then exit mouseUp
put it into tfolder
end mouseUp

now tfolder contains the path to the folder for example: HD X:Users:johnjohn:Pictures:notinmy:
To get a list of files that are in the folder we can use the dir external from Xtend, copy it to the images.proj from Xtend.
Open Xtend from mpi's or the rte's utilities menu.

The Dir external is pretty powerful and can return lots of information about the files returned, unfortunately in OSX a file may be identified by Type or extension, a jpg file may have the type JPEG or it might just have a .jpg extension.
So what I am going to do is to get all the files in the folder and its subfolders
put dir(tfolder,false,true) into tfilelist

tfilelist will now contain something like this:
HD X:Users:johnjohn:Pictures:notinmy:DSCF0002.JPG
HD X:Users:johnjohn:Pictures:notinmy:a word doc.doc
HD X:Users:johnjohn:Pictures:notinmy:anothefile.txt
HD X:Users:johnjohn:Pictures:notinmy:DSCF0005.JPG
HD X:Users:johnjohn:Pictures:notinmy:my sounds.cwk
HD X:Users:johnjohn:Pictures:notinmy:DSCF0007.JPG
HD X:Users:johnjohn:Pictures:notinmy:DSCF0009.JPG

The problem is we only want the images.
There a couple of ways to do this, this way uses a function that calls Applescript:

function isGraphicFile pfilePath
--first we build an appleScript in a variable
put empty into tAppleScript
put "tell application " & quote & "Finder" & quote & return after tAppleScript
put " set x to kind of alias " & quote & "[[pfilePath]]" & quote & return after tAppleScript
put "end tell" & return after tAppleScript
Get value(Script(do, Applescript, merge(tAppleScript)))
--we run an appleScript with
--get script(do,AppleScript,theScript)
--in this case we merge our script to get pfilePath into it
--and get the value to get rid of the quotes that appleScript encloses the kind it returns in.

--We now will check to see if the kind is an image format SC can deal with and retun true or false.
--The list of formats is pretty long so we can use the alt-L character ¬ to continue
--the script line on the next line.

return COMMA&it&COMMA is in ",JPEG Image,JPEG Image,JPEG Image,Windows Bitmap Image,JPEG 2000 Image,JPEG Image"¬
&",PDF Document,Apple PICT Document,Portable Network Graphics Image,Apple MacPaint Image"¬
&",Adobe Photoshop Image,Apple QuickTime Image,Silicon Graphics Image,Targa Image,TIFF Document"¬
&",Graphics Interchange Format Image,"
--Note we check for comma &it & comma not just it,
--that ensure that we have the whole kind not just document for instance

end isGraphicFile

We can put that script in our project script (control -p to edit the project script) and then add this to our button script:

put Dir (tfolder,false,true) into tfileList
repeat with x = the number of lines in tfilelist down to 1
if not isGraphicFile(line x of tFileList) then delete line x of tfilelist
end repeat

we now have a list of image files to add to our field, but we need the file names to go into the field and the whole path to go into the userProp.
We can loop though the list again:

repeat with x = 1 to the number of lines in tFilelist
put last item of line x of tfileList into line x of tfileNames
end repeat

Then put the results where we want them:

if the number of lines in cd fld "imagelist" = 0
then
put tfileNames into cd fld "imageList"
set the uImagePaths of cd fld "imageList" to tfileList
else
put CR& tfileNames after cd fld "imageList"
set the uImagePaths of cd fld "imageList" to the uImagePaths of cd fld "imageList"&CR& tfileList
end if

This will be a bit slow if you add a big folder of files, eg it took 2 and a half minutes to process my Pictures Folder that contained about 2 and a half thousand images (G4 400)
A way to speed up this script a bit would be to collect the file names as we loop through the file list testing for images.

Here are both scripts written out in full, in a new window