Grep is a tool that prints lines matching a pattern. To perform substitutions use instead commands like sed, awk, perl or ruby.
set p2file to "/path/to/abc.ini"
set rstring to " my replacement string"
-- makes an "in-place" edit of the .ini file & generates a backup of the original file with extension ".bak"
do shell script "perl -p -i.bak -e 's/(.*\\/cppr\\.width).*/\\1" & rstring & "/' " & quoted form of p2file
-- makes an "in-place" edit without backup
--do shell script "perl -pi -e 's/(.*\\/cppr\\.width).*/\\1" & rstring & "/' " & quoted form of p2file
set p2file to ((path to desktop as Unicode text) & "abc.ini")
set s_string to "/cppr.width"
set r_string to " my replacement string"
try
set {TID, text item delimiters} to {text item delimiters, s_string}
set ff to open for access file p2file with write permission
set theData to read ff
set theData to text items of theData
if (count theData) = 2 then
set theData to item 1 of theData & s_string & r_string
set eof of ff to 0
write theData to ff starting at eof
end if
close access ff
on error
try
close access file p2file
end try
end try
set text item delimiters to TID
Hello,
this is probably due to the fact that your file uses carriage-returns instead of line-feeds. So you have to convert them.
… this work for me with a CR delimited file…
set res to do shell script "perl -i.bak -pe 's/\\r/\\n/g; s/(.*\\/cppr\\.width).*/\\1" & rstring & "/' " & quoted form of p2file
…Mike, this is Sed’s normal behavior… it doesn’t make any “in-place” substitution, his result is send to standard output.
You have to redirect the standard output to a new file
set p2input to POSIX path of ((path to desktop as Unicode text) & "abc.ini")
set p2output to POSIX path of ((path to desktop as Unicode text) & "abc_new.ini")
set rstring to " my replacement string"
set res to do shell script "tr '\\r' ' \\n' <" & space & quoted form of p2input & " | sed -E 's/(.*\\/cppr\\.width).*/\\1" & rstring & "/' >" & space & quoted form of p2output
set a to paragraphs of (read (choose file))
set {TID, text item delimiters} to {text item delimiters, linefeed}
set a to a as text
set text item delimiters to TID
a
Note: linefeed has been introduced in Leopard, in Tiger use ASCII character 10
It’s fine, but now… how to to pass “var a” to find/replace functions?
tell application "Adobe Illustrator"
set aD to document 1
set w to width of aD
set h to height of aD
end tell
set myFile to (POSIX file "/Own/separationsX1")
set a to paragraphs of (read myFile)
set {TID, text item delimiters} to {text item delimiters, linefeed}
set a to a as text
set text item delimiters to TID
mySizes("cppr.height", h)
mySizes("cppr.width", w)
mySizes("ppr.height", h)
mySizes("ppr.width", w)
mySizes("ppr.image.right", w)
mySizes("ppr.image.bottom", h)
on mySizes(s, r)
try
set {TID, text item delimiters} to {text item delimiters, s}
set ff to open for access myFile with write permission
set theData to read ff
set theData to text items of theData
if (count theData) = 2 then
set theData to item 1 of theData & s & r
set eof of ff to 0
write theData to ff starting at eof
end if
close access ff
on error
try
close access myFile
end try
end try
set text item delimiters to TID
end mySizes
sorry, I read EOF instead of EOL.
what’s about this “conventional” solution
tell application "Adobe Illustrator"
tell document 1 to set {w, h} to {width, height}
end tell
try
set myFile to (POSIX file "/Own/separationsX1") as alias
set ff to open for access myFile with write permission
set theLines to paragraphs of (read ff)
repeat with aLine in theLines
if aLine contains "cppr.height" then
set contents of aLine to stripString(aLine, "cppr.height", h)
else if aLine contains "cppr.width" then
set contents of aLine to stripString(aLine, "cppr.width", w)
else if aLine contains "ppr.height" then
set contents of aLine to stripString(aLine, "ppr.height", h)
else if aLine contains "ppr.width" then
set contents of aLine to stripString(aLine, "ppr.width", w)
else if aLine contains "ppr.image.right" then
set contents of aLine to stripString(aLine, "ppr.image.right", w)
else if aLine contains "ppr.image.bottom" then
set contents of aLine to stripString(aLine, "ppr.image.bottom", h)
end if
end repeat
set {TID, text item delimiters} to {text item delimiters, linefeed}
set theLines to theLines as text
set text item delimiters to TID
set eof of ff to 0
write theLines to ff starting at eof
close access ff
on error
try
close access myFile
end try
end try
on stripString(listElement, s, r)
set o to ((offset of s in listElement) - 1)
if o = 0 then
return r
else
return text 1 thru o of listElement & r
end if
end stripString
tell application "Adobe Illustrator"
tell document 1 to set {w, h} to {width, height}
end tell
try
set myFile to (POSIX file "/Own/separationsX1") as alias
set ff to open for access myFile with write permission
set theLines to paragraphs of (read ff)
repeat with aLine in theLines
if aLine contains "cppr.height" then
set contents of aLine to stripString(aLine, "cppr.height ", h)
else if aLine contains "cppr.width" then
set contents of aLine to stripString(aLine, "cppr.width ", w)
else if aLine contains "ppr.height" then
set contents of aLine to stripString(aLine, "ppr.height ", h)
else if aLine contains "ppr.width" then
set contents of aLine to stripString(aLine, "ppr.width ", w)
else if aLine contains "ppr.image.right" then
set contents of aLine to stripString(aLine, "ppr.image.right ", h)
else if aLine contains "ppr.image.bottom" then
set contents of aLine to stripString(aLine, "ppr.image.bottom ", w)
end if
end repeat
set {TID, text item delimiters} to {text item delimiters, linefeed}
set theLines to theLines as text
set text item delimiters to TID
set eof of ff to 0
write theLines to ff starting at eof
close access ff
on error
try
close access myFile
end try
end try
on stripString(listElement, s, r)
set o to ((offset of s in listElement) - 1)
if o = 0 then
return s & r
else
return text 1 thru o of listElement & s & r
end if
end stripString