help please on search and replace problem with a droplet action script

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.

Hi,

I don’t know exactly, what the search string and the replace string is (it looks both to be one space character)
but this should do it.
I put the two parameters in the property lines (assuming to change two space characters into one)


property SearchString : space & space
property ReplaceString : space

on open theItems
	set ASTID to AppleScript's text item delimiters
	repeat with anItem in theItems
		try
			set itemName to name of (info for anItem)
			set AppleScript's text item delimiters to SearchString
			set itemName to text items of itemName
			set AppleScript's text item delimiters to ReplaceString
			set itemName to itemName as text
			tell application "Finder" to set comment of contents of anItem to itemName
		end try
	end repeat
	set AppleScript's text item delimiters to ASTID
end open

PS: Don’t ask me, what’s wrong in your scripts, they are looking very confusing :wink:

Thanks for the help. Although the Search parameter looks like a couple of spaces it isn’t. It needs to be what is surrounded in the quotes. Also the aim to to rewrite the OSX Finder Comments and leave the file name alone. So I made 2 changes to your script and it doesn’t seem to work now. Before it was successfully putting the file name into the comments which on this occasion I don’t want to do. What I am trying to do is fix a problem in the OSX Finder Comments of a load of files that have this set of characters that look like spaces but aren’t, remove them and replace them with a conventional space. This is so another application (digidesign Pro Tools can import the data from the OSX Finder Comments into its own cataloging system.


property SearchString : "   "
property ReplaceString : space

on open theItems
	set ASTID to AppleScript's text item delimiters
	repeat with anItem in theItems
		try
			set itemName of anItem to comment as text
			set AppleScript's text item delimiters to SearchString
			set itemName to text items of itemName
			set AppleScript's text item delimiters to ReplaceString
			set itemName to itemName as text
			tell application "Finder" to set comment of contents of anItem to itemName
		end try
	end repeat
	set AppleScript's text item delimiters to ASTID
end open

Is it OK to put the quotes into the property list?

How do I put tests into the script to make sure that it is picking up the Comments field and processing it correctly?

Thanks,

Mike.

this cannot work

set itemName of anItem to comment as text

if you want to get the comment of the item, use this

tell application "Finder" to set itemName to comment of anItem

comment is text, so a coercion is not needed

if you want to filter multiple (special) characters, you need a routine which processes each character of the string

Try something like this:

on run
	choose file with multiple selections allowed without invisibles
	open result
end run

on open theseItems
	repeat with thisItem in theseItems
		tell application "Finder"
			set comment of thisItem to my replaceText("whatever", space, get comment of thisItem)
		end tell
	end repeat
end open

-- find    : Text to be found
-- replace : Text to replace with
-- subject : Text to be searched
on replaceText(find, replace, subject)
	set prevTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to find
	set subject to text items of subject
	
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to prevTIDs
	
	return subject
end replaceText

Stefan,

You are a star, that idea worked a treat.

This is the fully working script…


property SearchString : "   "
property ReplaceString : space

on open theItems
	set ASTID to AppleScript's text item delimiters
	repeat with anItem in theItems
		try
			tell application "Finder" to set itemName to comment of anItem
			set AppleScript's text item delimiters to SearchString
			set itemName to text items of itemName
			set AppleScript's text item delimiters to ReplaceString
			set itemName to itemName as text
			tell application "Finder" to set comment of contents of anItem to itemName
		end try
	end repeat
	set AppleScript's text item delimiters to ASTID
end open

Case closed.

Thank you all for this excellent site. :slight_smile:

Mike.