Script won't batch all files like it should

Ok, I am fairly new at this and must say that I am quite proud of myself for this script :smiley: . Mainly because it will save me a lot of time and it took awhile for me to figure out. When I run this script on a folder full of files, it does what it should except it never does all of the files in the folder. The idea is to take my standard file name ā€œ0121_102706_02_05.jpgā€ which consists of a 4 digit code that represents an artist name and a 6 digit code for the date. The script calls filemaker for the artist name based on the 4 digit code. Then creates a folder based on the artist name and a subfolder for the date. Does anybody know why it wonā€™t do all of the files? They are all jpeg files and all are consistently named. I need to apply this to a folder with 1000+ images. Also, if anybody happens to have any suggestions as to streamlining the script so it doesnā€™t have to check with filemaker for every file if the 4 digit code is the same I would be very appreciative. Thanks for the help.

Paul


set this_folder to choose folder

tell application "Finder"
	set fileList to every file of this_folder
end tell
repeat with thisFile in fileList
	set thisFilename to name of thisFile
	set filecode to (characters 1 thru 4 of thisFilename)
	set filedate to (characters 6 thru 11 of thisFilename)
	set code1 to item 1 of filecode
	set code2 to item 2 of filecode
	set code3 to item 3 of filecode
	set code4 to item 4 of filecode
	set code6 to item 1 of filedate
	set code7 to item 2 of filedate
	set code8 to item 3 of filedate
	set code9 to item 4 of filedate
	set code10 to item 5 of filedate
	set code11 to item 6 of filedate
	set finalCode to {code1 & code2 & code3 & code4}
	set finalDate to {code6 & code7 & code8 & code9 & code10 & code11}
	
	tell application "FileMaker Pro"
		open alias "Macintosh HD:Users:jayb:Documents:Filemaker Documents:artist_codes.fp7"
		try
			delete every request
		end try
		create request
		set cell "Code Number" of request 1 to finalCode
		find
		set artName to field "Artist Name" of current record
	end tell
	set theFolder to alias "Masters_6TB:"
	tell application "Finder"
		try
			make new folder at theFolder with properties {name:artName}
			
		on error -->folder exists
			set dateFolder to finalDate as string
			try
				make new folder in folder artName in theFolder with properties {name:dateFolder} -->creates dated subfolder
			end try
			
			set fileVar to thisFile as string
			try
				move fileVar to folder dateFolder in folder artName in theFolder -->moves file into dated subfolder
			end try
		end try
		
	end tell
end repeat

You might consider these faster ways of getting your codes:


set N to "0121_102706_02_05.jpg"
set filecode to text 1 thru 4 of N --> "0121"
set filedate to text 6 thru 11 of N --> "102706"

-- To make the subfolders sort in date order in the Finder

tell N to set dateOrder to text 10 thru 11 & text 6 thru  9
--> "061027"

That is certainly a more concise way to get the code :wink: . Thanks. I still donā€™t understand why it wonā€™t do all the files in a folder.

I canā€™t help you with FM, but your try blocks donā€™t warn you if they donā€™t complete. You might try:


try
   delete every request
on error e
   display dialog e
end try

At least during debugging, do that for every try. Something is failing and we need to know what.

Not bad for a first script. :slight_smile:

Iā€™m afraid I canā€™t help with FMP either. However, the ā€˜tryā€™ block in the Finder section at the bottom of your script is arranged in such a way that, if the script successfully makes a new folder for the artist, no date folder is created for the file being handled at the time and the file isnā€™t moved. Iā€™d guess you want the code to look something like this:

set this_folder to choose folder

tell application "Finder"
	set fileList to every file of this_folder
