Grabbing information prior to a character in a list of file names.

I have a list of filenames that contain an “_” (underscore).

What I would like to do is set a variable from the characters prior to the underscore. I know that there is a way to do it, but I have not stumbled across it, yet.

In otherwords


repeat with x from 1 to count of allFiles
set thisOrder to all characters prior to "_" in item x of allFiles
end repeat

Any ideas?

How about this:

set theFileNames to {"test1_Foo.jpg", "test2_bar.png", "test3_Else.png"}
set Prior to {}

set {TID, text item delimiters} to {text item delimiters, "_"}

repeat with anItem in theFileNames
	set end of Prior to first text item of anItem
end repeat
set text item delimiters to TID

Prior ---> {"test1", "test2", "test3"}

That works great - now lets make it a bit more complex.

I now have two lists -
one that is the prefix of the filenames and one that is the list of filenames themselves.

What I need to do is to say if OrderID = Prior then go get the related file and input it into FileMaker.

so, if OrderID = “test2” - then I want it to be able to know that it is the 2nd item in the Prior list - and therefore the image I want is “test2_bar.jpg” from theFileNames list (which is also the second item).

How can i do that?

Is there a better way to handle this?

Should I create a record that relates the two lists?

something like this?
{OrderID: “test2”, file: “test2_bar.jpg”}

Can’t say that I have ever worked with records enough to know how to manipulate them…

This is the general idea:

set theFileNames to {"test1_Foo.jpg", "test2_bar.png", "test3_Else.png"}
set Leader to {}
set {TID, text item delimiters} to {text item delimiters, "_"}
repeat with anItem in theFileNames
	set end of Leader to first text item of anItem
end repeat
set text item delimiters to TID
-- The above we had before with Prior changed to Leader (for no reason)
set OrNum to text returned of (display dialog "Please enter a test number" default answer "test1")
if Leader contains OrNum then -- no point in running thru the repeat if it isn't there
	repeat with k from 1 to count of Leader
		if item k of Leader = OrNum then
			set theFile to item k of theFileNames
			exit repeat -- stop when you find it; don't continue to the end of the list
		end if
	end repeat
else
	display dialog "OrNum not found" -- Not there
end if

theFile

Ok, here is what I am doing - the whole enchilada.

as I have started to describe - I have a folder of images that I am comparing to a list of records… here is the script that I have been developing over the last few days.


set imageFolder to (choose folder with prompt "Where is the folder containing the images?") as string
set allFiles to list folder imageFolder without invisibles
set OrderID_fromAllFiles to {}

set {TID, text item delimiters} to {text item delimiters, "_"}

repeat with anItem in allFiles
	set end of OrderID_fromAllFiles to first text item of anItem
end repeat

set text item delimiters to TID

--return count of allFiles

tell application "FileMaker Pro"
	activate
	tell document "someFileMakerData"
		count the records
		set recordCount to the result
		tell layout "Layout #1"
			repeat with counterVar from 1 to recordCount by 1
				go to record counterVar
				tell record counterVar
					--First the script gets the Order ID from the record.
					tell cell "Order ID"
						(get data) & return
						try
							set OrderID to text 1 thru -2 of the result
						on error
							set OrderID to ""
						end try
					end tell
					--Then it checks for the corresponding image in the Job Folder.  If it is there, it imports the image into the record.  If not, then it checks to see if it is an order for a team image and imports the "Team Image Only" file.
					if OrderID_fromAllFiles contains OrderID then
						set cell "Available Image" to "True"
						repeat with k from 1 to count of allFiles
							if item k of OrderID_fromAllFiles = OrderID then
								set theFile to item k of OrderID_fromAllFiles
								exit repeat
							end if
						end repeat
						set cell "Image" to file (imageFolder & theFile)
						set cell "FileName" to theFile
						--call to remove OrderID.jpg from the list: allFiles - that way you can use the remaining items in the list to troubleshoot mismatched images.
						set itemsToDelete to {theFile}
						set cleanList to {}
						repeat with i from 1 to count allFiles
							if {allFiles's item i} is not in itemsToDelete then set cleanList's end to allFiles's item i
						end repeat
						set allFiles to cleanList
						
						--call to remove OrderID.jpg from the list: OrderID_fromAllFiles - that way you can use the remaining items in the list to troubleshoot mismatched images.
						set itemsToDelete to {OrderID}
						set cleanList to {}
						repeat with i from 1 to count OrderID_fromAllFiles
							if {OrderID_fromAllFiles's item i} is not in itemsToDelete then set cleanList's end to OrderID_fromAllFiles's item i
						end repeat
						set OrderID_fromAllFiles to cleanList
						
					else
						if cell "Pkg 1" contains "O" and cell "First" does not contain "XYZ" then
							set cell "Available Image" to "Graphic Image"
						else
							set cell "Available Image" to "False"
						end if
					end if
				end tell
			end repeat
			
			--The script then looks at the Database and looks for records that do not have proper image files.
			try
				count (every record whose (cell "Available Image" = "False"))
				set faultyImage_count to the result
			on error
				set faultyImage_count to 0
			end try
			if faultyImage_count > 0 then
				tell (every record whose (cell "Available Image" = "False"))
					set faulty_list to cell "Order ID"
				end tell
			else
				set faulty_list to {}
			end if
			set allFiles_count to the number of items in allFiles
		end tell
	end tell
end tell

--create a report of faulty images
tell application "TextEdit"
	activate
	make new document at the front
	if ((faultyImage_count ≠ 0) or (allFiles_count ≠ 0)) then
		set ASTID to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {", "}
		set text of front document to (return & "Orders without Images" & return & (faulty_list as string) & return & return & "Image Files without Orders" & return & (allFiles as string))
		set AppleScript's text item delimiters to ASTID
	else
		set text of front document to "return & return &Great Job!  Everything lines up!"
	end if
end tell


My issue goes back to the item k of the two lists…

At some point the item k of allFiles and OrderID_fromAllFiles is not lining up properly because I am getting a statement in the record that says the image is in the folder, but it imports the improper imagefile. (Edit) → This is not happening all the time - Sometimes, it does indeed return the correct image - however, there is something with the item k of one list does not equal the item k of the other and it returns the wrong image…

Is the section that cleans out the two lists resorting the records at some point?

So, I go back to the last question I had re: records. Should I create a record for each entry and then go through and delete the records? What would that look like?

If the cleaner is screwing things up try this instead:

set theFileNames to {"test1_Foo.jpg", "test2_bar.png", "test3_Else.png"}
set Leader to {}
set {TID, text item delimiters} to {text item delimiters, "_"}
repeat with anItem in theFileNames
	set end of Leader to first text item of anItem
end repeat
set text item delimiters to TID
-- The above we had before with Prior changed to Leader (for no reason)
set OrNum to text returned of (display dialog "Please enter a test number" default answer "test1")
if Leader contains OrNum then -- no point in running thru the repeat if it isn't there
	repeat with k from 1 to count of Leader
		if item k of Leader = OrNum then
			set theFile to item k of theFileNames
			set item k of theFileNames to "gone"
			set item k of Leader to "gone"
			exit repeat -- stop when you find it; don't continue to the end of the list
		end if
	end repeat
else
	display dialog "OrNum not found" -- Not there
end if
{theFile, theFileNames, Leader}
--> {"test2_bar.png", {"test1_Foo.jpg", "gone", "test3_Else.png"}, {"test1", "gone", "test3"}}

Then it’s easy later to remove the “gone” entries. You could also use missing value (no quotes).

Thank you - I think the logic part of my brain shutdown a few hours ago…

Happens to us all. Good luck with rest of it.