My Apple scripts to replace M3 and M5 commands with M10/M11 and M9 to M5 at the end of the code. I tried doing all of this in one script but could not figure it out.
set textToFindList to {“M3”, “M5”}
set textToReplaceWithList to {“M11 P2 S100”, “M10 P2”}
if (length of textToFindList) is not equal to ¬
(length of textToReplaceWithList) then return
set theFile to choose file
– tell application “Finder” to duplicate file theFile with exact copy
set theText to read theFile as «class utf8»
repeat with i from 1 to length of textToFindList
set theText to my findAndReplaceInText(theText, ¬
item i of textToFindList, ¬
item i of textToReplaceWithList)
end repeat
my writeToFile(theText, theFile, true)
– # Handlers #
on findAndReplaceInText(theText, theSearchString, theReplacementString)
–considering case
set AppleScript’s text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript’s text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript’s text item delimiters to “”
return theText
–end considering
end findAndReplaceInText
on writeToFile(theText, theFile, overwriteExistingContent)
try
set theFile to theFile as string
if theFile contains “/” then
set theOpenedFile to open for access theFile with write permission
else
set theOpenedFile to open for access file theFile with write permission
end if
if overwriteExistingContent is true then set eof of theOpenedFile to 0
write theText to theOpenedFile starting at eof
close access theOpenedFile
return true
on error
try
close access file theFile
end try
return false
end try
end writeToFile
Second script to replace M9 with M5
set textToFindList to {“M9”}
set textToReplaceWithList to {“M5”}
if (length of textToFindList) is not equal to ¬
(length of textToReplaceWithList) then return
set theFile to choose file
– tell application “Finder” to duplicate file theFile with exact copy
set theText to read theFile as «class utf8»
repeat with i from 1 to length of textToFindList
set theText to my findAndReplaceInText(theText, ¬
item i of textToFindList, ¬
item i of textToReplaceWithList)
end repeat
my writeToFile(theText, theFile, true)
– # Handlers #
on findAndReplaceInText(theText, theSearchString, theReplacementString)
–considering case
set AppleScript’s text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript’s text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript’s text item delimiters to “”
return theText
–end considering
end findAndReplaceInText
on writeToFile(theText, theFile, overwriteExistingContent)
try
set theFile to theFile as string
if theFile contains “/” then
set theOpenedFile to open for access theFile with write permission
else
set theOpenedFile to open for access file theFile with write permission
end if
if overwriteExistingContent is true then set eof of theOpenedFile to 0
write theText to theOpenedFile starting at eof
close access theOpenedFile
return true
on error
try
close access file theFile
end try
return false
end try
end writeToFile