Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Select similar files in a Finder window via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Select similar files in a Finder window via AppleScript
Authored by: MacBeliever on May 06, '10 06:39:56PM

I use this two compare files in two separate windows, selecting similar or different items. Probably needs a little fixing, but it works for me.
global fileListWindow1
global fileListWindow2
global nameListWindow1
global nameListWindow2
tell application "Finder"
activate me
set windowCount to count every window
if windowCount is less than 2 then
display dialog "You need to have at least two windows open"
return
end if
set nameExtensionQuestion to display dialog "Do you want to ignore the name extension when comparing?" buttons {"yes", "no", "cancel"} default button {"yes"}
set theAnswer2ExtensionQuestion to button returned of nameExtensionQuestion
if theAnswer2ExtensionQuestion is equal to "yes" then
set considerNameExtension to true
else if theAnswer2ExtensionQuestion is equal to "no" then
set considerNameExtension to false
end if
activate me
set theQuestion to display dialog "Do you want to select different or similar items?" default button "differences" buttons {"differences", "similarities", "cancel"}
set theReply to button returned of theQuestion
with timeout of 100000 seconds
set oldDelimiters to AppleScript's text item delimiters
set errorList to {}
set fileListWindow1 to {}
set fileListWindow2 to {}
set nameListWindow1 to {}
set nameListWindow2 to {}
activate
set idList to my dockNonDockWindowCount(windowCount)
set idWindow1 to item 1 of idList
set idWindow2 to item 2 of idList

-- have to do this check because in filevault, this fails: tell window id idWindow1
if (get id of window 1) is not idWindow1 then
my switchWindows(idWindow1)
end if
set countWindow1 to count every item of window 1
if (get id of window 1) is not idWindow2 then
my switchWindows(idWindow2)
end if
set countWindow2 to count every item of window 1

--inventory both windows
my switchWindows(idWindow1)
set theFilesi to my getAliases(idWindow1)
set theNamesi to my getNames(theFilesi, considerNameExtension)
my switchWindows(idWindow2)
set theFilesj to my getAliases(idWindow2)
set theNamesj to my getNames(theFilesj, considerNameExtension)
my switchWindows(idWindow1)

-- match and compare things between the windows
if theReply is "differences" then
set choiceIsSimilarities to false
else if theReply is "similarities" then
set choiceIsSimilarities to true
end if
my gatherTargetItems(countWindow1, countWindow2, theNamesi, theNamesj, theFilesi, theFilesj, choiceIsSimilarities)
set AppleScript's text item delimiters to oldDelimiters
activate

--select items in both windows
if (get id of window 1) is not idWindow1 then
my switchWindows(idWindow1)
end if
tell window 1 to select {}
select fileListWindow1
my switchWindows(idWindow2)
tell window 1 to select {}
select fileListWindow2
--set bigList to {}
--set end of bigList to my nameListWindow1 & my nameListWindow2
display dialog "Done"
end timeout
end tell

--lists window items that are either similar or different, depending on user's choice
on gatherTargetItems(countWindow1, countWindow2, theNamesi, theNamesj, theFilesi, theFilesj, choiceIsSimilarities)
tell application "Finder"
if (count theNamesi) is less than 1 then
display dialog "nono"
end if
repeat with counter from 1 to (countWindow1)
set itIsThere to false
repeat with secondCounter from 1 to (countWindow2)
if item counter of my theNamesi is equal to item secondCounter of my theNamesj then
set itIsThere to true
exit repeat
end if
end repeat
if itIsThere is choiceIsSimilarities then
try
set end of fileListWindow1 to item counter of my theFilesi
set end of my nameListWindow1 to item counter of my theNamesi
end try
end if
end repeat
repeat with counter from 1 to (countWindow2)
set itIsThere to false
repeat with secondCounter from 1 to (countWindow1)
if item counter of my theNamesj is equal to item secondCounter of my theNamesi then
set itIsThere to true
exit repeat
end if
end repeat
if itIsThere is choiceIsSimilarities then
try
set end of fileListWindow2 to item counter of my theFilesj
set end of my nameListWindow2 to item counter of my theNamesj
end try
end if
end repeat
end tell
end gatherTargetItems

--gets path to every item in window
on getAliases(windowId)
tell application "Finder"
set everyItem to {}
if (get id of window 1) is not windowId then
my switchWindows(windowId)
end if
if (count window 1) is greater than 1 then
if (get id of window 1) is not windowId then
my switchWindows(windowId)
end if
set everyItem to every item of window 1 as alias list
else
set end of everyItem to (item 1 of window 1 as alias)
end if
ignoring application responses
tell application "Finder" to set selection to {}
end ignoring
end tell
return everyItem
end getAliases

--gets name of every item in window
on getNames(everyItem, considerNameExtension)
set allFiles to {}
repeat with counter in everyItem
tell application "Finder"
set displayedName to get name of (get info for counter)
if considerNameExtension is true then
set displayedName to my bust(displayedName)
end if
set end of allFiles to displayedName
end tell
end repeat
return allFiles
end getNames

--moves second window frontmost
on switchWindows(windowId)
tell application "Finder" to activate
tell application "Finder"
repeat until windowId is equal to (get id of window 1)
my flip()
end repeat
end tell
end switchWindows

-- makes sure windows are active and not minimized in the dock
on dockNonDockWindowCount(windowCount)
tell application "Finder"
set notCollapsedSet to {}
set loopCounter to 0
repeat with counter from 1 to (windowCount)
set windowInfo to get properties of window counter
if collapsed of windowInfo is false then
set loopCounter to loopCounter + 1
set end of notCollapsedSet to id of window counter
if loopCounter is 2 then exit repeat
end if
end repeat
set activeCount to count notCollapsedSet
if activeCount is less than 2 then
display dialog "You must have two windows active. Either open a new window or activate a minimized window from the dock"
tell me to quit
end if
end tell
return notCollapsedSet
end dockNonDockWindowCount

-- switch from one window to another
on flip()
tell application "Finder" to activate
tell application "System Events"
key code 50 using {command down}
end tell
end flip

--converts Finder items into aliases
on finderDocListToAliasList(finderDocList)
if (count finderDocList) is greater than 1 then
set aliasList to {}
repeat with i in finderDocList
set end of aliasList to finderDocToAlias(i)
end repeat
return aliasList
else
return finderDocToAlias(item 1 of finderDocList)
end if
end finderDocListToAliasList

on listThem(theFiles, theNames, myList, nameList)
set end of myList to item counter of theFiles
set end of nameList to item counter of theNames
end listThem

-- gets name of file without the extension
-- kinda copied from Matt Neuburg's book
on bust(s)
set text item delimiters to "."
set pathParts to text items of s
set pathPartsCount to count pathParts
if pathPartsCount is greater than 2 then
set newName to items 1 thru -2 of pathParts
else if pathPartsCount is less than 3 then
set newName to item 1 of pathParts
end if
set newName to newName as string
return {newName}
end bust



[ Reply to This | # ]