Nested folders and Ignoring file type

Hi-

I have a working script that does a bunch of things, including taking data from an exported FileMaker tab delimited text document and using the data to embed into IPTC header fields of image files using some command line tools. The script below works fine, but now I need to deal with sub foilders within the chosen folder of image files and also to ignore files that are not .mos files (which are Leaf digital camera RAW files). Any assistance is appreciated. I have tried a few things with no luck and time is closing in. I think my issue is that the working script creates a list of filenames, rather than files themselves. Thanks in advance.

property newExportFile : “/Users/xsanadmin/Desktop/newFM_Export.txt”
property type_list : {“MOSC”}
property extension_list : {“.mos”}

do shell script “/Applications/ProcessingFolder/cleanup.sh”

choose folder with prompt “Choose the folder of image files.”
set thePath to the POSIX path of result
set fileNames to (list folder (POSIX file (result)) without invisibles)

repeat with i from 1 to (count fileNames)
set FileList to (item i of the fileNames)
if (the kind of FileList is “Folder”) then
set this_item to (this_item & (every item of FileList))
if (the file type of this_item is in the type_list) or (the name extension of this_item is in the extension_list) then
do shell script "field=$(cut -f 1 " & newExportFile & “); echo $field”
set fieldValue to the quoted form of the result
do shell script "/Applications/ProcessingFolder/Image-ExifTool-5.72/exiftool -overwrite_original " & ¬
“-IPTC:city=” & fieldValue & “” & " " & ¬
quoted form of (thePath & (item i of the fileNames))
end if
end if
end repeat

repeat with i from 1 to (count fileNames)
set FileList to (item i of the fileNames)
if (the kind of FileList is “Folder”) then
set this_item to (this_item & (every item of FileList))
if (the file type of this_item is in the type_list) or (the name extension of this_item is in the extension_list) then
do shell script "field=$(cut -f 2 " & newExportFile & “); echo $field”
set fieldValue2 to the quoted form of the result
do shell script "/Applications/ProcessingFolder/Image-ExifTool-5.72/exiftool -overwrite_original " & ¬
“-IPTC:headline=” & fieldValue2 & “” & " " & ¬
quoted form of (thePath & (item i of the fileNames))
end if
end if
end repeat

repeat with i from 1 to (count fileNames)
set FileList to (item i of the fileNames)
if (the kind of FileList is “Folder”) then
set this_item to (this_item & (every item of FileList))
if (the file type of this_item is in the type_list) or (the name extension of this_item is in the extension_list) then
do shell script "field=$(cut -f 3 " & newExportFile & “); echo $field”
set fieldValue3 to the quoted form of the result
do shell script "/Applications/ProcessingFolder/Image-ExifTool-5.72/exiftool -overwrite_original " & ¬
“-IPTC:credit=” & fieldValue3 & “” & " " & ¬
quoted form of (thePath & (item i of the fileNames))
end if
end if
end repeat

repeat with i from 1 to (count fileNames)
set FileList to (item i of the fileNames)
if (the kind of FileList is “Folder”) then
set this_item to (this_item & (every item of FileList))
if (the file type of this_item is in the type_list) or (the name extension of this_item is in the extension_list) then
do shell script "field=$(cut -f 4 " & newExportFile & “); echo $field”
set fieldValue4 to the quoted form of the result
do shell script "/Applications/ProcessingFolder/Image-ExifTool-5.72/exiftool -overwrite_original " & ¬
“-IPTC:source=” & fieldValue4 & “” & " " & ¬
quoted form of (thePath & (item i of the fileNames))
end if
end if
end repeat

repeat with i from 1 to (count fileNames)
set FileList to (item i of the fileNames)
if (the kind of FileList is “Folder”) then
set this_item to (this_item & (every item of FileList))
if (the file type of this_item is in the type_list) or (the name extension of this_item is in the extension_list) then
do shell script "field=$(cut -f 5 " & newExportFile & “); echo $field”
set fieldValue5 to the quoted form of the result
do shell script "/Applications/ProcessingFolder/Image-ExifTool-5.72/exiftool -overwrite_original " & ¬
“-IPTC:province-state=” & fieldValue5 & “” & " " & ¬
quoted form of (thePath & (item i of the fileNames))
end if
end if
end repeat

