Problem with POSIX path

I have an Applescript that utilizes several shell scripts to reference a tab delimited text file, clean up the line ending, separate the values into variables and then use an open source command tool, exiv2, to embed those variables into a folder of images. Everything seems to work fine except I get an error stating that the exiv2 command cannot find the file, though the command looks perfect, and if run in the Terminal works fine.

Here is the script. If someone could look it over and tell me if I am missing something obvious, they would save me some gray hair. Thanks in advance.

tell application "Finder"
    activate
    
    do shell script "/Users/hrgold/Desktop/TheSpace/cleanup.sh"
    
    do shell script "field1=$(cut -f 1 /Users/hrgold/Desktop/TheSpace/newFM_Export.txt)"
    do shell script "field2=$(cut -f 2 /Users/hrgold/Desktop/TheSpace/newFM_Export.txt)"
    do shell script "field3=$(cut -f 3 /Users/hrgold/Desktop/TheSpace/newFM_Export.txt)"
    
    set theFolder to (choose folder with prompt "Choose the folder of image files.")
    set thePath to the POSIX path of theFolder
    set the fileNames to (list folder (theFolder) without invisibles)
end tell
repeat with i from 1 to number of items in the fileNames
    set theName to (item i of the fileNames)
    set theFullPath to the POSIX path of (thePath) & (theName)
    set s to "/opt/local/bin/exiv2  -M " & quoted form of "\"set Iptc.Application2.Headline $field1\" "
    set t to theFullPath
    set s to s & t
    do shell script s
end repeat

cleanup.sh is this:

[code]#!/bin/bash

perl -p -e ‘s/\r/\r\n/g’ </Users/hrgold/Desktop/TheSpace/FM_Export.txt >/Users/hrgold/Desktop/TheSpace/newFM_Export.txt[/code]

First, you don’t need the Finder for any of this, so get rid of the “tell” block. Second, your field variables are only defined in the shell their run in. Each use of do shell script will start a new shell. Look in the Event Log after trying this script:

do shell script "test=Hello; echo $test"
do shell script "echo $test"

Event Log:

I would find a different way of determining that value.

I’m not sure what exactly your script is doing, but you can try something like this:

property newExportFile : "/Users/hrgold/Desktop/TheSpace/newFM_Export.txt"

do shell script "/Users/hrgold/Desktop/TheSpace/cleanup.sh"

choose folder with prompt "Choose the folder of image files."
set thePath to the POSIX path of result
set fileNames to (list folder (POSIX file (result)) without invisibles)

repeat with i from 1 to (count fileNames)
	do shell script "/opt/local/bin/exiv2  -M " & ¬
		quoted form of ("set Iptc.Application2.Headline `cut -f 1 " & newExportFile & "`") & " " & ¬
		quoted form of (thePath & (item i of the fileNames))
end repeat

I see what you mean about about the field variables. Your script works great except for one thing. The Exiv2 command line wants this form:

exiv2 -M “set Iptc.Application2.Headline $field”

with $field being the value you want to embed. With your script what winds up being embedded is:

cut -f 1 /Users/hrgold/Desktop/TheSpace/newFM_Export.txt

Could I run two terminal commands for the same do shell script command and keep the variable?

The next issue is that I have to pass more than one variable to each image file using a different IPTC header. I assume I could just run multiple repeat loops, one after the other for each value I want to embed in a different IPTC header.

Thanks very much for your help.

PS:
This would seem to solve the problem but I now get an error when trying to compile: Syntax Error Expected expression but found “local”

property newExportFile : “/Users/hrgold/Desktop/TheSpace/newFM_Export.txt”

do shell script “/Users/hrgold/Desktop/TheSpace/cleanup.sh”

choose folder with prompt “Choose the folder of image files.”
set thePath to the POSIX path of result
set fileNames to (list folder (POSIX file (result)) without invisibles)

repeat with i from 1 to (count fileNames)
do shell script "field=$(cut -f 1 " & newExportFile & "); echo $field;
set fieldValue to the result
do shell script "/opt/local/bin/exiv2 -M " & ¬
quoted form of (“set Iptc.Application2.Headline & fieldValue”) & " " & ¬
quoted form of (thePath & (item i of the fileNames))

end repeat