Wednesday, October 17, 2018

#1 2018-10-12 01:04:41 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 15

Check index numbers of data in two lists

I have a question about how best to do somethng with lists.

I have two lists of file aliases and all of the files are located in one folder. The first list is of files selected in the Finder (selectedFiles) and the second list is of all files in the folder (allFiles).

I need to do the following:

1. Get the item number of item 1 of selectedFiles within allFiles.

2. Get the item number of item 2 of selectedFiles within allFiles

3. Repeat step 2 for the remainder of the selectedFiles (i.e. item 3 through item -1 of selectedFiles).

4. Prompt the user if the item number obtained in step 1 is higher than any of the item numbers obtained in steps 2 and 3.

I wrote the following which works but I have to think there's a simpler method. Any suggestions are appreciated.

Applescript:


on checkFileOrder()
   --Set variable to index number of the first-selected file.
   repeat with a from 1 to count allFiles
       if item a of allFiles = item 1 of selectedFiles then
           if a = 1 then
               return
           else
               exit repeat
           end if
       end if
   end repeat
       --Check the index numbers of the rest of the selected files.
   repeat with b from 2 to count selectedFiles
       repeat with c from 1 to count allFiles
           if item c of allFiles = item b of selectedFiles then
               if c < a then
                   display dialog "Files selected out of order." buttons {"OK"} ¬
                       cancel button 1 default button 1 with title "PDF Add" with icon stop
               else
                   exit repeat
               end if
           end if
       end repeat
   end repeat
end checkFileOrder

Last edited by peavine (2018-10-13 06:49:48 pm)

Offline

 

#2 2018-10-14 03:48:48 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4690

Re: Check index numbers of data in two lists

Hi peavine.

I'm not sure exactly what you want to do or why, since the order of items in a folder is no more to be relied upon than the order of items in a selection. But for the sake of the exercise, if I'm right in thinking you just want to check that the selected items are in the same order in both lists, a simple solution would be:

Applescript:

tell application "Finder"
   set allFiles to files of desktop as alias list
   set selectedFiles to selection as alias list
end tell

checkFileOrder(selectedFiles, allFiles)

on checkFileOrder(selectedFiles, allFiles)
   -- Build a list containing the selected files in the order they appear in allFiles.
   set matchedFiles to {}
   repeat with i from 1 to (count allFiles)
       set thisFile to item i of allFiles
       if (thisFile is in selectedFiles) then set end of matchedFiles to thisFile
   end repeat
   
   -- If matchedFiles and selectedFiles aren't exactly the same (size, contents, and order), tell the user.
   if (matchedFiles is not equal to selectedFiles) then
       display dialog "Files selected out of order." buttons {"OK"} ¬
           cancel button 1 default button 1 with title "PDF Add" with icon stop
   end if
end checkFileOrder

But practically, you may find it more useful to force the lists to be in the same order like so:

Applescript:

tell application "Finder"
   set allFiles to (sort files of desktop by name)
   set selectedFiles to (sort selection by name)
end tell

In this case, the results are lists of Finder references rather than of aliases, so you'd need to use the Finder to interpret them, at least initially. And you'd have to index the items by their positions in the lists rather than in the folder, but that's a better idea anyway.  smile


NG

Offline

 

#3 2018-10-14 02:49:45 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 15

Re: Check index numbers of data in two lists

Thanks Nigel. Your first suggestion worked perfectly in my existing script. I then rewrote the script along the lines in your second suggestion and that was even better--much cleaner and not prone to error. I use this script a lot, so it's nice to get it working well.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)