My script stops after adding a movie in iTunes

Hello,

I’m currently writing an AppleScript in order to make a folder action to convert a movie (so that it can be played by an iPod) and add it into iTunes.
Here is the text of my AppleScript:

on adding folder items to this_folder after receiving input_items
repeat with input_item in input_items
process_item(input_item)
end repeat
end adding folder items to

on process_item(this_item)
set myFileName to getFileName(this_item)
with timeout of 3600 seconds -- one hour per movie time limit
do shell script "/Applications/ffmpegx.app/Contents/Resources/ffmpeg -i " & the quoted form of POSIX path of this_item & " -f mp4 -y -maxrate 1000 -b 300 -qmin 3 -qmax 5 -bufsize 4096 -g 300 -vcodec mpeg4 -acodec aac " & the quoted form of ((POSIX path of (get path to temporary items folder from user domain)) & myFileName & ".mp4")
end timeout

do shell script "rm -f " & the quoted form of POSIX path of this_item
set newItem to (path to temporary items folder from user domain as string) & myFileName & ".mp4" as alias
tell application id "com.apple.iTunes"
activate
add newItem
end tell
do shell script "rm -f " & the quoted form of the POSIX path of newItem
end process_item

on getFileName(thefile)
set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {":"}}
set mytextfilename to last text item of (thefile as text)
set AppleScript's text item delimiters to oldDelims
return mytextfilename
end getFileName

Don’t be afraid, I’m in the beginning of my project: the code is yet quite dirty (a lot of checkings are missing, …).
But let’s focus on my issue.
The thing is the script randomly stops after the “adding in iTunes” line of the code.
Sometimes the movie is added in iTunes and the script goes on, whereas sometimes the movie is added in iTunes and the script stops.

I tried to:

  • catch an eventual error: there are none.
  • delay 10 seconds after the adding (since the script should then remove a file used by the add-to-iTunes line): it didn’t help…

Do you have an idea of what’s happening?
I don’t expect you to correct my code: I can perform some tests on your advice.
Above all, I want to understand how the process can be so random (I work in a IT department, so I know how computers can be weird sometimes, but here… I’m very surprised!).

At the very end, if you have also some advice not related to that issue for my project, I would be very glad to hear from you.

Thanks in advance.

Model: MacBook
Browser: Safari 525.27.1
Operating System: Mac OS X (10.5)

I’m not sure if this will fix your problem but this is working for me.


add newItem to playlist "Movies"

This script copies the movies from my local machine to a remote
machine and tells iTunes on the Remote Machine to load the movie.
I have used it several hundred times with no errors.

hth,

Craig

Thank you Craig for your suggestion.
Unfortunately, it didn’t help… I really can’t understand what’s wrong!

Can someone test my script:

  • add the script to a folder
  • put a copy of a movie in this folder (the script will delete the movie put in the folder)
  • wait until the new movie is added in iTunes
  • go to ~/Library/Caches/TemporaryItems
  • check if the new movie is still there.

Thanks in advance.

Try this.


on getFileName(thefile)
	set thefile to name of (info for (alias thefile))
	set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"."}}
	set mytextfilename to first text item of (thefile as text)
	set AppleScript's text item delimiters to oldDelims
	return mytextfilename
end getFileName

Regards,

Craig

Thank you for your help, Craig.
Although it helped me with one line of my ToDo list (replacing the extension instead of adding a new one),
it didn’t change the way how iTunes stops my script.

After some more tests, I’ve figured out in which case the script stops and when it does not.
In fact, the script goes on if the movie to be inserted has already been inserted (I believe it does not add it twice in iTunes and goes on, which seems quite logical).

Also, after my script process, it looks like foder actions are disabled (the check box is unflag).
However, it only “looks like” since adding another movie in the folder make my script process that movie… so strange!

But still no solution to making it work…

Do you use ScriptDebugger?

If not, you may want to give it a try. You can step
through each line of code and see the results.

That’s how I find most errors in my code.

Craig

Thanks again, Craig.
I gave it a try… and it looks like ScriptDebugger works better than Applescript!
When testing my script with it, it just do exactly what i want, with no error and no stop.
It converted my movie, removed the old one, add the new one in iTunes and… finally removed the new one (what my folder action script doesn’t want to do).

Why Applecsript doesn’t like me? :wink:
Is there a known bug in Applescript about that?
Am I supposed to contact an Apple developer team?
Is yes, which one and how to describe my problem (english isn’t my mother tongue, so I’m afraid not to be clear enough…)?