Can I pass CSV data to multiple Applescripts?

Hello All!

From what I thought was a simple task, has now spiraled into about 18,000 lines of code. :frowning: I shouldnt moan, without AS there would be no way I could have gotten this far.

I have a TSV that I read with about 280 variables defined in it.

I call the variables as follows because the order of the fields can change:

set {theHeaders, theDatas} to paragraphs of (read file ProcessDATA)
set oTIDs to AppleScript’s text item delimiters # save the original TIDs
set AppleScript’s text item delimiters to {tab} # define the new TIDs
set rowColumns to count text items of theHeaders # extracts the number of columns

repeat with i from 1 to rowColumns
if text item i of theHeaders is “userfirstName” then
set userfirstName to text item i of theDatas
end if

if text item i of theHeaders is "userLastName" then
	set userLastName to text item i of theDatas
end if
  • PLUS 280 Fields…

My question is:

How can I pass the variables of the spreadsheet to other scripts without re-reading the TSV every time in every script?

Due to the huge amount of code lines needed, I keep running into error 2707 overflow, so to read the TSV once would save about 1000 lines of code in each script.

Is it possible to use on run(xxx) in the other scripts?

Any help would be wonderful! I hope I have made myself somewhat clear

You don’t say what version of the OS you are running, but assuming 10.10 or later, it’s easy enough to make a record with all the values. Put this at the start of your script:

use framework "Foundation"
use scripting additions

And then use:

set {theHeaders, theDatas} to paragraphs of (read file ProcessDATA)
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {tab}
set theHeaders to text items of theHeaders
set theDatas to text items of theDatas
set AppleScript's text item delimiters to saveTID
set theRecord to (current application's NSDictionary's dictionaryWithObjects:theDatas forKeys:theHeaders) as record

You can then pass the record, and access values using:

userLastName of theRecord

Edited second snippet to add missing lines.

Hello Shane and thank you for the quick response!!

I know I am being silly here, but in which script do I put which bits.

I am using 10.11. and for some reason I cannot get this to work.

Script1

use framework “Foundation”
use scripting additions

set ProcessDATA to POSIX path of “/Users/xxxxxx/Desktop/IMLS246810.txt” as POSIX file
set {theHeaders, theDatas} to paragraphs of (read file ProcessDATA)
set theRecord to (current application’s NSDictionary’s dictionaryWithObjects:theDatas forKeys:theHeaders) as record

set oTIDs to AppleScript’s text item delimiters # save the original TIDs
set AppleScript’s text item delimiters to {tab} # define the new TIDs
set rowColumns to count text items of theHeaders # extracts the number of columns

repeat with i from 1 to rowColumns
if text item i of theHeaders is “userfirstName” then
set userfirstName to text item i of theDatas
end if

if text item i of theHeaders is "userLastName" then
	set userLastName to text item i of theDatas
end if

set AppleScript’s text item delimiters to “/”

do shell script “osascript /Users/xxxxxx/Desktop/test2.scpt”

Script2
use framework “Foundation”
use scripting additions
display dialog userLastName of theRecord

A couple of things.

First, use the Applescript button at the top of the window to format any scripts you post – it makes them easier to read and test.

Second, there’s no need to use osascript. If you want to run other scripts, use run script.

So here’s a sample second script:

on run {theRecord}
	set theValue to userLastName of theRecord
	display dialog theValue
end run

And here’s a script to read in the data, then run the above script, passing it the data:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theFile to alias "Macintosh HD:Users:shane:Desktop:Sample.txt"
set {theHeaders, theDatas} to paragraphs of (read theFile)
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {tab}
set theHeaders to text items of theHeaders
set theDatas to text items of theDatas
set AppleScript's text item delimiters to saveTID

set theRecord to (current application's NSDictionary's dictionaryWithObjects:theDatas forKeys:theHeaders) as record
set script2 to alias "Macintosh HD:Users:shane:Desktop:Script 2.scpt"
run script script2 with parameters {theRecord}

Thank you so much for pointing me in this direction! You are AWESOME!

What a wonderful time saver on reading the data

I created a basic TSV and it worked perfectly! I cannot thank you enough!

I am getting an error however on my big TSV

error “*** -[NSDictionary initWithObjects:forKeys:]: count of objects (364) differs from count of keys (363)” number -10000

This error is because I have 363 fields in my spreadsheet and it is trying to read a blank/null in 364 I beleive

is there a way to stop this? or is it based in my spreadsheet creation via shell concatenate (‘cat’) do you think?

Empty objects should be fine. The only restrictions are that you must have the same number of entries in both paragraphs, which your error suggests you don’t, and each value in the header paragraph must be unique.

Gotchya!

I truly cannot thank you enough Shane! You have literally saved me days.