I would like to turn the contents(filenames) of a folder into a .csv

My problem is that the files are organised in sets. I only want to output the filename of each set without anything else but the first numbers.

Desired Output:


Example of files in folder:

  • 10405a.jpg
  • 10405b.jpg
  • 10405c.jpg

  • 160405a.jpg

  • 160405b.jpg
  • 160405c.jpg

  • 2002035-1.jpg

  • 2002035-2.jpg
  • 2002035-3.jpg

I found some code that i modified with no luck. I assume i have to loop through the files and check for any non-digit characters and pars that somehow. Any help would be much appreciated.

set src to (choose folder with prompt "Choose source folder where source tree is rooted at")'s POSIX path
    set theCsv to ""
    repeat with i from 1 to number of items in src
        tell application "Finder" to set this_item to displayed name of item i of src
        set this_item to this_item & ",\\n"
        set theCsv to theCsv & this_item
    end repeat
    do shell script "echo " & quoted form of theCsv & " > ~/Desktop/names.csv"
  • If this is a one time thing I'd just use Terminal and cd to the target directory and, assuming it contains only the target files, use the following compound command: echo 'Object #' > "$HOME/Desktop/names.csv"; ls | grep -Eo '^[0-9]+' | sort -u >> "$HOME/Desktop/names.csv" Commented Mar 20, 2020 at 12:41
  • @ user3439894 Thanks! That works great. I will have to do this on a weekly basis. The directory will only contain these target. Perhaps I can implement this in some sort of AppleScript and run it every week.
    – oatfoolery
    Commented Mar 20, 2020 at 15:08
  • Another thing, this Target Directory is on a local Server.
    – oatfoolery
    Commented Mar 20, 2020 at 15:22

1 Answer 1


You could try the following example AppleScript code:

set src to quoted form of ¬
    (choose folder with prompt "Choose source folder where source tree is rooted at")'s POSIX path

set shellCommand to ¬
    "cd " & src & " && echo 'Object #' > \"$HOME/Desktop/names.csv\"; ls | grep -Eo '^[0-9]+' | sort -u >> \"$HOME/Desktop/names.csv\""

do shell script shellCommand

Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

  • Amazing! Thanks. Modified it a bit and got it to work on my mounted volume.
    – oatfoolery
    Commented Mar 20, 2020 at 16:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .