Applescript for multiple find/replace using separate masterlist

applescript

I am trying to create a script to extract a list of data from column A on document 1, and then use columns A & B on document 2 as multiple find/replace data.

Everything works in so far as grabbing the data required but I don't understand how to write the script to do the find/replace. I want it to run through the entire list from document 1 whilst referencing the first row of the find/replace list (document 2), and then to do the same with row 2, and so on until it reaches the end of document 2.

You will see how I have tried to execute this at the bottom but I don't understand how this should be written. I'd really appreciate some pointers here if anyone would care to help me.

-- set paths

tell application "Finder"

set folderpath to folder "Macintosh HD:Users:Will:Desktop:Data1"
set filePath to first file of folderpath as alias
set folderpath2 to folder "Macintosh HD:Users:Will:Desktop:Data2"
set filePath2 to first file of folderpath2 as alias

end tell


-- grab data


set fileRefr to (open for access filePath)
set theText to (read fileRefr)
set textList to paragraphs of theText
close access fileRefr

set uniqueList to {}
set dataList to every paragraph of (do shell script "cat " & quoted form of POSIX path of filePath & " | awk -F'" & tab & "' 'BEGIN{getline}{print $1}'")
set uniqueList to uniqueList & dataList


set fileRefr2 to (open for access filePath2)
set theText2 to (read fileRefr2)
set textList2 to paragraphs of theText2
close access fileRefr2
set theCNT to (count of textList2) - 1

set uniqueList2 to {}
set dataList2 to every paragraph of (do shell script "cat " & quoted form of POSIX path of filePath2 & " | awk -F'" & tab & "' 'BEGIN{getline}{print $1}'")
set uniqueList2 to uniqueList2 & dataList2

set uniqueList3 to {}
set dataList3 to every paragraph of (do shell script "cat " & quoted form of POSIX path of filePath2 & " | awk -F'" & tab & "' 'BEGIN{getline}{print $2}'")
set uniqueList3 to uniqueList3 & dataList3


-- find/replace data


repeat with i from 1 to count theCNT
set uniqueList to (replace_chars of uniqueList from (item i of uniqueList2) to (item i of uniqueList3))
end repeat

Best Answer

You must use the text from the first document instead of putting it into a list.

Put the searching text and replacing text from the second document into a list.

Like this script :

tell application "Finder"
    set filePath to (first file of folder "Data1" of desktop) as alias
    set filePath2 to (first file of folder "Data2" of desktop) as alias
end tell
-- get unique text from the first column
set uniqueText to do shell script "awk -F'" & tab & "' 'BEGIN{getline}{print $1}' " & quoted form of POSIX path of filePath

-- get unique text from the first column, but print the first and the second column to get the searching text and the replacing text (pair), this put the awk's result into a list
set uniqueList2 to paragraphs of (do shell script " awk -F'" & tab & "' 'BEGIN{getline}{print $1\"\\t\"$2}' " & quoted form of POSIX path of filePath2)

set uniqueList to my replace_chars(uniqueText, uniqueList2) -- find/replace data, put the paragraphs into a list

to replace_chars(t, L2)
    set tid to text item delimiters
    set tc to count L2
    repeat with i from 1 to tc
        set text item delimiters to tab
        set {toFind, toReplace} to text items of (item i of L2) -- get items separated by tab from a item in uniqueList2)
        set text item delimiters to toFind
        set {L, text item delimiters} to {text items of t, toReplace}
        set t to L as text
    end repeat
    set text item delimiters to tid
    return paragraphs of t
end replace_chars