Custom Sort

Dear Scripters,

If this puzzle appeals to you please let me know how you would script the following.

I have hundreds of folders with 20 -50 pdfs each and I need to combine them into one pdf per folder. I’m going to use the JoinPDF command line utility but I need to order the pdfs before sending them to JoinPDF via do shell script (i.e. joinPDF destfile file1 file2 file3) (destfile will be the name of the enclosing folder).

So, what should be file 1 is named something like 453finance_review.pdf and file 2 is named 465accountant’s_statement.pdf and file 3 is 469other.pdf. There are frequently more than one of the said files in each folder so that 469other.pdf might be accompanied by 470other.pdf, 471other.pdf and so on.

I’ve tried various sort routines but of course they sort by numerical or alphabetical order as would be expected. I would like to provide a list and have the files ordered according to the list and then I could pass them on to joinPDF.

Any thoughts?

I’m not clear on how exactly you want these sorted. Can you please restate it? (I’m pretty slow, actually :stuck_out_tongue: ). Also, I didn’t know JoinPDF existed - looks very useful.

  • Dan

Thanks for your question and sorry I did not explain clearly.

I have many folders containing the same type of documents with similar names but with different numerical prefixes, like: 876wake_up.pdf, 483eat_dinner.pdf or 893watch_tv.pdf and I need to make one file out of all of them. BUT the numbers preceding the file names don’t necessarily indicate their order (but if the rest of the file name is the same it does ie… 893eat_lunch.pdf, 894eat_lunch.pdf, 895eat_lunch.pdf should be in order in the final document).

So 876wake_up.pdf must come before 895eat_lunch.pdf which comes before 483eat_dinner.pdf (and all others named xxxeat_dinner.pdf).

I’ve been trying to rename the files based on what is in the file name but the problem with that is that I don’t know how many files are in each folder and I don’t know how many of those files have the same ‘root’ name.

I need to find a way to order those files according to a rule or a list, such as rule_list{“wake_up”,“eat_breakfast”,“eat_lunch”,“persecute_enemies”}. Thus: “if the name of item i contains item e in rule_list then …” I don’t know… this is where my knowledge of Applescript breaks down.

I hope this makes it a little clearer.

Mike

Ah, thank you, that makes sense. Would this work?

property cr : ASCII character (13)
property rule_list : {"wake_up", "eat_breakfast", "eat_lunch", "persecute_enemies"}
property destfile : "Macintosh HD:destination folder:destination file"

set lst to list folder alias "Macintosh HD:TEST_AREA:" without invisibles

set saveDelim to text item delimiters
set text item delimiters to " "
set s to ""
repeat with target in rule_list
	set sortString to ""
	repeat with pdf in lst
		if (offset of target in pdf) > 0 then
			set sortString to sortString & pdf & cr
		end if
	end repeat
	if sortString != "" then
		set rslt to do shell script "echo " & sortString & "|sort"
		set tmpL to text items of rslt
		set shellString to (tmpL as text) & " "
	   set s to s & shellString
	end if
end repeat

set cmd to "/path/to/JoinPDF " & (quoted form of POSIX path of destfile) & " " & s
do shell script cmd
set text item delimiters to saveDelim

  • Dan

I just realized that the command in my script above strings together the filenames in the correct order but not as full paths, so something like

property cr : ASCII character (13) 
property rule_list : {"wake_up", "eat_breakfast", "eat_lunch", "persecute_enemies"} 
property destfile : "Macintosh HD:destination folder:destination file" 

set srcFldr to "Macintosh HD:TEST_AREA:"
set lst to list folder alias srcFldr without invisibles 

set saveDelim to text item delimiters 
set text item delimiters to " " 
set s to "" 
repeat with target in rule_list 
   set sortString to "" 
   repeat with pdf in lst 
      if (offset of target in pdf) > 0 then 
         set sortString to sortString & quoted form of posix path of (srcFldr & pdf) & cr 
      end if 
   end repeat 
   if sortString != "" then 
      set rslt to do shell script "echo " & sortString & "|sort" 
      set tmpL to text items of rslt 
      set shellString to (tmpL as text) & " " 
      set s to s & shellString 
   end if 
end repeat 

set cmd to "/path/to/JoinPDF " & (quoted form of POSIX path of destfile) & " " & s 
do shell script cmd 
set text item delimiters to saveDelim 

might work better. (The last script built the string successfully - except for paths - but I haven’t tested this one.)

  • Dan

Dear Dan,

This is certainly more than I expected! And except for some (user induced) errors I’m getting I can tell this is just what I need!!!

One issue when I try to cut and past this into my script editor is the ! character is rejected… so I substituted ‘is not equal to’ .

I also get the following applescript error:
“sh: -c: line 1: unexpected EOF while looking for matching `‘’
sh: -c: line 2: syntax error: unexpected end of file”

I think there is a missing ’ at the end of ‘do shell script cmd’

Yet when I cut and paste the out put from the event log before the error terminal makes the file… (but with permission denied errors on a few of the files ?) I am sure these are things I can workout latter. (got to take the wife to school :? )

Thanks again - now I have to study it an try to figure your script out!!

Mike

You’re very welcome. I forgot to mention the != not translating properly but obviously you figured that out yourself, and yeah, maybe there’s a missing quote in there. Please let me know if you still don’t get this working because I’m sure it should with any typos removed. BTW, I prefer persecuting my enemies between breakfast and lunch but I guess that’s just me. :stuck_out_tongue:

  • Dan

Thanks again Dan!!

For those of you spectating or in search of a similar solution Dan’s script works wonderfully!

I did need to change the line:
set sortString to sortString & quoted form of POSIX path of (srcFldr & pdf) & cr

to remove the control character at the end… this character needed to be replace with ‘space’ like so:
set sortString to sortString & quoted form of POSIX path of (srcFldr & pdf) & space

I think this makes the first line unnecessary as well.

Many thanks (again) to Dan and the good people here at MacScripter!