Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
 
Thread Tools Rate Thread Display Modes
Old 12-12-2012, 05:24 AM   #1
paldo
Triple-A Player
 
Join Date: Dec 2006
Location: Switzerland
Posts: 174
find and replace function

I have a few documents with lots of text that I have to replace.

For example I have to replace:

<div id="answer1" style="visibility:hidden; color:brown" >
<div id="answer2" style="visibility:hidden; color:brown" >
<div id="answer3" style="visibility:hidden; color:brown" >
...............................................................................
<div id="answer154" style="visibility:hidden; color:brown" >

That means that I have to replace 154 pieces of text that are almost similar, with

<div class="A">A:<div>

Is there a way to automate this process?
I'm using OS 10.7 and dreamweaver CS5.5

I appreciate your help
paldo is offline   Reply With Quote
Old 12-12-2012, 06:28 AM   #2
benwiggy
League Commissioner
 
Join Date: Aug 2006
Posts: 5,040
You don't need to AppleScript, you can just use a regular expression in the DreamWeaver Find and Replace dialog.
http://www.adobe.com/devnet/dreamwea...s_pt2.edu.html
benwiggy is offline   Reply With Quote
Old 12-12-2012, 06:48 AM   #3
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,050
And the expression you want is:

Find:
<div id="answer[0-9]*" style="visibility:hidden; color:brown" >

Replace:
<div class="A">

Or a better html option (and simpler replacement):

style="visibility:hidden; color:brown"

class="answer"

this way you keep the ID.
acme.mail.order is offline   Reply With Quote
Old 12-12-2012, 07:10 AM   #4
benwiggy
League Commissioner
 
Join Date: Aug 2006
Posts: 5,040
Quote:
Originally Posted by acme.mail.order
Or a better html option (and simpler replacement):

style="visibility:hidden; color:brown"

class="answer"

Though this will affect other tags with those attributes, which may cause problems.
benwiggy is offline   Reply With Quote
Old 12-12-2012, 02:07 PM   #5
paldo
Triple-A Player
 
Join Date: Dec 2006
Location: Switzerland
Posts: 174
Quote:
Originally Posted by acme.mail.order
And the expression you want is:

Find:
<div id="answer[0-9]*" style="visibility:hidden; color:brown" >

Replace:
<div class="A">

Great, it works ! Thank you so much.

As I also have 154 questions
I then tried to replace the following:

<div id="question1" onclick="Answer('question1','answer1','red','green');" style="color:blue" >
<div id="question2" onclick="Answer('question2','answer2','red','green');" style="color:blue" >
<div id="question3" onclick="Answer('question3','answer3','red','green');" style="color:blue" >
.....................until question 154

with:

Find:

<div id="question[0-9]*" onclick="Answer('question[0-9]*','answer[0-9]*','red','green');" style="color:blue" >

but no way to make it work, it doesn't find . Why?
paldo is offline   Reply With Quote
Old 12-12-2012, 02:37 PM   #6
paldo
Triple-A Player
 
Join Date: Dec 2006
Location: Switzerland
Posts: 174
After a long internet search I got it:

<div(.*)blue" >

Thank you, you made my day.
paldo is offline   Reply With Quote
Old 12-12-2012, 03:15 PM   #7
fracai
MVP
 
Join Date: May 2004
Posts: 2,012
It probably didn't match because the parentheses were being treated as groups, rather than plain characters. Similarily, your solution probably doesn't need the parens as you aren't doing anything with what is matched by the .*

You can capture matched text and then insert it in the replacement using parens to group the capture during the match and backreference characters in the replacement ($1, $2 or \1, \2 depending on the implementation).
__________________
i am jack's amusing sig file
fracai is offline   Reply With Quote
Old 12-13-2012, 04:43 AM   #8
paldo
Triple-A Player
 
Join Date: Dec 2006
Location: Switzerland
Posts: 174
Quote:
Originally Posted by fracai
Similarily, your solution probably doesn't need the parens as you aren't doing anything with what is matched by the .*

It works with parenthasis but I will keep in mind your advise for next time. Thank you.
paldo is offline   Reply With Quote
Old 12-13-2012, 06:59 AM   #9
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,050
Quote:
Originally Posted by paldo

<div id="question1" onclick="Answer('question1','answer1','red','green');" style="color:blue" >
<div id="question2" onclick="Answer('question2','answer2','red','green');" style="color:blue" >
<div id="question3" onclick="Answer('question3','answer3','red','green');" style="color:blue" >
.....................until question 154

That's just..... nasty.

How about:
HTML:
Code:
<div id="question2" class="question">
CSS:
Code:
.question { color: blue; }
JavaScript:

Code:
jQuery(document).ready( setupQuestions );

function setupQuestions() {
  jQuery('.question').click( function() {
    event.preventDefault();
    var param2 = this.id.replace('question','answer');
    Answer(this.id, param2, 'red', 'green');
  });
}
In general, if you are writing the same code more than twice, there's a Better Way.

Don't forget to load jQuery.
acme.mail.order is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 05:28 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.