I have spent nearly 2 days trying to get a script to work that will search and replace some specific characters in the OSX Finder Comments of some files. I have around 60 folders with anything from 10 to 99 items in each folder and so I though it should be reasonably easy to create a script that would do this for me on a folder by folder basis.
This basic script works
set ourText to "Public House Very Busy, Cafe Royale, Edinburgh - Saturday Night"
set findThis to " "
set replaceItWith to " "
set newText to switchText of ourText from findThis to replaceItWith
on switchText of theText from SearchString to ReplaceString
set OldDelims to AppleScript's AppleScript's text item delimiters
set AppleScript's AppleScript's text item delimiters to SearchString
set newText to text items of theText
set AppleScript's AppleScript's text item delimiters to ReplaceString
set newText to newText as text
set AppleScript's AppleScript's text item delimiters to OldDelims
return newText
end switchText
I have a script that takes the file names and puts them into the files OSX Comments
on open theItems
repeat with anItem in theItems
tell application "Finder"
activate
try
set itemName to name of anItem as text
set comment of anItem to itemName
end try
end tell
end repeat
end open
But as soon as I try and put the search and replace script into it…
on open theItems
repeat with anItem in theItems
tell application "Finder"
activate
try
set ourText to comment as text
set OldDelims to AppleScript's AppleScript's text item delimiters
set AppleScript's AppleScript's text item delimiters to SearchString
set newText to text items of theText
set AppleScript's AppleScript's text item delimiters to ReplaceString
set newText to newText as text
set AppleScript's AppleScript's text item delimiters to OldDelims
set findThis to " "
set replaceItWith to " "
set newText to switchText of ourText from findThis to replaceItWith
to switchText of theText from SearchString to ReplaceString
end switchText
set comment of anItem to newText
end try
end tell
end repeat
end open
It won’t compile, and the Script Editor reports 'Expected “end” or “on” but found “to” and highlights the “to” of
to switchText of theText from SearchString to ReplaceString
So I then move that section outside of the ‘open’ section like this
on open theItems
repeat with anItem in theItems
tell application "Finder"
activate
try
set ourText to comment as text
set OldDelims to AppleScript's AppleScript's text item delimiters
set AppleScript's AppleScript's text item delimiters to SearchString
set newText to text items of theText
set AppleScript's AppleScript's text item delimiters to ReplaceString
set newText to newText as text
set AppleScript's AppleScript's text item delimiters to OldDelims
set comment of anItem to newText
end try
end tell
end repeat
end open
set findThis to " "
set replaceItWith to " "
set newText to switchText of ourText from findThis to replaceItWith
to switchText of theText from SearchString to ReplaceString
end switchText
and it doesn’t work!!
So I took another tack
set itemName to words of "Public House Very Busy, Cafe Royale, Edinburgh: - Saturday Night"
itemName as string
set AppleScript's text item delimiters to space
set commentName to itemName as string
set AppleScript's text item delimiters to ""
commentName
Again this basic script works but obviously takes out all the punctuation as well but that wouldn’t be the end of the world.
But putting inside the action loop, it doesn’t work
on open theItems
repeat with anItem in theItems
tell application "Finder"
activate
try
set itemName to comment as text
itemName as string
set AppleScript's text item delimiters to space
set commentName to itemName as string
set AppleScript's text item delimiters to ""
commentName
set comment of anItem to commentName
end try
end tell
end repeat
end open
I also tried a sub rountine
on open theItems
repeat with anItem in theItems
tell application "Finder"
activate
try
processItem(anItem)
end try
end tell
end repeat
end open
on processItem(anItem)
set ourText to the comment of anItem as text
set newText to switchText of ourText from " " to " "
set comment of anItem to newText
end processItem
to switchText of theText from SearchString to ReplaceString
set OldDelims to AppleScript's AppleScript's text item delimiters
set AppleScript's AppleScript's text item delimiters to SearchString
set newText to text items of theText
set AppleScript's AppleScript's text item delimiters to ReplaceString
set newText to newText as text
set AppleScript's AppleScript's text item delimiters to OldDelims
end switchText
but that doesn’t seem to work.
It seems as if whenever I put a handler inside the loop it stops working.
I guess I am doing something very basically wrong but cannot find it.
Any guidance or help would be much appreciated.
Thanks,
Mike.