AppleScript and do shell script

Discussion in 'Mac Programming' started by refulgentis, Feb 7, 2009.

  1. refulgentis macrumors regular

    May 14, 2006
    I have a rather lengthy script I'm using as a folder action for automatic video conversion, essentially it boils down to "add (extension) to this folder? ok, pass it to a shell script and convert."

    This works fine, except for one thing -- since the folder action is still running while it's running "do shell script (conversion line)", any files added during the conversion are ignored. This is unacceptable, because video conversion takes so darn long.

    Any suggestions?

  2. HiRez macrumors 603


    Jan 6, 2004
    Western US
    You could have your script maintain a queue of files to be processed. Store the original list of files and then, after each conversion, drop the current (just converted) file off the list, get a list of the current contents of the drop folder and compare that to the original list, adding into the queue the new ones. Kind of a PITA but it should work.

    Alternately, you could launch each conversion process as a background process, allowing the AppleScript to immediately return and become available for relaunching. The only problem there is that all those processes will be run simultaneously instead of one at a time, which could cause problems with CPU starvation or low memory if you drop a lot of files at once. You can launch a "do shell script" as background by appending " &> /dev/null &" to the end of your command-line section (note the space between this and your original command):

    do shell script "ftp blah blah blah &> /dev/null &"
  3. refulgentis thread starter macrumors regular

    May 14, 2006
    Yeah...I'm brainstorming ways to handle a queue like that. It'll take a significant rewrite to separate the file parsing code from the handing off for conversion code, and then to do the queue parsing on top...but hey, its just a 100 line applescript. :p

    Ahh I don't know why I didn't think of that. Thanks!

Share This Page