Importing .csv as Rich Text

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:

:slight_smile:

I did try to export the new RTF as a Word document (so I could then print the results, all together), but received this error:

Screen Shot 2021-03-08 at 9.52.47 AM

Might not be an issue relate to this thread, but thought I’d flag it, just in case.

As a workaround, MS Word will happily open RTF, so no need to export.

Given the exotic naming convention (extension .jpg instead of .RTF) and Microsoft’s known fondness of said extension conventions…

You may of course use any extension you want, even .zzz or .hör if that pleases you. However, there’s a bunch of software out there that relies on the extension to identify the type of data contained in a file.

And there are humans who might find it easier if the extension matches the data in the file.

I noticed that, too; it would be super easy to add to the script to change .xyz to .rtf - @kdonovan11 is that required?

Piggy-backing off of this…

Say that I have an excel spreadsheet of nine (or more) columns, and I’d like to create text (preferably markdown) files with each of the rows where the name of the file pulls from a couple of the fields and the content of the file has everything in the format “field: value” [new line], plus additional text. Would that be much more complicated?

For example:

row 681 Isaac M. WADE 3658 1815 great (x4) great-aunt’s husband -6 Wade 3764

The file called “3658 Isaac M. Wade” with contents

Isaac M. Wade (Family Historian ID: 3658)

Record Id: 3658
Individual: Isaac M. Wade
Date of Birth: 1815
Relationship: great (x4) great-aunt’s husband
Generation: -6
Father: Wade
Father’s Record Id: 3764
Mother:
Mother’s Record Id:

Notes

Related Files

Footnotes/Works Cited