Author Topic: Case sensitive find / change  (Read 2613 times)

0 Members and 1 Guest are viewing this topic.

Offline mark1

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Case sensitive find / change
« on: October 17, 2007, 04:07:25 AM »
Hi

I have a sequence of fairly simply find / changes I want to execute.

try
set contents of every text where it is "XyZ" to "XYZ"
end try

They need, however, to be case sensitive (i.e. find and change 'XyZ" not "xyz").
The text is taken from a database so has no styling.

I'm presuming that I should use a 'considering case' statement but I just can't get it to work.

Any ideas?

Thanks

Mark

Offline Emma

  • Full Member
  • ***
  • Posts: 205
  • Karma: 9
Re: Case sensitive find / change
« Reply #1 on: October 17, 2007, 04:20:35 AM »
Script didn't run quite as you'd typed it, but I suspect that was doing it from memory?

Anyway, very odd - I put it in a 'considering case' and it made no difference. So I went mad and ran this:

tell document 1 of application "QuarkXPress"
     try
          set every text of every story where it is ((ASCII character 88) & (ASCII character 121) & (ASCII character 90)) to ((ASCII character 88) & (ASCII character 89) & (ASCII character 90))
     end try
end tell

- And it STILL changed xyz to XYZ although I'd specifically given it ASCII values (and did the same when I added considering case for luck). That is very weird. Can anyone enlighten us?
------------
Quark 6.5
OS X 10.4.8
G4

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Re: Case sensitive find / change
« Reply #2 on: October 17, 2007, 07:35:05 AM »
Reading this, here's what "I" think:

Considring case works when Applescript is doing the work, in this case the "search" is made by Quark which uses an unknown "behind the scene" procedure that must not be case sensitive.
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5

Offline Emma

  • Full Member
  • ***
  • Posts: 205
  • Karma: 9
Re: Case sensitive find / change
« Reply #3 on: October 17, 2007, 07:49:03 AM »
Thanks Michel! Inspired. So try this....

set my_strings to {}

tell document 1 of application "QuarkXPress"
     set my_refs to object reference of every text of every story where it is "XyZ"
     
     repeat with i in my_refs
          set my_strings to my_strings & (text of i)
     end repeat
     
end tell

considering case
     repeat with j from 1 to length of my_strings
          if item j of my_strings is "XyZ" then set item j of my_strings to "XYZ"
     end repeat
end considering

tell document 1 of application "QuarkXPress"
     repeat with k from 1 to length of my_refs
          set text of item k of my_refs to item k of my_strings
     end repeat
end tell

- the work gets done outside Quark!
------------
Quark 6.5
OS X 10.4.8
G4

Offline mark1

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Case sensitive find / change
« Reply #4 on: October 17, 2007, 08:56:17 AM »
That's great. I was fumbling around trying to use object references and work outside Quark, but was nowhere near this. Thank you.

As I have a few of these find / changes to execute the I have structured the script like so:

Code: [Select]
SAndR("XYZ", "123")
SAndR("xyz", "456")
---etc

on SAndR(searchstring, replacestring)
set my_strings to {}

tell document 1 of application "QuarkXPress"
set my_refs to object reference of every text of every story where it is searchstring

repeat with i in my_refs
set my_strings to my_strings & (text of i)
end repeat

end tell

considering case
repeat with j from 1 to length of my_strings
if item j of my_strings is searchstring then set item j of my_strings to replacestring
end repeat
end considering

tell document 1 of application "QuarkXPress"
repeat with k from 1 to length of my_refs
set text of item k of my_refs to item k of my_strings
end repeat
end tell

end SAndR

The idea for this comes from another post on this site.

For the record this also works (I found it elsewhere), but only for entire words.
Here the 'considering case' does seem to operate within Quark.

Code: [Select]
tell application "QuarkXPress"
activate
set findText to "XYZ"
set replaceText to "xyz"
tell current box of front document
set i to count words
repeat with x from 1 to i
considering case
if word x is findText then set word x to replaceText
end considering
end repeat
end tell
end tell

Cheers

Mark

Offline Emma

  • Full Member
  • ***
  • Posts: 205
  • Karma: 9
Re: Case sensitive find / change
« Reply #5 on: October 17, 2007, 08:58:55 AM »
I seem to recognise the S&R script - nice to know my stuff is being useful!!

Interesting, so 'case' works for words but not for text. Probably works for 'character' too, though no help here.

Glad the problem is solved!
------------
Quark 6.5
OS X 10.4.8
G4

Offline DeeTee

  • Visitor
  • Posts: 1
  • Karma: 0
Re: Case sensitive find / change
« Reply #6 on: March 19, 2014, 11:41:46 AM »
Hello,

I am new to Apple Script and struggling to find a solution to my problem.  I have to search and replace many different sets of two characters within many Quark documents, however I need to replace them with a single character. i.e. search for "ab" replace with X" (sometimes also I need to replace only a single character with a single character).  I also need it to be case sensitive and I am not replacing entire words.

The above code replaces the same length strings so the problem doesn't present itself.

In my case if I run this script (the first one Posted by: mark1 on: October 17, 2007, 05:56:17 AM) and want to replace "ab" with "X" in this text:

123ab456ab789ab123ab456ab789

I get this:

123X456aX89abX3ab4Xab789

In the first instance of "ab", it is replaced with "X" as I want, but in the second instance "b7"! is replaced with "X" i.e. it is shifted to the right by one character.  In the third instance it is shifted by 2 characters and "12" is replaced by "X" and so on.

If any one can think of a solution to this I would really grateful - it would save my hours of time.

Thanks,

DeeTee
« Last Edit: March 19, 2014, 11:52:57 AM by DeeTee »

Offline Jean-Marie Schwartz

  • Sr. Member
  • ****
  • Posts: 436
  • Karma: 6
  • Gender: Male
Re: Case sensitive find / change
« Reply #7 on: March 26, 2014, 10:42:00 AM »
Hi DeeTee! Not much people around here!  ???

I think the reason why the script does not work as YOU expect is that it takes the list of references in the regular order. So characters with a given reference at the 1st stage of the script are not the same as those while the script is running because of the changes you made. The solution I think off (because I learnt it from our master Michel) is to take the list in reverse.
Please add the line
Code: [Select]
set my_refs to reverse of my_refs
after
Code: [Select]
set my_refs to object reference of every text of every story where it is searchstring

Give that a shot and report any result.
Jean-Marie Schwartz
May the Schwartz be with you :-)

= Mac Pro Quad-Core Intel 2.8 GHz 10 Go 1066 MHz DDR3 Mac OS 10.8.5 QuarkXPress 9.5.4.1 Adobe CS5 =