Applescript: filenames from folder to append to csv

For my bookkeeping I need to scan and store my receipts as a PDF file, but transfer some information (like amount and category) to an Numbers (Excel)-sheet as well. I'm using TextExpender to include al information needed for the Numberssheet inside the filename, which looks something like this:


UIT4,20130401,23,categoryA,.pdf


I want to transfer all the pdf-filenames in the folder to append to a csv file, so I get:


test.csv

UIT4,201304001,23,categoryA

UIT5,201305002,25,categoryB

UIT2,201306001,22,categoryA


etc.

I can import the csv in Numbers and all is well.


My question for you is how can I use applescript to get all the pdf-filenames in a folder to append to a csv-file?


I've tried this modified script by Pierre L, but I get a error regarding the ending of the file??


set theFolder to choose folder

set theTextFile to POSIX file "/Users/user_name/Desktop/test.csv" -- for example

set theText to readtheTextFile

tell application "Finder"

set theFileNames to name of files of theFolder whose name extension is "pdf"

repeat with thisName in theFileNames

try

set xx to (text 1 through 2 of thisName) as integer

set theNewFileName to paragraph (xx + 1) of theText

if character 3 of thisName is "-" then

set theNewFileName to theNewFileName & character 4 of thisName

end if

set name of filethisName of theFolder to theNewFileName & ".pdf"

end try

end repeat

end tell


If you'd like to help out, that would be great!

iMac, OS X Mavericks (10.9)

Posted on Nov 24, 2013 1:30 AM

Reply
8 replies

Nov 24, 2013 2:45 PM in response to Michiel78

Your request is pretty simple. Your example is not.


For example:


I want to transfer all the pdf-filenames in the folder to append to a csv file, so I get:


test.csv

UIT4,201304001,23,categoryA

UIT5,201305002,25,categoryB

UIT2,201306001,22,categoryA


What does 'UIT4,201304001,23,categoryA' represent? is that an entire file name? or is that some additional data wrapped around the file name? If there's additional data there, what does that data represent? Where does it come from?


If I assume for now that it *is* the file name, what was it about Pierre's script that you thought was relevant? Nothing there seems related to writing a set of filenames to a text file.


Before we get to that part, let's talk about the file itself. Is this a pre-existing file with pre-existing data that teyou are appending data to the end of, or is it a blank slate that gets cleared each time the data is imported into Numbers? That's kind of important to know.


Given that there are so many unanswered questions it's impossible to answer your question directly. However, here's an example that meets the original brief - it may, of course, need some changes as the requirements are fleshed out:


set theFolder to (choose folder)


tell application "Finder"

set csvFile to open for access (file ((path todesktopastext) & "my.csv")) with write permission


set eofcsvFileto 0 -- remove this line to append data

set theFiles to every file of theFolder whose name extension is "pdf"

repeat with eachFile in theFiles

set fn to name of eachFile

write (fn & return) as text to csvFile

end repeat


close accesscsvFile

end tell

Nov 24, 2013 11:07 PM in response to Camelot

Hi Camelot,


Thank you for your excellent help, and I'm sorry I didn't make the example more clear.

Although the shell script I found later does pretty well what I want (see my 3rd post, 4:20AM), yours is an even more elegant solution as it is better human-readable and can be modified easier.


What I do: I scan a receipt and I manually name this file using a specific name-structure (via TextExpander), and save it as PDF, something like this:


[OUT#]_[RECEIPT DATE]_[AMOUNT]_[CATEGORY]_[REMARKS].pdf


So for example for every scanned receipt I create a filename like:

OUT4_20131001_45_catA_testremark.pdf


The reason I use this structure is that the info in it is needed for filling in a Numbers-sheet my accountant provides. Now, I have to do double work, scanning and naming the receipts, and filling in the accountant Numbers sheet, all by hand.


So I figured if I could transfer all the filenames in a folder to a CSV file, and import that into the Numbers sheet, it reduces the workload as I only have to type in the data once (when saving the pdf). That's where applescript comes in. So my workflow now:


1.First I let Hazel replace all "_" with ";" in the original PDF-files.

2.Then Hazel'll run your Applescript which creates the CSV file (collecting data as OUT4;20131001;45;catA;testremark).

3.After that, Hazel replaces all ";" back to "_" in the original PDF-files.

4.I manually import the CSV file into Numbers and copy paste from there.


I'll need to do this say 4 times a year, so I don't think more automation is necessary. Or perhaps you could write the replacement for "_" into ";" in the Applescript itself.


Some more questions about your script:

- the script lets me choose a folder in a dialog box, is it also possible to direct it to a pre-specified folder?

- is it possible to let the script create a new CSV-file named OUT4_[current date].csv?



But thanks again for your excellent help Camelot. In essence, I've got what I need now.



(I misunderstood Pierre's script; I see now it renames original files based on data provided in a txt-file; which is not what I need 🙂 )




Nov 24, 2013 11:39 PM in response to Michiel78

Or perhaps you could write the replacement for "_" into ";" in the Applescript itself.


That's pretty easy using AppleScript - that way you could leave the file names intact and just change them before writing the data to the file. There are several ways of doing this and, unless you really want, I won't delve deep into the process of using text item delimiters, but an example script is below.


- the script lets me choose a folder in a dialog box, is it also possible to direct it to a pre-specified folder?


Of course. The first line is the one that triggers the Choose Folder dialog:


set theFolder to (choose folder)

You can just change this to any folder on disk by providing its path:


set theFolder to "Macintosh HD:Users:username:Desktop:My folder"


and making sure you tell the Finder that this represents a folder path:


set theFiles to every file of folder theFolder whose name extension is "pdf"


(note the addition of the 'folder' keyword.


- is it possible to let the script create a new CSV-file named OUT4_[current date].csv?


Yes, but....


The third line in the script creates the output file.


set csvFile to open for access (file ((path todesktopastext) & "my.csv")) with write permission


You can call it whatever you like - replace 'my.csv' with the desired file name. Just be careful in putting formatted dates into filenames since characters like the '/' in '11/24/2013' will be confusing since the / is used as a directory delimiter ('11/24/2013' would normally indicate the file called '2013' inside the '24' folder that is inside the '11' folder). Date formatting in AppleScript can be tricky, but its certainly do-able. How do you want it formatted?


Revised script that incorporates replacing underscores with commas:



set theFolder to "Macintosh HD:Users:you:Desktop:Some folder"


tell application "Finder"

set csvFile to open for access (file ((path todesktopastext) & "my.csv")) with write permission


set eofcsvFileto 0 -- remove this line to append data

set theFiles to every file of folder theFolder whose name extension is "pdf"

repeat with eachFile in theFiles

set fn to name of eachFile

set csvData to my replaceChars(fn, "_", ",")


write (csvData & return) astexttocsvFile

end repeat


close accesscsvFile

end tell


on replaceChars(inputFileName, oldChar, newChar)

set {tid, my text item delimiters} to {my text item delimiters, oldChar}

set newfilename to text items of inputFileName

set my text item delimiters to newChar

set newfilename to newfilename as text

set my text item delimiters to tid

return newfilename

end replaceChars





This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript: filenames from folder to append to csv

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.