Importing Twitter data (images, media, text) from CSV file

I have a large amount of data pulled from twitter (jpg, mp4) stored in a csv file with metadata, along with entries with no media (just tweets). I am hoping to find a way to automate importing the files with the metadata from the spreadsheets into DT.

I found a previous thread that somewhat covers it, but I’m running the applescript and getting no results. I am highlighting the csv file in DT as I’m running it in Script Editor.

This is an example of the spreadsheet:

This is the script I’m currently using:

set docsPath to "/Users/me/folder"
tell application id "DNtp"
	set doc to (item 1 of (selection as list))
	set docHeaders to (columns of doc)
	set md1 to (item 2 of docHeaders) as string
	set md2 to (item 3 of docHeaders) as string
	set md3 to (item 4 of docHeaders) as string
	set md4 to (item 5 of docHeaders) as string
	set md5 to (item 6 of docHeaders) as string
	set md6 to (item 7 of docHeaders) as string
	set md7 to (item 8 of docHeaders) as string
	set md8 to (item 9 of docHeaders) as string
	set md9 to (item 10 of docHeaders) as string
	set md10 to (item 11 of docHeaders) as string
	set md11 to (item 12 of docHeaders) as string
	set md12 to (item 13 of docHeaders) as string
	set docContents to (cells of doc)
	repeat with csvItem in docContents
		set newRecord to import (docsPath & (item 1 of csvItem)) to current group
		add custom meta data (item 2 of csvItem) for md1 to newRecord
		add custom meta data (item 3 of csvItem) for md2 to newRecord
		add custom meta data (item 4 of csvItem) for md3 to newRecord
		add custom meta data (item 5 of csvItem) for md4 to newRecord
		add custom meta data (item 6 of csvItem) for md5 to newRecord
		add custom meta data (item 7 of csvItem) for md6 to newRecord
		add custom meta data (item 8 of csvItem) for md7 to newRecord
		add custom meta data (item 9 of csvItem) for md8 to newRecord
		add custom meta data (item 10 of csvItem) for md9 to newRecord
		add custom meta data (item 11 of csvItem) for md10 to newRecord
		add custom meta data (item 12 of csvItem) for md11 to newRecord
		add custom meta data (item 13 of csvItem) for md12 to newRecord
	end repeat
end tell

Any help would be greatly appreciated!

Please enclose code in code fences to make it easier to read and use:
```
code goes here
```

You might want to run the script in Script Editor and watch the Apple Events tab there. It will tell you what exactly is happening and what does not work as expected.

1 Like

What’s the URL of this thread?

My initial response was wrong, because I was not aware that cell captures the whole line; having experimented, I now know it does; I’ll leave my initial response here, and have added what I think the problem is at the bottom of this response.

Going through your script, the following seems weird to me:

repeat with csvItem in docContents followed by various item x of csvItem would imply that csvItem consists of a multitude of items. However, csvItem is set to be a cell of doc; even assuming (which I cannot test) that csvItem contains the contents of that cell, your cells appear to only contain one item each. Is it possible you were intending to set docContents to lines of doc rather than cells of doc?

Your file name is not item one of the line (so set newRecord to import (docsPath & (item 1 of csvItem)) to current group needs to be adjusted).

Furthermore, I’m not sure your line set docsPath to “/Users/me/folder” doesn’t want an additional / at the end (if folder is actually the name of a folder and not part of the filename, then a / at the end is required).

Note that in my testing, setting mdx to an item of docHeaders provided unexpected results: the column header is augmented by “#text”, so a column called “a” results in mdx “a#text”. If that is universally the case, and you don’t want your metadata headers to contain “#text”, you should clean up mdx before adding custom metadata.

It’s telling me I can’t include links in my post. Here is the second half of the URL (edit by supernat user: I’ve included the whole link below)

The path of the file is most likely invalid, at least according to your screenshot. docsPath doesn’t have a trailing slash and the filename is not stored in the first column (according to your screenshot at least).

I said that yesterday :stuck_out_tongue:

Well… obviously I didn’t read every sentence of the whole thread :laughing:

Well… I did ramble on a bit :crazy_face:

Oof, forgot to post an update. I ended up figuring it out from things said here (a mix of the file path, strange formatting in the rest of my spreadsheet, and reducing the amount of custom metadata fields) and it now works fine 95% of the time. Here is the final code for documentation purposes:

set docsPath to "/Users/MyName/MyFilePath/"
tell application id "DNtp"
	set doc to (item 1 of (selection as list))
	
	set docHeaders to (columns of doc)
	set md1 to (item 2 of docHeaders) as string
	set md2 to (item 3 of docHeaders) as string
	set md3 to (item 4 of docHeaders) as string
	set md4 to (item 5 of docHeaders) as string
	
	
	set docContents to (cells of doc)
	
	repeat with csvItem in docContents
		set newRecord to import (docsPath & (item 1 of csvItem)) to current group
		
		add custom meta data (item 2 of csvItem) for md1 to newRecord
		add custom meta data (item 3 of csvItem) for md2 to newRecord
		add custom meta data (item 4 of csvItem) for md3 to newRecord
		add custom meta data (item 5 of csvItem) for md4 to newRecord
		
	end repeat
end tell

Still need to figure out how to input the individual lines of text as their own separate entry, but that seems like another problem entirely.