• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Labelling identically-named files in two Finder folders
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Labelling identically-named files in two Finder folders


  • Subject: Re: Labelling identically-named files in two Finder folders
  • From: Christopher Nebel <email@hidden>
  • Date: Mon, 31 Jan 2005 13:27:15 -0800

On Jan 31, 2005, at 11:05 AM, has wrote:

peter boardman wrote:
I want to compare two folders and label any files that appeared in both (names the same, not bothered about comparing anything else).
This should work:
tell application "Finder"
set folder1 to folder "Path:To:First:Folder"
set folder2 to folder "Path:To:Second:Folder"
set label of every file of folder1 whose name is in (get name of every file of folder2) to 5
set label of every file of folder2 whose name is in (get name of every file of folder1) to 5
end tell

And so it does, but it (the explanation, not the script) has the disadvantage of skipping straight to the solution. Let's look at this in a bit more detail:


tell application "Finder"
    set l to every window whose collapsed is false
    set p to every item of first item of l
    set q to every item of second item of l
    set pCount to count p
    set qCount to count q
    if pCount ≥ qCount then
        my compareLists(p, q)
    else
        my compareLists(q, p)
    end if
end tell
to compareLists(x, y)
    -- longest list first
    tell application "Finder"
        repeat with i from 1 to count x
            set a to name of item i of x
            repeat with j from 1 to count y
                set b to name of item j of y
                if a is b then
                    set label index of item j of y to 5
                    set label index of item i of x to 5
                    exit repeat
                end if
            end repeat
        end repeat
    end tell
end compareLists

First off, you're trying to find an item with a particular name by looping over all the items and inspecting their names yourself. Never write code to do what the system will do for you. AppleScript lets you use items by name and test for existence, so do so:

to compareLists(x, y)
-- longest list first
tell application "Finder"
repeat with i from 1 to count x
set a to name of item i of x
if exists item named a of y -- this replaces the inner loop.
set label index of item j of y to 5
set label index of item i of x to 5
end if
end repeat
end tell
end compareLists


(You don't technically need the "named", but it makes it a little clearer what's going on.)

Now that the inner loop is gone, you can exploit the fact that it's semantically equivalent whether you check the longer or shorter list. (If a exists in b, then b exists in a.) Therefore, you should scan the shorter list, not the longer one, since that will be faster.

The real way to optimize this sort of thing, however, is to reduce it to a filter (or "whose") clause -- can you describe the items you want in an expression? In this case, you can, and that's what has did above.


--Chris Nebel AppleScript Engineering

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: Labelling identically-named files in two Finder folders (From: has <email@hidden>)

  • Prev by Date: Re: Labelling identically-named files in two Finder folders
  • Next by Date: Re: if there is two monitor, where is the display dialog?
  • Previous by thread: Re: Labelling identically-named files in two Finder folders
  • Next by thread: default apache
  • Index(es):
    • Date
    • Thread