end tell
repeat with thisFile in fileList
	set thisFilename to name of thisFile
	set finalCode to text 1 thru 4 of thisFilename
	set finalDate to text 6 thru 11 of thisFilename
	
	tell application "FileMaker Pro"
		open alias "Macintosh HD:Users:jayb:Documents:Filemaker Documents:artist_codes.fp7"
		try
			delete every request
		end try
		create request
		set cell "Code Number" of request 1 to finalCode
		find
		set artName to field "Artist Name" of current record
	end tell
	set theFolder to alias "Masters_6TB:"
	tell application "Finder"
		try
			make new folder at theFolder with properties {name:artName}
		end try
		
		try
			make new folder in folder artName in theFolder with properties {name:finalDate} -->creates dated subfolder
		end try
		
		try
			move thisFile to folder dateFolder in folder artName in theFolder -->moves file into dated subfolder
		end try
		
	end tell
end repeat

Wow, you guys are good! Thanks for the help. It now copies everything over. One last thingā€¦How would I go about holding the 4 digit filecode on the clipboard or in a variable so that the next file is checked against the previous file, if it is equal then proceed to the filing and folder creation but if it is not equal then look up the code in FMP? Any help would be great.

Paul



set this_folder to choose folder

tell application "Finder"
	set fileList to every file of this_folder
end tell
repeat with thisFile in fileList
	set thisFilename to name of thisFile
	set filecode to text 1 thru 4 of thisFilename
	set filedate to text 6 thru 11 of thisFilename
	
	
	tell application "FileMaker Pro" -->get filecode from FMP
		open alias "Macintosh HD:Users:jayb:Documents:Filemaker Documents:artist_codes.fp7"
		try
			delete every request
		on error e
			display dialog e
		end try
		create request
		set cell "Code Number" of request 1 to filecode
		find
		set artName to field "Artist Name" of current record
	end tell
	set theFolder to alias "Masters_6TB:test:" -->start filing and folder creation
	tell application "Finder"
		try
			make new folder at theFolder with properties {name:artName}
		end try
		
		
		
		set dateFolder to filedate as string
		try
			make new folder in folder artName in theFolder with properties {name:dateFolder} -->creates dated subfolder
		end try
		
		set fileVar to thisFile as string
		try
			move fileVar to folder dateFolder in folder artName in theFolder -->moves file into dated subfolder
		end try
	end tell
end repeat

Like this, perhaps:


set saveCode to {} -- a placeholder.
repeat with thisFile in fileList
	set thisFilename to name of thisFile
	set finalCode to text 1 thru 4 of thisFilename
	set end of saveCode to finalCode
	set finalDate to text 6 thru 11 of thisFilename
	
	-- Suppose saveCode looks like {0121, "Jones", 0212, "Smith", etc. }
	-- and before create request for a new file:
	
	if filecode is in saveCode then -- find Artist
		repeat with k from 1 to count saveCode
			if item k of saveCode is filecode then
				set ArtName to item (k + 1) of saveCode
				exit repeat -- found it, don't keep looking
			end if
		end repeat
	else -- the rest commented out because I don't have FM
		(*create request
set cell "Code Number" to request 1 of filecode
find 
set artName to field "Artist Name" of current record
set end of saveCode to artName -- save that right after the code
*)
	end if
	-- and so on
end repeat

I think there are a couple of bugs in Adamā€™s code, but the ideaā€™s good. Hereā€™s a complete (and hopefully working!) version of the entire script:

set this_folder to choose folder
set theFolder to alias "Masters_6TB:"

tell application "Finder"
	set fileList to every file of this_folder
end tell

set matchedCodes to {}
repeat with thisFile in fileList
	set thisFilename to name of thisFile
	set filecode to text 1 thru 4 of thisFilename
	set filedate to text 6 thru 11 of thisFilename
	
	if (filecode is in matchedCodes) then
		-- If this file code has appeared earlier in the run, search the odd-numbered items of matchedCodes for it.
		set i to 1
		repeat until (item i of matchedCodes is filecode)
			set i to i + 2
		end repeat
		-- Read the artist name from the following, even-numbered item.
		set ArtName to item (i + 1) of matchedCodes
	else
		-- Otherwise, get the artist name from FMP.
		tell application "FileMaker Pro"
			open alias "Macintosh HD:Users:jayb:Documents:Filemaker Documents:artist_codes.fp7"
			try
				delete every request
			end try
			create request
			set cell "Code Number" of request 1 to fileCode
			find
			set artName to field "Artist Name" of current record
		end tell

		-- Update the list of matched codes and artist names.
		set end of matchedCodes to filecode
		set end of matchedCodes to ArtName
		
		-- Try making a folder for this new artist.
		tell application "Finder"
			try
				make new folder at theFolder with properties {name:ArtName}
			end try
		end tell
	end if
	
	tell application "Finder"
		try
			make new folder in folder ArtName in theFolder with properties {name:filedate} -->creates dated subfolder
		end try
		
		try
			move thisFile to folder filedate in folder ArtName in theFolder -->moves file into dated subfolder
		end try
	end tell
