Folder Action Repeat Loop only Executes Once

I am still feeling my way through all the scripting stuff so please bare with me.

on adding folder items to thisFolder after receiving addedItems
	tell application "Finder"
		
		repeat with i from 1 to number of items in addedItems
			set anItem to item i of addedItems
			set the item_info to the info for anItem
			
			set oldDelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {"."}
			
			try
				set fileName to name of anItem
				set nameWithoutExtension to first text item of fileName
				set fileExt to last text item of fileName
				
				do shell script "/Applications/ffmpegX.app//Contents/Resources/ffmpeg -i " & "/Users/Reid/Sites/RMSstream/wma_media/" & fileName & " -acodec mp3 /Users/Reid/Sites/RMSstream/tmp_media/" & nameWithoutExtension & ".mp3"
				
				(*tell application "Terminal"
					do script "/Applications/ffmpegX.app//Contents/Resources/ffmpeg -i " & "/Users/Reid/Sites/RMSstream/wma_media/" & fileName & " -acodec mp3 /Users/Reid/Sites/RMSstream/tmp_media/" & nameWithoutExtension & ".mp3"
				end tell*)
				
			end try
			set AppleScript's text item delimiters to oldDems
		end repeat
	end tell	
end adding folder items to

So the idea is I drop a collection of wma files into a folder and the get spit out as mp3s elsewhere.

When I put the files in the folder the script executes converts the first file and then stops. In troubleshooting I did discover that after executing the shell script (using either method above) it will not execute on as if it is jumping out of the loop. Any help you can offer would be greatly appreciated. Thanks.

Ciao,
eventually your script is running out of time because the ffmpeg conversion takes too long for AppleScript’s default settings (which is, If I remember well, 3 minutes). You can try to change the timeout settings using

with timeout of 3000 -- seconds
-- your script code
end timeout

Good scripting
Farid

Welcome aboard reidef!

Aside from Farid’s comment, your method of finding the base name and extension of the file name will fail if there are any other periods in it like my.file.txt. The bombproof method is this script which doesn’t have to be in a Finder tell block, but works if it is:


set F to choose file
tell (info for F)
	set ext to name extension
	set basename to name's text 1 thru ((my (offset of ("." & ext) in name)) - 1)
end tell

‘offset’ is a useful term for parsing text - it is the location from the beginning of the string of its argument.


set myText to "Now is the ninth of November, 2006"
set OffS to offset of "," in myText --> 29
set Piece to text 1 thru (OffS - 1) of myText
--> "Now is the ninth of November"

In the line to find basename, I’m using the offset of period & the extentsion of the file name and taking everything before it.

Thank you both for the prompt reply. I implemented Farid’s suggestion to no avail so I began to segment the script to try and figure out why it doesn’t repeat. I removed the variables from the loop and tested it using 2 files and now the loop doesn’t execute at all. Did I mess up the syntax of the loop or somewhere else?

on adding folder items to thisFolder after receiving addedItems
	tell application "Finder"
		display dialog "File detected." buttons {"OK"} default button 1
		
		repeat with i from 1 to 2
			set anItem to item i of addedItems
			set the item_info to the info for anItem
			
			set oldDelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {"."}
			
			try
				set fileName to name of anItem
				set nameWithoutExtension to first text item of fileName
				set fileExt to last text item of fileName
				
				display dialog ("File " & i & " of " & number of items in addedItems & " processed.") buttons {"OK"} default button 1
			end try
			set AppleScript's text item delimiters to oldDems
		end repeat
	end tell
end adding folder items to

When I place the 2 files in the folder the first dialogue appears but the ones in the loop never do. I am quite confused.

Thanks again,

  • Reid

On 4th from last line change: “set AppleScript’s text item delimiters to oldDems” to “set AppleScript’s text item delimiters to oldDelims

I hang my head in shame and bow to your greatness.

Thank you!