The Mac Observer

Unparsed


AppleScript: How to Identify Files in Progress

July 3rd, 2008 by Stephen Swift

I still get an occasional AppleScript question from people who probably found my old column.  A few weeks ago Paul had a question about copying files.  He works for a TV station, and he is constantly capturing video using Final Cut Pro and dumping it into a scratch directory.  When the transfer is complete, he wants it to automatically move to a work folder.  The problem is there’s no obvious or elegant solution to tell if a file transfer is in progress, such as in a download, copying a file, or like in this case from a video camera.

After a couple of Google searches, I found this promising bit of code using fstat: 
find /Volumes -type f -name ‘*.[pP][sS][dD]‘ \! -exec sh -c “fstat {}|grep -q {}” \; -print | while read FN

Unfortunately, I couldn’t get it to work under my install of Tiger.  If you know of any unix commands or scripts that could identify a state of a file, please share them in the comments.

A pure AppleScript approach would be to use an idle handler to record the size of the item in the item’s comment field.  Every time the script runs, it would compare the recorded size to the current size, and perform an action if the size matched.  It’s a bit of a hack to rely on the comment field, especially if you’re concerned about deleting comments that may or may not already be there.  Here’s a crude script mock-up to help you get started:

on idle
tell application "Finder"
set source_folder to path to desktop folder as alias
set dest_folder to path to home folder as alias
set the item_list to list folder source_folder without invisibles
set source_folder to source_folder as string
end tell
repeat with i from 1 to number of items in the item_list
set this_item to item i of the item_list
set this_item to (source_folder & this_item) as alias
set is_folder to folder of (info for this_item)

if not is_folder then
tell application "Finder"
set FileComment to comment of this_item
set FileSize to size of this_item
end tell
try
set FileComment_num to FileComment as number
on error
set FileComment_num to 0
end try
if FileComment_num < FileSize then
tell application "Finder" to set comment of this_item to FileSize
else
tell application "Finder" to move this_item to dest_folder
end if
end if
end repeat
return 60
end idle

Leave a Reply

Apple Stock Quote

  • AAPL: $96.80. Change Today: +8.06.
  • (Prices delayed up to 20 minutes.)
  • Discuss in our Apple Finance Board
Apple iTunes

Hot Topics

Top Deals From DealsOnTheWeb