I can do this in PHP! @#$% Apple Script

Sorry about the frustration bleeding into the subject line… At any rate, any help would be appreciated. I am trying to use apple script to do some automatic text replacement with in one file. Example text from file –

$workOutData = $_POST[‘ActualTricepsLvlSet1’];
$workOutData = $_POST[‘ActualTricepsLvlSet2’];
$workOutData = $_POST[‘ActualTricepsLvlSet3’];
$workOutData = $_POST[‘ActualTricepsLvlSet4’];
$workOutData = $_POST[‘ActualTricepsLvlSet5’];
$workOutData = $_POST[‘ActualTricepsLvlSet6’];

Script I have worked out so far (“to find text” and “to change” come from satimage dictionary) —

tell application “BBEdit”
set form_text to get contents of text window 1
end tell – application “BBEdit”
– global x
property x : 1
global findThis
global replaceItWith
global form_text
property OldDelims : {“”}
set findPos to 1
set findThis to “workOutData”

set foundText to find text findThis in form_text with string result
set changeTo to foundText & x

set form_text to change foundText into changeTo in form_text starting at findPos for 11
set x to (x + 1)
– return theResult
– return foundText & " changeTo is: " & changeTo & " form_text is: " & form_text
tell application “BBEdit”
set contents of text window 1 to form_text
end tell – application “BBEdit”

the goal being that each time workOutData is found an incremented value is appended to it so ideally after the script is run the text would look like this —

$workOutData1 = $_POST[‘ActualTricepsLvlSet1’];
$workOutData2 = $_POST[‘ActualTricepsLvlSet2’];
$workOutData3 = $_POST[‘ActualTricepsLvlSet3’];
$workOutData4 = $_POST[‘ActualTricepsLvlSet4’];
$workOutData5 = $_POST[‘ActualTricepsLvlSet5’];
$workOutData6 = $_POST[‘ActualTricepsLvlSet6’];

I have several hundred fields to rename and the thought of manual editing is making me think that job at McD’s might not be so bad after all. :slight_smile:

Well, i’ve used this method for a while in ASS, should do the trick for your needs:

on searchAndReplaceString_stringToSearchAndReplace_theSearchString_theReplaceString_ignoreCase_ignoreDiacriticals(stringToSearchAndReplace, theSearchString, theReplaceString, ignoreCase, ignoreDiacriticals)
	set returnString to stringToSearchAndReplace
	set oldASTID to AppleScript's text item delimiters
	
	if ignoreCase is true and ignoreDiacriticals is false then
		ignoring case
			set AppleScript's text item delimiters to theSearchString
			set tempString to text items of stringToSearchAndReplace
			set AppleScript's text item delimiters to theReplaceString
			set returnString to tempString as string
		end ignoring
	else if ignoreCase is false and ignoreDiacriticals is true then
		ignoring diacriticals
			set AppleScript's text item delimiters to theSearchString
			set tempString to text items of stringToSearchAndReplace
			set AppleScript's text item delimiters to theReplaceString
			set returnString to tempString as string
		end ignoring
	else if ignoreCase is true and ignoreDiacriticals is true then
		ignoring case and diacriticals
			set AppleScript's text item delimiters to theSearchString
			set tempString to text items of stringToSearchAndReplace
			set AppleScript's text item delimiters to theReplaceString
			set returnString to tempString as string
		end ignoring
	else if ignoreCase is false and ignoreDiacriticals is false then
		set AppleScript's text item delimiters to theSearchString
		set tempString to text items of stringToSearchAndReplace
		set AppleScript's text item delimiters to theReplaceString
		set returnString to tempString as string
	end if
	set AppleScript's text item delimiters to oldASTID
	return returnString
end searchAndReplaceString_stringToSearchAndReplace_theSearchString_theReplaceString_ignoreCase_ignoreDiacriticals

and you use it like this:

set resultOfSearchReplace to (my searchAndReplaceString_stringToSearchAndReplace_theSearchString_theReplaceString_ignoreCase_ignoreDiacriticals(textToSearchAndReplace, stringToSearchFor, stringToReplace, true, true))

hope that helps…

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

Thanks very much leonsimard,

While the script is good at catching and replacing it still won’t allow an incremented replacement. When the script is run it replaces every instance not just one of them. For instance the first loop will generate the following

