Importing .csv as Rich Text

As far as I know csv is plain text. If that’s true are you aware of that?

my understanding is that the csv will contain e.g., “apple, green, pear, green, orange, orange, plum, purple” and is to be split into one rtf for each line, so an rtf called “apple” and containing “green”, an rtf call “plum” and containing “purple” and so on; if I am correct then the fact that a csv is plain text has no immediate bearing, or? I’m not completely sure that’s what the OP meant though, which is why I asked for clarification.

1 Like

My use case is monthly .csv transaction files from a bank
I use an applescript to parse it into separate notes

Thank you!

!

Yes, perhaps RTF was a red herring: I would like the ability to add rich text edits later, within DT, but will only be importing plain text. (Though, it will have line breaks. Does that make it RTF?)

Here are two screenshots:

  • One is the type of Excel sheet I have. It is image file names (in column A) and then the transcription of the image (in column B).
  • One is a result I did manually: I want the RTF file in DT to be named the image file name from the spreadsheet (i.e., column A) and the transcription of that image to be in the content (thus, drawing from column B).

Please let me know if I overlooked anything.

Screen Shot 2021-03-07 at 1.56.06 PM

Here is the second image.

(Fwiw, while these say ‘confidential,’ they are not. They are 50 years old.)

Ok, that is most certainly scriptable. I will offer a script if nobody else does, but my script will either be cumbersome (using offset of “,” and then redefining the text in each round) or I will need to read up about how to use delimiters in AppleScript. Whilst the latter would do me good, perhaps somebody who actually knows will beat me to it :wink:

(edit: I think I can use @pete31 post here to put something together)

1 Like

This first step proves the concept:

tell application id "DNtp"
	set thetext to "apples,green,pears,greenish yellow,oranges,orange,plums,purple"
	set thelist to my tid(thetext, ",")
	set n to 0
	repeat with theEntry in thelist
		if n is equal to 0 then
			set theTitle to theEntry
			set n to 1
		else
			create record with {name:theTitle, type:rtf, content:theEntry} in incoming group of database "test"
			set n to 0
		end if
	end repeat
end tell

-- thanks to the resident expert @pete31 for the following section
on tid(theInput, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set theOutput to text items of theInput
	set AppleScript's text item delimiters to d
	return theOutput
end tid

The above results in this (on my test database, it won’t work for you unless you happen to have a database called “test”):

A couple of questions, though: how do you want to trigger the script? Via a smart rule (which reacts to any csv file, for example)? Or as an a toolbar script based on files which you highlight in DT before triggering the script?

I see that your column B contains entries which in themselves contain commas; so you’d need to provide more information on your csv files: which delimiter do they use? If they use commas, you’d need to do a find/replace on your data prior to export, e.g. replacing every comma with “@” or any other symbol which does not otherwise occur in your text. Then the script could be adapted to replace any occurrence of “@” with “,”.

[Edit: From here: Here are the rules Excel uses to determine how it saves CSV files: 1. If your language / region / locale uses comma (,) for decimal separation then Excel will save using semi-colons 2. If it uses a dot (.) full stop or period, then it will delimit with commas (,).]

Instead of the Applescript text item delimiters, my method is to walk through the characters, breaking on comma (unless the comma is enclosed within quotes)

Excel does not export the text in quotes (on my Mac using my version); so I’m not sure how that would help?

anyway, this second proof of concept also works:

tell application id "DNtp"
	try
		set thetext to "apples,green,pears,greenish@ yellow,oranges,orange,plums,purple"
		set thelist to my tid(thetext, ",")
		set n to 0
		repeat with theEntry in thelist
			if n is equal to 0 then
				set theTitle to theEntry
				set n to 1
			else
				set cleanedEntry to my tid(theEntry, "@")
				set c to 0
				set restoredEntry to ""
				repeat with theContent in cleanedEntry
					if c is equal to 0 then
						set restoredEntry to restoredEntry & theContent
					else
						set restoredEntry to restoredEntry & "," & theContent
					end if
					set c to c + 1
				end repeat
				create record with {name:theTitle, type:rtf, content:restoredEntry} in incoming group of database "test"
				set n to 0
			end if
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

-- thanks to the resident expert @pete31 for the following section
on tid(theInput, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set theOutput to text items of theInput
	set AppleScript's text item delimiters to d
	return theOutput
end tid

Welcome again, @kdonovan11

Seeing as you have the file open in Excel and you only have two columns of data, you could try this…

tell application "Microsoft Excel"
	tell active sheet
		tell used range
            repeat with thisCell from 1 to (count cells) by 2
				my createFile(value of cell thisCell, value of cell (thisCell + 1))
			end repeat
		end tell
	end tell
end tell

on createFile(recName, recContent)
	tell application id "DNtp"
		create record with {name:recName, type:rtf, content:recContent} in incoming group
	end tell
end createFile

1 Like

Jim, I can see you writing. Do not come up with a two-line version of doing this, or I might have to weep :smiley:

2 Likes

damn, off I go :see_no_evil:

1 Like

Haha!
Trust me, scripting Microsoft apps is generally not so simple. Their dictionaries are verbose and very obtuse compared to other apps.

Yeah - I just like to be good at things, and as far a AppleScript is concerned it’s rather apparent who can and who can’t :wink: thanks for the by 2 bit; as you can see from my script, I did solve that lack of knowledge, but knowing how to do it would have produced cleaner code :slight_smile:

Anyway, 'twas fun, thanks for playing :smiley:

I learned a long time ago, life isn’t a competitiion. I’m as good as I am today, with emphasis on as I am. I strive to excel (no pun intended :stuck_out_tongue: ) but don’t compare myself with others. I may use them for inspiration, but not comparision :slight_smile:

1 Like

Don’t you worry, I’m just doing a bit of jolly banter. I enjoy learning from others no end :slight_smile: In this case, for example, I completely failed to consider the possibility of extracting directly from Excel - so I like that bit of lateral thinking (which, as is so often the case, was inhibited in me by a fixation error triggered by the mention of exporting to csv). So thanks again for your input - that is how I learn :slight_smile:

You’re welcome. And maybe once the flood subsides, I can throw my hat into the ring a bit more often. :slight_smile:

1 Like

Amazing. Thank you very much.

Am I correct that this is designed to deposit the new RTF in DT’s “Inbox”? Rather than, say, in the active Database? That’s certainly fine by me – I can manually add them where they need to go, since it will change from task to task – I just want to make sure they are not arriving both in the Inbox and hiding in another Database, as well.

You’re welcome. And it’s directing only to the Global Inbox in its current form. That could be changed but it’s 5am… :sleeping::blush: