Finding & Replacing A Variable Text String In Text Edit

Hi! I don’t even know if I asked this correctly.

I’m trying to find and replace certain links in HTML code using TextEdit. I know that the links I’m looking for will always begin with “<a href=” and will always end with “a>”.

The text between the references will vary so I can’t use a simple string. How can I tell TextEdit to find a string with a definite beginning and ending sequence but a different sequence in the middle?

I’m looking into using the grep command now.

since your looking into shell commands (awk and grep) you should take a look at sed

grep ‘<a href…a>>’ /thefile?

If you have a string:

set HTMLString to "Here an example with a link to <a href=\"any server\">any server</a> forum."

do shell script "sed 's/<a href=[^>]*>[^<]*<\\/a>/<a href=\"http:\\/\\/\">macscripter<\\/a>/g' <<<" & quoted form of HTMLString

Thanks! I may have mis-stated the beginning and ending strings. This is an example line of code that I’m trying to work with:

<a href="x-devonthink-item://F67EA8CB-84E5-4B06-891F-33509D86457B"><font class="font0">Announcement From Corevalus.pdf

I’d like to replace "x-devonthink-item://F67EA8CB-84E5-4B06-891F-33509D86457B" with a link to a local file. I tried modifying what I thought were the beginning and ending references in the code but I don’t see the change represented in the result at the bottom of the apple-script editor.

Here’s what I have so far.


The goal of this script is to publish a report written using DevonThink's HTML document.  This report can then be shared with others who do not have DevonThink.  


--Step 1: Get the HTML code of the report as text.

tell application "DEVONthink Pro"
	get the source of think window 1 as string
	set the_file to result
end tell

--Step 2: Create a text file with that text.

tell application "TextEdit"
	delay 1
	close window 1
	delay 1
	make new document with properties {name:"DevonThink Report", text:the_file}
	set bounds of window 1 to {62, 23, 1371, 883}
end tell

--Step 3: Find the links in TextEdit.

tell application "System Events"
	keystroke "f" using command down
	keystroke "<a href="
	delay 2
	keystroke "p" using {control down, option down, command down}
	delay 2
	keystroke "a" --tell application "System Events" to key code 31 using control down
	delay 2
	keystroke return
	keystroke "</a>"
end tell

--Step 4: Choose a file to replace the link.

tell application "Finder"
	choose file default location (desktop) as alias
	get URL of result
	set the_new_file to result
end tell

tell application "Script Editor"
end tell

--Step 5: Replace the link.

tell application "TextEdit"
	display dialog "Highlight the link you want to replace"
end tell

--tell application "System Events"
--keystroke "s" using {shift down, command down}
--keystroke "Final Devon Think Report"
--end tell

This should work:

set HTMLString to "Here an example with a link to <a href=\"any server\">any server</a> forum."
set newURL to ""

set result to do shell script "echo '" & HTMLString & "' | sed 's,\\(<a href=\"\\)\\([^/]*\\)\\(\">\\),\\1" & newURL & "\\3,g'"

Is your use case restricted to TextEdit, or do you just have a text file that needs the changes you indicate?

Actually, now that I have reread one of your later posts, it seems that you are pulling the HTML code from DevonThink, and then you want the user to choose the href URL to change, correct?

If this is correct, then I would use a tool other than TextEdit.
Both of these tools are free, and very scriptable:

  1. TextWrangler and has a great RegEx tool.[/*]
  2. Satimage.osax has a great RegEx tool.[/*]

You could, using RegEx, get all of the href’s, and present the user with an AppleScript choose list.
Then, using AppleScript choose file, pick a file URL to replace it with.

Let me know if this is of interest to you, and I’ll provide more info.

COMMENT TO FORUM ADMINS:  Forum does not seem to confirm to standard BBCode:
[url=]BBCode guide[/url]

Problem is that PHPBB hasn’t been updated in years and is running an very old version (1.2 or 1.3 IIRC) therefore the “new” BBCode is unsupported. I really miss the strikethrough, which already is available in the next version. I think Ray Barber is the only one who has the possibility update the site. Currently the only active moderator is Nigel Garvey and he’s unable to update PHPBB to a newer version. It’s too bad that the site hasn’t been maintained or updated in 8 years.

Using the very, very, awesome Table dialog recently published by Shane Stanley, you could present the user with a nice table list of hrefs to select from:

Myriad Tables Script Library

Hi. I’ve just spotted this.

It looks as if you were trying to use BBCode’s [list] tags. I seem to remember MacScripter supporting them in the past, but they’ve not worked here for a while now. They never saw much use, but I don’t know why they were dropped.

The board software was customised some time ago at Ray’s own expense to support the unique [applescript] tags, which allow posted scripts to be opened in people’s default script editors with just one click. This is one of the reasons the software’s not updated very often. Each update would need to be professionally doctored by a FluxBB expert to maintain the custom tags. I gather this site’s no great earner for Ray, so he’s understandably loath to throw a lot of cash at it.

That said, he’s keen for it to be as useful as possible to the “AppleScript community” and will certainly at least listen if there’s a demand for a particular facility. He had the new [format] tags implemented a month or two back. These do what you’d normally expect from [code] tags (monospaced font, line indents preserved), but are differently named to make a clear distinction between their use and that of the [applescript] tags. Unfortunately, despite some e-mails about it at the time, the site’s own BBCode guide’s still not been updated.

My reading of post #6 is that the x-devonthink-item URL (between the quotes in the <a href .> tag) has to be replaced with the file URL, eg.:

set default_location to (path to desktop)

tell application (path to frontmost application as text) to set the_new_file to (choose file default location default_location)
tell application "System Events" to set file_url to the_new_file's URL

-- Example source for testing:
set some_html to "</font><a href=\"x-devonthink-item://F67EA8CB-84E5-4B06-891F-33509D86457B\"><font class=\"font0\">Announcement From Corevalus.pdf</font></a> </div>"

-- Regex replacement using sed.
-- Both URLs contain several "/"s, so use a delimiter other than "/" in the s command.
set sed_string to "s|(<a href=\")x-devonthink-item[^\"]+|\\1" & file_url & "|"
-- Use 'quoted form' for the entire s command in case the file URL contains an apostrophe or single quotes.
set edited_html to (do shell script ("echo " & quoted form of some_html & "| sed -E " & quoted form of sed_string))