repeat with i from 1 to (count fileNames)
set FileList to (item i of the fileNames)
if (the kind of FileList is “Folder”) then
set this_item to (this_item & (every item of FileList))
if (the file type of this_item is in the type_list) or (the name extension of this_item is in the extension_list) then
do shell script "/Applications/ProcessingFolder/SetFile -c ‘SCam’ -t ‘MOSC’ " & quoted form of (thePath & (item i of the fileNames))
end if
end if
end repeat

display dialog “All Done” buttons {“OK”} default button “OK”

gold:

Your three lines near the top wherein you choose a folder to collect all the files is the root of the problem, I believe. Yes, it only returns filenames, not their locations. Replace that with this:

set fileNames to {}
set the_folder to choose folder with prompt "Choose the folder of image files."
tell application "Finder" to set all_files to every file of entire contents of folder the_folder
repeat with a_file in all_files
	set end of fileNames to (quoted form of POSIX path of (a_file as Unicode text))
end repeat


Now, your list in fileNames is a full POSIX reference to every file in the entire folder that was chosen. You can now set up your loop on that list, check to see if it meets your requirements, and send it to the shell.

Good luck

casdvm

You could exclude the .mos files by changing the "finder line in casdvm’s snippet to:

tell application "Finder" to set all_files to every file of entire contents of folder the_folder whose name extension is not "mos"

I don’t think casvdm’s solution addresses the need for the variable thePath, which is passed to to:

do shell script "/Applications/Image-ExifTool-5.68/exiftool -overwrite_original " & ¬
	"-IPTC:city=" & fieldValue & "" & " " & ¬
	quoted form of (thePath & (item i of the fileNames))

Jerome’s solution would seem logical, except it should be:

tell application “Finder” to set all_files to every file of entire contents of folder the_folder whose name extension is “mos”

because I only want to process .mos files.

Lastly, I am still confused as to how to get files within folders, within folders to be seen. Thanks!!!

gold:

I apologize for being unclear before. When you use the term [entire contents], the Finder goes through every single subfolder of the chosen folder and grabs each file it finds. So, the list fileNames is a COMPLETE list of every file of the chosen folder, as well as evey subfolder within that chosen folder. The list also contains each file as a DIRECT REFERENCE, meaning that you no longer need to use your if statements to test if the item is a folder or not; it will never be a folder, only a file. Also, you never need to use your preamble thePath, because every item of the list is a DIRECT REFERENCE, so the preamble that indicates which folder and subfolder it belongs to is already there.

Jerome is correct in his instructions to add another parameter to my [tell Finder] line; that could save your script a lot of time when it runs, depending on how many files are to be processed. Although I believe you said that wanted EVERY file that is [.mos], and no others, right?

I haven’t got time to completely go through your script to re-address the items in the list, but here is a sample of what we are talking about:

set fileNames to {}
set the_folder to choose folder with prompt "Choose the folder of image files."
tell application "Finder" to set all_files to every file of entire contents of folder the_folder whose name extension is "mos"--This will grab every file in the chosen folder, as well as every file in every sub folder of the chosen folder whose name extension is .mos
repeat with a_file in all_files
	set end of fileNames to (quoted form of POSIX path of (a_file as Unicode text))
end repeat

repeat with i from 1 to (count fileNames)
	--do your shell stuff here, every item in the list is a DIRECT REFERENCE, already in quoted form, so just plug in the (item i of filenames) as the file to work on, and you are home free
end repeat

I hope this clears it up a bit. Yes, you need to re-do some of your code, but believe me, this GREATLY simplifies the grabbing of your needed files, so that you can use your repeat loops to just do the work, and not worry about getting a bum reference to an incorrect file.

Good luck, let us know if you are still unsure of things.

casdvm

casvdm-

Thanks loads! Your last post clarified things and I re-did the code and all works great! Here’s the final code:

property newExportFile : "/Users/hrgold/Desktop/TheSpace/newFM_Export.txt"

do shell script "/Users/hrgold/Desktop/TheSpace/cleanup.sh"