$workOutData1
$workOutData1
$workOutData1

2nd loop –

$workOutData21
$workOutData21
$workOutData21

3rd loop –

$workOutData321
$workOutData321
$workOutData321

The desired result is –

$workOutData1
$workOutData2
$workOutData3

So some how I need the script to change only the first instance it finds terminate and loop again searching for workOutData ignore workOutData1 find the second instance of workOutData, change it to workOutData2 terminate and loop … and so on But I can not make it ignore workOutData within workOutData1

Hmmm, I don’t know if your data is always formatted the same way, but why don’t you search for " = $_POST[‘ActualTricepsLvlSet" and replace it with “” so you’ll end up with "$workOutData1’];", and then do a second search for “'];” and replace it again for “”?

The ideal in your case would be a grep search, which you could have access to with a do shell script command. I can’t really teach you anything in this case, and grep is a bit challenging if you never played around with it.

Is the pattern always going to be the same? If yes, I could construct the grep search for you, but that’s all I could do… you’d have to figure out the rest.

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

Yes in this case the pattern will always be the same. And thanks for the grep tip, I used it long ago, guess I will have to revisit…

If you have a bit of time and are willing to create the grep script I would be grateful, not starting from scratch is usually a good benefit.:smiley:

Thank You

Sure. I’d need to have a bit more examples of your data, examples of what it is and what it needs to be. And also… a bit of time. :slight_smile:

And like I said I would only be able to construct the grep search syntax only, not the code that goes with it… but this I am sure you can find on the macscripter’s forums, with a simple “site:macscripter.net do shell script grep” query in google…

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

Text item delimiters are good for finding every instance of the word you’re looking for. As such this is an easy task…

set textToAppendInteger to "workOutData"
set theText to "$workOutData = $_POST['ActualTricepsLvlSet1'];
$workOutData = $_POST['ActualTricepsLvlSet2'];
$workOutData = $_POST['ActualTricepsLvlSet3'];
$workOutData = $_POST['ActualTricepsLvlSet4'];
$workOutData = $_POST['ActualTricepsLvlSet5'];
$workOutData = $_POST['ActualTricepsLvlSet6'];"

-- use text item delimiters to find all the instances of textToAppendInteger in theText
set {tids, text item delimiters} to {text item delimiters, textToAppendInteger}
set theItems to text items of theText
set itemCount to count of theItems
set text item delimiters to tids

-- loop through the list items and append your stuff as you reassemble the text
set newText to ""
repeat with i from 1 to itemCount
	if i is itemCount then
		set newText to newText & item i of theItems
	else
		set newText to newText & item i of theItems & textToAppendInteger & (i as text)
	end if
end repeat
return newText

Thank You regulus6633,

I had just logged on to say that I managed to make it work and saw your response. Your script is clearly superior to mine and I appreciate it very much. I have included my much more hacked version just because I got it to work, and I thought it wasn’t bad for a first script :smiley:

– declare vars
global x
global findThis
global formText
global newText

tell application “BBEdit”
set formText to get contents of text window 1
end tell – application “BBEdit”

– init vars
– setting x to 2 is done to compensate for the search’s blank lead item
– I don’t know if this is specific to my system or not so UAYOR
set x to 2

– this is the string you are looking for
– change lookForMe to what you want to find
set findThis to “lookForMe”
– init newText and call getAndSet function
set newText to getAndSet(formText, findThis)

on getAndSet(theText, searchText)

set AppleScript's text item delimiters to searchText
set newText to text items of theText
set numberOfRecords to count newText
-- again compensating for the blank first record
set numberOfRecords to numberOfRecords - 1
-- setup loop
repeat numberOfRecords times
	-- get items which contain all text per line but the delimiters or searchText
	-- one at a time
	set singleItem to item x of newText
	-- add your search string (findThis) add the iterate add the singleItem which contains the
	-- text on both sides of the search string
	set item x of newText to findThis & (x - 1) & singleItem
	set x to (x + 1)
end repeat
set AppleScript's text item delimiters to {""}
set newText to newText as text

end getAndSet

– send new text back to BBEdit

tell application “BBEdit”
set contents of text window 1 to newText
end tell – application “BBEdit”