Decoding number in file name

I’m very new to scripting, and have gotten stumped on my first project.

I receive files from a customer with names like:
205_1.pdf
299_1.pdf
355_2.pdf
277_2.pdf
etc.

The ‘_’ precedes the quantity of the files they want me to print. If the file is 263_3, I need to write a script that will make two additional copies of that file, naming them 263_3_2 and 263_3_3 or something like that, into the same folder. Then I can auto print a whole folder with the correct quantity from Adobe Acrobat using the batch processing. Any help would be greatly appreciated.

This seems to work… Ask if it doesn’t make sense. :slight_smile:

-- This script assumes that the chosen folder contains only files
-- and that their names all follow the format: "<anything>_<number>.<extension>".

set myFolder to (choose folder)
set fileNames to list folder myFolder without invisibles

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."

repeat with thisName in fileNames
  -- Convert each name to plain text, where an underscore counts as a separate word
  set thisName to thisName as string
  -- Separate the extension from the rest of the name
  set {fName, fExtn} to thisName's {text 1 thru text item 2, text item -1}
  try
    -- The last word before the extension should indicate the number of copies
    set copies to (word -1 of fName) as integer
    -- Create any necessary duplicates and rename them
    repeat with i from 2 to copies
      tell application "Finder"
        duplicate file thisName of myFolder
        set name of result to fName & "_" & i & "." & fExtn
      end tell
    end repeat
  end try
end repeat

set AppleScript's text item delimiters to astid

Oops! Misprint. Can’t be my fault… :wink: Here’s the script again:

-- This script assumes that the chosen folder contains only files
-- and that their names all follow the format: "<anything>_<number>.<extension>".

set myFolder to (choose folder)
set fileNames to list folder myFolder without invisibles

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."

repeat with thisName in fileNames
  -- Convert each name to plain text, where an underscore counts as a separate word
  set thisName to thisName as string
  -- Separate the extension from the rest of the name
  set {fName, fExtn} to thisName's {text 1 thru text item -2, text item -1}
  try
    -- The last word before the extension should indicate the number of copies
    set copies to (word -1 of fName) as integer
    -- Create any necessary duplicates and rename them
    repeat with i from 2 to copies
      tell application "Finder"
        duplicate file thisName of myFolder
        set name of result to fName & "_" & i & "." & fExtn
      end tell
    end repeat
  end try
end repeat

set AppleScript's text item delimiters to astid

That works perfectly. Thank you very much, Nigel. I can follow most of it, except the line “set {fName, fExtn} to thisName’s {text 1 thru text item -2, text item -1,” which will give me something to look up.
I bought the O’Reilly book on “Definitive Applescript” but it seemed like a guide for people who already knew other languages and programming conventions, that just wanted to learn Applescript’s differences, rather than non-programmers trying to streamline their workflows. But I’m going to keep trying to learn this. Again, thank you!
-Brad