set fileNames to {}
set the_folder to choose folder with prompt "Choose the folder of image files."
tell application "Finder" to set all_files to every file of entire contents of folder the_folder whose name extension is "mos"
repeat with a_file in all_files
	set end of fileNames to (quoted form of POSIX path of (a_file as Unicode text))
end repeat

repeat with i from 1 to (count fileNames)
	do shell script "field=$(cut -f 1 " & newExportFile & "); echo $field"
	set fieldValue to the quoted form of the result
	do shell script "/Applications/Image-ExifTool-5.68/exiftool -overwrite_original " & ¬
		"-IPTC:city=" & fieldValue & "" & " " & ¬
		(item i of the fileNames)
end repeat

repeat with i from 1 to (count fileNames)
	do shell script "field=$(cut -f 2 " & newExportFile & "); echo $field"
	set fieldValue2 to the quoted form of the result
	do shell script "/Applications/Image-ExifTool-5.68/exiftool -overwrite_original " & ¬
		"-IPTC:headline=" & fieldValue2 & "" & " " & ¬
		(item i of the fileNames)
end repeat

repeat with i from 1 to (count fileNames)
	do shell script "field=$(cut -f 3 " & newExportFile & "); echo $field"
	set fieldValue3 to the quoted form of the result
	do shell script "/Applications/Image-ExifTool-5.68/exiftool -overwrite_original " & ¬
		"-IPTC:credit=" & fieldValue3 & "" & " " & ¬
		(item i of the fileNames)
end repeat

repeat with i from 1 to (count fileNames)
	do shell script "field=$(cut -f 4 " & newExportFile & "); echo $field"
	set fieldValue4 to the quoted form of the result
	do shell script "/Applications/Image-ExifTool-5.68/exiftool -overwrite_original " & ¬
		"-IPTC:source=" & fieldValue4 & "" & " " & ¬
		(item i of the fileNames)
end repeat

repeat with i from 1 to (count fileNames)
	do shell script "field=$(cut -f 5 " & newExportFile & "); echo $field"
	set fieldValue5 to the quoted form of the result
	do shell script "/Applications/Image-ExifTool-5.68/exiftool -overwrite_original " & ¬
		"-IPTC:province-state=" & fieldValue5 & "" & " " & ¬
		(item i of the fileNames)
end repeat

repeat with i from 1 to (count fileNames)
	do shell script "/Developer/Tools/SetFile -c 'SCam' -t 'MOSC' " & (item i of the fileNames)
end repeat

display dialog "All Done" buttons {"OK"} default button "OK"

Gold:

Nicely done, congratulation. Would you be so kind as to enlighten me (and anyone else reading this) what these shell programs are that you are using and where you got them? There were a couple of posts yesterday inquiring into the whole metadata access issue and it looks like you have something useful there.

casdvm

The cleanup.sh script is just a shell script to clean up the line breaks in the tab delimited text file that comes out of FileMaker.

{#!/bin/bash

perl -p -e ‘s/\r/\r\n/g’ </path-to-input-file.txt >/path-to-output-file.txt}

I use the Unix command “cut” to extract the information I want from the particular column of the text file.

ExifTool is a wonderful command line tool to do lots of things with image files. It is available at:

http://www.sno.phy.queensu.ca/~phil/exiftool/

Finally, I use SetFIle which comes with the Apple Developer tools to reset the file type and creator of the files. They seem to get corrupted when writing to the metadata header fields of the files.

The concept here is that a photo studio has a FileMaker database to keep track of jobs. They wanted to embed certain information from the job records into hundreds or thousands of image files so that the information made the image files searchable in a digital asset management system. I used IPTC fields in the headers because they are fairly standardized as far as asset management systems go. This script automates the process for the most part.

Hope that helps others. Please don’t hesitate to contact me if you need more information. My email is howie@cdiny.com. Thanks again for all your assistance!

This is great! Thanks for the info.

casdvm

Thanks for catching that, I missed the “not” when reading the post.

hrgold,

Thanks for the information on the shell scripting. I have wanted to start learning that for some time but never seem to get around to it. This looks like it could be very usefull, and wish I knew about it a year ago…

Question, is this information compatible with the metadata used by PhotoShop and other Adobe applications?