end repeat

Pardon the bugs, gentlemen - it was very late when I had a quick look at the thread before heading for the sack. :rolleyes:

Do you have FM, Nigel? If not, how do you get the nicely compiled post?

so do chime in here with a little FM hint

if you have a table of with artist and related artist code you could just go to FM once get the two fields as arrays and the compare the code in the file name to the elements of the artist code then youā€™ll know element the artist naem is and you can xtract that fot make the folder

I think that would make your Applescript code easier to manage

if that doensā€™t make sense let me know and Iā€™ll throw together and example

mm

ok well here is a starting point


tell application "FileMaker Pro Advanced"
	tell window "artcode"
		set ArtName to field "ArtistName" -- returns an array of artist names
		set ArtCode to field "ArtistCode" -- returns and array of artist codes
	end tell
end tell

set this_folder to choose folder
-set theFolder to alias "Masters_6TB:"


tell application "Finder"
	set fileList to every file of this_folder
end tell

repeat with thisFile in fileList
	set thisFilename to name of thisFile
	set ArtistCode to text 1 thru 2 of thisFilename --- or whatever the extraction is suppose to be
	set finalCode to text 1 thru 4 of thisFilename
	set finalDate to text 6 thru 11 of thisFilename
	
	repeat with i from 1 to count of itmes in ArtCode
		if ArtistCode is equal to item i of ArtCode then
			set ArtistNname to item i of ArtName
			exit repeat
		end if
	end repeat
	
	
	-- carry on
end repeat

Ah. Well. Having only a dial-up connection, I compose most of my messages while off-line (apart from some of the ones that are obviously appallingly phrased or have been edited several times). For this, I save the threads that interest me to disk before disconnecting. I compose my replies in TextEdit, using a certain script to copy any text from Script Editor into them along with the necessary

 tags. I then past the completed messages into Stickies notelets, which are conveniently small, and paste from these into the BBS's reply windows when back on line.

I don't have FileMaker, but once I'd pasted the text of Paul's original script into Script Editor, I only had to comment out the FileMaker 'tell' block and the alias, after which I was able to compile the rest and fool around with it. After copying the modified script into my TextEdit reply, I uncommented the 'tell' block and the alias in TextEdit itself and restored the tab indents, giving the impression that those parts of script had been compiled too.  :)

Slick, Mr. G. :lol:

You guys are great!!! :smiley: I still need to dissect and figure out exactly what you did Nigel. You did have a bug, you didnā€™t define dateFolder and had a try statement on the move section. So it did not actually copy the files because datefolder wasnā€™t defined. But seems to work great now. I think you guys saved me about 100 hairs on my head!!! :slight_smile:

Bugs are the inevitable result of not being able to test if you donā€™t own the scripted applications.:rolleyes:

were you able to use the FM hint I through out there for you ā€¦ does it aleast make sense ?:smiley:

Browser: Firefox 2.0
Operating System: Mac OS X (10.4)

mcgrailm,

Thanks for the tip. I think that it makes sense. I have yet to digest all the tips. I am still rather new at this so I need to sit and stew(marinate if you will) a little bit on this. What would it accomplish differently from what I had?

you would only eveer have to call filemaker once at the begining of the script without have to put any logic around it you just open the database show all the records and extract the records from a field into arrays :cool:

mm

Many apologies. There were a few changes of variable name further up-thread and I managed to confuse myself. Iā€™m glad you were able to sort it out. Iā€™ve now corrected it above myself for posterity. :slight_smile: