Applescript – Search and replace in Merlin

It’s been a while since we’ve posted the last applescript sample for Merlin. The following code, when copied, pasted in an empty applescript file and compiled can be used for “Search & Replace in Merlin projects”. It prompts for the string to be searched. Asks for the replacement string and informs in case it succeeds or the string had not been found at all.

Enjoy 🙂
__

(*
	This script goes throught all activities of the top most opened project, asks for the string to search, prompts for the string to replace with, checks the title, notes, additional title and subtitle of the activities for the searched text, and replaces the unwanted string.
	Written by Vicky Stamatopoulou
	For ProjectWizards
	Oct 7, 2010
*)

---------------------------

property NeedAProjectMessage : "You must have an open project in order to run this script"
property EnterSearchString : "Please enter the string you want to search for."
property EnterReplaceString : "Please enter the string you want to replace with."
property StringNotFound1 : "The string \""
property StringNotFound2 : "\" was not found"
property DoneMessage1 : " occurrence(s) of \""
property DoneMessage2 : "\" were replaced with \""

global inList, TheDueDate

on collectAllSubactivities(parentActivity)
	global inList
	tell application "Merlin"
		set inList to inList & (parentActivity as list)

		repeat with act in activities of parentActivity
			collectAllSubactivities(act, inList) of me
		end repeat

	end tell
end collectAllSubactivities

on allSubActivities(parentActivity)
	global TheDueDate
	set inList to {}
	tell application "Merlin"
		collectAllSubactivities(parentActivity, inList) of me
	end tell
	return inList
end allSubActivities

tell application "Merlin"
	global inList
	try
		set doc to the first document
	on error
		display dialog NeedAProjectMessage
		set chosenFile to (choose file)
		open chosenFile
		set doc to the first document
	end try

	set proj to root project of doc

	set TheSearchString to display dialog EnterSearchString default answer ""
	set TheSearchString to (text returned of TheSearchString) as string

	set TheReplaceString to display dialog EnterReplaceString default answer ""
	set TheReplaceString to (text returned of TheReplaceString) as string

	set allActivities to allSubActivities(proj) of me

	set TheCounter to 0
	repeat with act in allActivities

		if title of act contains TheSearchString then
			set mytemp to title of act as text
			set oldTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to TheSearchString
			set mytemp to mytemp's text items
			set AppleScript's text item delimiters to TheReplaceString
			set mytemp to mytemp as text
			set AppleScript's text item delimiters to oldTID

			set title of act to mytemp
			set TheCounter to TheCounter + 1
		end if

		try
			get subtitle2 of act
			if subtitle2 of act contains TheSearchString then
				set mytemp to subtitle2 of act as text
				set oldTID to AppleScript's text item delimiters
				set AppleScript's text item delimiters to TheSearchString
				set mytemp to mytemp's text items
				set AppleScript's text item delimiters to TheReplaceString
				set mytemp to mytemp as text
				set AppleScript's text item delimiters to oldTID

				set subtitle2 of act to mytemp
				set TheCounter to TheCounter + 1

			end if
		end try
		try
			if item description of act contains TheSearchString then
				set mytemp to item description of act as text
				set oldTID to AppleScript's text item delimiters
				set AppleScript's text item delimiters to TheSearchString
				set mytemp to mytemp's text items
				set AppleScript's text item delimiters to TheReplaceString
				set mytemp to mytemp as text
				set AppleScript's text item delimiters to oldTID

				set item description of act to mytemp
				set TheCounter to TheCounter + 1

			end if
		end try
		try
			if description of act contains TheSearchString then
				set mytemp to description of act as text
				set oldTID to AppleScript's text item delimiters
				set AppleScript's text item delimiters to TheSearchString
				set mytemp to mytemp's text items
				set AppleScript's text item delimiters to TheReplaceString
				set mytemp to mytemp as text
				set AppleScript's text item delimiters to oldTID

				set description of act to mytemp
				set TheCounter to TheCounter + 1

			end if
		end try

	end repeat
	if TheCounter = 0 then
		display dialog (StringNotFound1 & TheSearchString & StringNotFound2) as string buttons {"Ok"}

	else
		display dialog (TheCounter & DoneMessage1 & TheSearchString & DoneMessage2 & TheReplaceString & "\"") as string buttons {"Ok"}
	end if
end tell

___
If you like this script and need to use it often for your work, just place it into your ~/Library/Application Support/SendToMenu folder to have it always available under File > SendTo.

Related posts:
Expanding the ‘Send To” submenu of Merlin

One thought on “Applescript – Search and replace in Merlin

  1. Well – just tried this. Never used apple script before, but I thought i’d give it a go anyway.
    Really is simple. Navigate to the menu >applications>utilities>applescript_editor
    Click Applescript editor. Paste the above text into the main editor window. Then >File>save_as… and save the file as ‘Find and Replace’ in exactly the following place… /library/application support/Merlin/sendtomenu
    Then make sure you close down (quit) Merlin.
    Reopen Merlin.
    go to >file>sendto> and you should see ‘Find and Replace’ (or what ever you called your script)
    Select this option and you see a ‘Find’ window… then a replace ‘window’ etc
    Works for me…
    Thanks Vicky

Comments are closed.