Hey Guys trying to search a xml file for a line containing this K=“Color_tag_index” V=“0”/ and replace it with this K=“Color_tag_index” V=“1”/
tried adapting Bruce’s snippet to no luck
set thesearchList to ("K="Color_tag_index" V="0"/")
set theReplaceList to (K="Color_tag_index" V="1"/)
set theCos to (" /Users/rapdigital/Pictures/Untitled/Capture/519300101/CaptureOne/Settings50/519300101_001.IIQ.cos")
tell application "TextEdit"
set theDoc to open theCos
-- get text contents
set theText to text of theDoc
tell me to set newText to str_ireplace(thesearchList, theReplaceList, theText)
-- or: set newText to my str_ireplace(theSearchList, theReplaceList, theText)
-- replace text
try
set text of theDoc to newText
on error eMsg number eNum
display dialog "Error setting text (" & eNum & ")" buttons {"OK"} default button 1
end try
-- close and save file
try
--save theDoc in theCos
close theDoc saving yes saving in theCos
on error eMsg number eNum
display dialog "Error saving (" & eNum & ")" buttons {"OK"} default button 1
end try
-- reopen
open theCos
end tell
--end repeat
--end run
(*
clone of PHP's str_ireplace function
original posted by Bruce Philips
http://bbs.applescript.net/profile.php?id=5342
on bbs.applescript.net
http://bbs.applescript.net/viewtopic.php?id=18551
note: the above mentioned version doesn't consider case, that's the only
modification by SwissalpS. The original would be str_replace.scpt
*)
on str_ireplace(search, replace, subject)
considering case
local search, replace, subject, ASTID, returnList, searchCount, thisSubject, i, n
set ASTID to AppleScript's text item delimiters
set returnList to true
-- This wouldn't make sense
if search's class is not list and replace's class is list then return subject
-- Make one-item lists if needed
if search's class is not list then set search to {search}
if subject's class is not list then set {subject, returnList} to {{subject}, false}
set searchCount to count search
get count subject
try
repeat with i from 1 to result
set thisSubject to subject's item i
repeat with n from 1 to searchCount
set AppleScript's text item delimiters to search's item n
set thisSubject to thisSubject's text items
if replace's class is list then
try
get replace's item n
on error
get "" -- `replace` ran out of items
end try
else
get replace
end if
set AppleScript's text item delimiters to {result}
set thisSubject to "" & thisSubject
end repeat
set subject's item i to thisSubject
end repeat
end try
set AppleScript's text item delimiters to ASTID
if not returnList then set subject to subject's first item
return subject
end considering
end str_ireplace
Thanks in advance
Searching and replacing text is simple in applescript. You do not need TextEdit or any other program to do it. You just use applescript’s text item delimiters. Heres an example of how to find/replace text.
Suppose you wanted to replace the word “that” with “which” in the following…
set theText to "Some text that you want to modify."
set AppleScript's text item delimiters to "that"
set theItems to text items of theText
set AppleScript's text item delimiters to "which"
set newText to theItems as text
set AppleScript's text item delimiters to ""
return newText
--> result: "Some text which you want to modify."
So in your case you would just get the initial text by reading the xml file into applescript. Then you make your changes and then write the changes back out to the xml file.
What about the formatting of the text?
with this code I can change ALL 0’s to 1’s
set thesearchList to "0"
set theReplaceList to "1"
set theCos to (" /Users/rapdigital/Pictures/Untitled/Capture/519300101/CaptureOne/Settings50/519300101_001.IIQ.cos")
tell application "TextEdit"
set theDoc to open theCos
-- get text contents
set theText to text of theDoc
tell me to set newText to str_ireplace(thesearchList, theReplaceList, theText)
-- or: set newText to my str_ireplace(theSearchList, theReplaceList, theText)
-- replace text
try
set text of theDoc to newText
on error eMsg number eNum
display dialog "Error setting text (" & eNum & ")" buttons {"OK"} default button 1
end try
-- close and save file
try
--save theDoc in theCos
close theDoc saving yes saving in theCos
on error eMsg number eNum
display dialog "Error saving (" & eNum & ")" buttons {"OK"} default button 1
end try
-- reopen
open theCos
end tell
--end repeat
--end run
(*
clone of PHP's str_ireplace function
original posted by Bruce Philips
http://bbs.applescript.net/profile.php?id=5342
on bbs.applescript.net
http://bbs.applescript.net/viewtopic.php?id=18551
note: the above mentioned version doesn't consider case, that's the only
modification by SwissalpS. The original would be str_replace.scpt
*)
on str_ireplace(search, replace, subject)
considering case
local search, replace, subject, ASTID, returnList, searchCount, thisSubject, i, n
set ASTID to AppleScript's text item delimiters
set returnList to true
-- This wouldn't make sense
if search's class is not list and replace's class is list then return subject
-- Make one-item lists if needed
if search's class is not list then set search to {search}
if subject's class is not list then set {subject, returnList} to {{subject}, false}
set searchCount to count search
get count subject
try
repeat with i from 1 to result
set thisSubject to subject's item i
repeat with n from 1 to searchCount
set AppleScript's text item delimiters to search's item n
set thisSubject to thisSubject's text items
if replace's class is list then
try
get replace's item n
on error
get "" -- `replace` ran out of items
end try
else
get replace
end if
set AppleScript's text item delimiters to {result}
set thisSubject to "" & thisSubject
end repeat
set subject's item i to thisSubject
end repeat
end try
set AppleScript's text item delimiters to ASTID
if not returnList then set subject to subject's first item
return subject
end considering
end str_ireplace
but I need to change only one 0 to 1 one this line
That isn’t always line x in the the doc.
As long as your search text is unique so that if finds the correct one to replace, it doesn’t matter where in the text it is located. Give this a try.
set searchWord to "K=\"Color_tag_index\" V=\"0\""
set replaceWord to "K=\"Color_tag_index\" V=\"1\""
set theCos to "/Users/rapdigital/Pictures/Untitled/Capture/519300101/CaptureOne/Settings50/519300101_001.IIQ.cos"
set posixFileCos to POSIX file theCos
set theText to read posixFileCos
set AppleScript's text item delimiters to searchWord
set theItems to text items of theText
set AppleScript's text item delimiters to replaceWord
set newText to theItems as text
set AppleScript's text item delimiters to ""
set openFile to open for access posixFileCos with write permission
set eof of openFile to 0
write newText to openFile starting at eof as string
close access the openFile
Thanks Hank
Not the first time those leading slashes \ have been my down fall!!
Thanks Heaps I love this site.
What if the search finds nothing? How would I add a if statement?