On Open theItems returns wrong count of items
The On Open block in my applescript is not behaving reliably. I'm dropping up to 8 files on it and sometimes it thinks there are two sets (for example, if I select 8 files and drop on the script application, it thinks there is 1 set of 3 files and one set of 5 files).
It's very frustrating.
I am trying to combine the Finder comments of a small group of files and put the combined set onto a single file. I've written the following script to do that, and it works most of the time. To test it out you have to drag a group of files with Finder comments onto the icon, then after it's done you drag the file you want the combined comments assigned to onto the script app.
The problem is that when you drop 8 files on the script, *sometimes* it runs thru the open block twice with two different sets of files (that combined add up to the dropped set).
This messes up the aggregation of the comments, so when you go to assign the comments to the new file, you only get the second part of the comments.
is that clear? anyway, here's the script. if you see any reason why it would be running the open block more than once when you drop a few files on it, let me know. I'm running Yosemite 10.10.1 on a brand new Mac Mini with a fusion drive. I have seen similar on open problems on my MBA and in Mavericks. Any clues?
-------
property mainComment : ""
property itemList : {}
on open theItems
delay 1
set howMany to count of theItems
-- display dialog howMany
if howMany > 1 then -- when more than 1 file is dropped, combine the Finder comments
set mainComment to ""
set itemList to {}
repeat with theItem in theItems
tell application "Finder"
set myComment to comment of theItem
set wordCount to the number of words in myComment
if wordCount > 1 then
repeat with x from 1 to wordCount
set thisWord to word x of myComment
if itemList does not contain thisWord then
set itemList to itemList & thisWord
end if
end repeat
end if
end tell
end repeat
repeat with myWord in itemList
set mainComment to mainComment & " " & myWord as text
end repeat
else -- when only 1 file is dropped, set the Finder comments
display dialog mainComment
tell application "Finder"
set comment of (item 1 of theItems) to mainComment
end tell
end if
end open