Dialog Loop from Script Application is disables folder action

I am trying to do two things at once: transcode a file/files and then move the files all together to another folder (where more applescripting then takes place). So everything has been working really well until I decided I needed a dialog box to display that the process was working, but only to have this dialog come up on the first file as it gets processed. Unfortunately, adding in this extra piece seems to have disabled my folder action that then moves the files once they are all in place. I don’t understand why this would happen.

Here is the code that transcodes the files, and I have set apart the area that is screwing things up in CAPS.


on open these_items
	
	repeat with i from 1 to the count of these_items

    --THIS IS THE PART THAT IS DISABLING MY FOLDER ACTIONS!
		if i is 1 then
			set ffmpeg_ok to "Transcoding has begun."
			activate (display dialog (ffmpeg_ok) buttons {"OK"} default button 1)
		end if
    --END PART THAT IS DISABLING FOLDER ACTIONS

		set this_item to (item i of these_items)
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) and ¬
			((the file type of the item_info is in the type_list) or ¬
				the name extension of the item_info is in the extension_list) then
			process_item(this_item)
		end if
	end repeat
end open

-- this sub-routine processes folders
on process_folder(this_folder)
	set these_items to list folder this_folder without invisibles
	repeat with i from 1 to the count of these_items
		set this_item to alias ((this_folder as text) & (item i of these_items))
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) and ¬
			((the file type of the item_info is in the type_list) or ¬
				the name extension of the item_info is in the extension_list) then
			process_item(this_item)
		end if
	end repeat
end process_folder

-- this sub-routine processes files
on process_item(this_item)
	
	set {name:Nm, name extension:Ex} to info for this_item
	if Ex is missing value then set Ex to ""
	if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
	set destinationFile to quoted form of (POSIX path of (path to movies folder) & Nm & ".mp4")
	with timeout of 10800 seconds -- three hour per movie time limit
		do shell script "/Applications/ffmpegx.app/Contents/Resources/ffmpeg -i " & quoted form of POSIX path of this_item & " -vcodec mpeg4 -b 2000 -s 640x480 -acodec aac -ab 448 -ar 48000 -vol 1024 -f mp4 " & destinationFile & "&> ~/Library/Logs/ffmpeg_transcode/" & quoted form of Nm & ".log.txt & "
		
	end timeout
	--delay 45
end process_item

So just in case you need it, here is the folder action that is not occurring when I add in that i loop for my dialog box:


on adding folder items to this_folder after receiving added_items

	checkStableSize(added_items)
	delay 45
	tell application "Finder"
		set theFolder to path to movies folder from user domain
		set targetFolder to folder "email@email.net" of home
		move entire contents of theFolder to targetFolder
	end tell
end adding folder items to


-- Handler to wait until a file is fully loaded.
on checkStableSize(theItem)
	set F to quoted form of POSIX path of theItem
	set sizeThen to first word of (do shell script "du -d 0 " & F) as integer --coercing to integer fixes the quote problem
	repeat
		try
			delay 1 -- seconds (set to longer if needed)
			set sizeNow to first word of (do shell script "du -d 0 " & F) as integer --coercing to integer fixes the quote problem
			if sizeNow - sizeThen = 0 then exit repeat
			set sizeThen to sizeNow
		on error
			exit repeat
		end try
	end repeat
end checkStableSize

I would greatly appreciate any suggestions you can throw my way as to what is going on. Thanks!

I’m sorry, I was wrong. It is the line where the shell script writes to my log. When I have this line in and create a log file, then for some reason the folder action never happens. Can anyone tell me what I’m doing wrong? I would like to generate logs for each transcode process as well as eventually get all the files moved to another location.

Hi,

why not this?

on open these_items
	display dialog "Transcoding has begun." buttons {"OK"} default button 1
	repeat with i from 1 to the count of these_items
.

maybe your checkStableSize() routine doesn’t work.
In the folder action handler the variable added_items is a list
but the handler expects a single item

I was able to change my logging command slightly by removing the “&” at the end, so that now it works properly. However, the problem of putting that dialog display at the beginning is that it hasn’t actually started to process any items. I want the dialog to act as a flag so the user knows the command has begun, where as putting it at the top displays the dialog even if the ffmpeg command doesn’t start. What I would ideally want is the dialog to display in this area:


on process_item(this_item)
	set {name:Nm, name extension:Ex} to info for this_item
	if Ex is missing value then set Ex to ""
	if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
	set destinationFile to quoted form of (POSIX path of (path to movies folder) & Nm & ".mp4")
	with timeout of 10800 seconds -- three hour per movie time limit
		do shell script "/Applications/ffmpegx.app/Contents/Resources/ffmpeg -i " & quoted form of POSIX path of this_item & " -vcodec mpeg4 -b 2000 -s 640x480 -acodec aac -ab 448 -ar 48000 -vol 1024  -f mp4 " & destinationFile & "&>~/Library/Logs/ffmpeg_transcode/" & quoted form of Nm & ".log.txt "
	end timeout
	--delay 45
end process_item


EXCEPT that I only want the dialog to display for the first item, not for every item if there is more than one to process. I can’t figure it out, as this is a function, but I need it to somehow know when it’s the first item.

what’s about this, the dialog dismisses automatically after 3 seconds

on open these_items
	display dialog "Transcoding starting." buttons {"OK"} default button 1 giving up after 3
	repeat with i from 1 to the count of these_items
.