Except for one difference: I want to replace not one peice of text a bunch of times, but deleting a sequential series of numbers (more specifically, ill-formatted page numbers on an e-book). I’m wanting to delete them by saying ‘change “17” to “”’ The first one is 17 and the last one is 160. I’m basically wanting to automate the autonomous typing in the page number, hitting next, hitting replace, typing in the next page number, hitting next, hitting replace, etc.
Furthermore, it seems like the GUI stuff isn’t working. I’m a total newbie at applescripting (first time today). I’ve enabled it in universal access, but I still get errors for stuff like “click button”.
tell application "TextEdit" to activate
tell application "System Events"
tell process "TextEdit"
keystroke "f" using command down
set n to 17
repeat with n from 17 to 160
set text field "Find" of window "Find" to n
set text field "Replace" of window "Find" to ""
try
click button "Next" of window "Find"
click button "Replace" of window "Find"
end try
set n to n + 1
end repeat
end tell
end tell
That’s what I have so far. It doesn’t work, but you might get the point.
Any help would be awesome, I’m trying to print this book out and bind it for someone for christmas.
Thanks in advance.
Model: Powerbook G4
Browser: Safari 416.13
Operating System: Mac OS X (10.4)
Instead of using GUI scripting, you should try something like this:
tell application "TextEdit"
launch
set theText to text of front document
end tell
set ASTID to AppleScript's text item delimiters
repeat with i from 1 to 5
set AppleScript's text item delimiters to {i as Unicode text}
set theText to every text item of theText
set AppleScript's text item delimiters to {""}
set theText to theText as Unicode text
end repeat
set AppleScript's text item delimiters to ASTID
tell application "TextEdit"
activate
make new document with properties {text:theText}
end tell
Also.
When using a repeat loop like this, you don’t need to create “n” beforehand, nor do you need to manually increase “n” inside the loop.
You can also do a search and replace directly in TextEdit. Something like this:
set r to ""
tell application "TextEdit"
repeat with i from 17 to 160
set s to i as string
set (first word of text of front document where it is s) to r
end repeat
end tell
You might use ‘paragraph’ instead of ‘word’ if your text contains numbers and page numbers have their own paragraphs.
Don’t know wheather this or using offset or AS tid is faster.
Thanks! it looks pretty good, but the one problem is that it’s losing the basic formatting (when text is a different size, or center justified, etc) and i’d like to not have to go through this again. is that possible or should i just suck it up?
Oh! i didn’t see your message, kel, until i had typed my reply. I’m a little confused about what you mean, would you be able to put it in a script i can just copy and paste in? I just don’t understand what S is supposed to mean, and also if that’s the full script or not.
tell application "TextEdit"
repeat with i from 1 to 5 -- use whichever numbers you need
set (first word of text of front document where it is (i as Unicode text)) to ""
end repeat
end tell
For a larger file, TIDs might be faster, but this method won’t destroy formatting in Rich Text documents.
Hmm. that would only get rid of the fist number. I’m just working with a sample until I get it right; I don’t want to spend 20 minutes between goes.
Anyway, here’s the script:
tell application "TextEdit"
repeat with i from 17 to 20 -- use whichever numbers you need
set (first word of text of front document where it is (i as Unicode text)) to ""
end repeat
end tell
and here’s the sample text I’m using (pretend it’s rich-text formatted) - you should be able to just copy and paste it into a textedit document to re-create what I’m doing.
Edit: mods, you don’t need to worry about any copyright stuff, if you’re concerned. It’s a copyright-free book off of gutenberg.org
yeah, but i’ve changed a handful of formatting things around to accomodate the book binding, such as font size and margins, etc.
come to think of it, though, the only thing that i’m concerned about is the font size - i don’t want to have to go through and make all of the chapter headings big again. But, this is proving to be somewhat annoying, and if you don’t have anything off of the top of your head it’s not that big of a deal.
the one i downloaded was the 7.8 mb zip file, and then i opened the .html file in word and converted it to a .doc
I realize that you can select from top to bottom (instead of selecting all) and it won’t copy the page numbers over… but that would take all of the fun out of playing with applescript.
I found out that we’re looking for lines like this:
I had some problems with line breaks, so I used BBEdit to get rid of them (by reformating/reflowing the document). Then I used a “grep” find/replace with this expression:
That seems to be a huge step in the right direction. I’m not sure if you have BBEdit, so I e-mailed you a copy of the modified file.
I copied your text into TextEdit and found that each paragraph ends with space and line feed in the rtf document. I seem to remember that books at Project Gutenburg are really big and have numbers in the text so used this:
set lf to ASCII character 10
set t to "17" & space & lf
tell application "TextEdit"
activate
set first paragraph of text of front document where it is t to ""
end tell
But then I always get trailing spaces when copying from these frames.