AppleScript with TextEdit

Is it possible (and if so, how) to use AppleScript to open a particular TextEdit file, select a certain portion (as in from the top to when it reaches the word “TEN”) and delete that. Then have it delete the last three lines in the file?

Is that possible?

Does that make sense?

Can someone help me?

I swear I’m not insane!


This will keep you from having to deal with TextEdit.
Adjust as necessary to fit your exact needs.



--path to text file
set theFile to (path to desktop as Unicode text) & "t.txt"
set readFile to read file theFile

--get everything after "TEN"
set textAfterTEN to item 2 of my tidStuff("TEN", readFile)

set fileParas to paragraphs of textAfterTEN

--remove the last three lines
set finalText to items 1 thru -4 of fileParas

--get the list back to a string
--may need to tweak the result depending
--on where the TEN is in the text
set OD to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set finalText to items of finalText as string
set AppleScript's text item delimiters to OD

--write over the file with new results
writeToEOFileEraseFirst(theFile, finalText)

on tidStuff(paramHere, textHere)
	set OLDtid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to paramHere
	set theItems to text items of textHere
	set AppleScript's text item delimiters to OLDtid
	return theItems
end tidStuff

on writeToEOFileEraseFirst(the_path, theText)
		close access file the_path
	end try
		set file_ref to open for access file the_path with write permission
		set eof file_ref to 0
		write theText to file_ref
		close access file_ref
	on error errMsg
			close access file_ref
			display dialog errMsg
		end try
	end try
end writeToEOFileEraseFirst

would you mind possibly explaining some of that? I kind of want to learn apple script, and learning by doing seems to be the best method.

This will keep you from not having to deal with TextEdit. :wink: It’s probably slower than Craig’s, but will preserve any text styling in the document:

tell application "TextEdit"
	open (choose file)
	tell front document
		tell its text -- NB. 'its'. We need the TextEdit reference, not the text itself.
			delete (paragraphs -3 thru -1)
			set afterTen to (offset of "TEN" in (it as text)) + 3 -- 'it as text' because 'offset' does need the actual text.
			delete (characters 1 thru afterTen)
		end tell
	end tell
end tell

Very nice Nigel. I guess I should have wrote, “I haven’t scripted TextEdit so here’s a really long version to replace something simple.” :stuck_out_tongue:

Thanks for helping rublind with what he actually asked for.



Okay, first of all, thank you.

Now, on to more serious business.

The first script erases the file (well, I know why it does that, but it doesn’t write the data back after).

The second script crashes TextEdit on my system.

Basically I used TEN as an example, don’t know if that is actually going to make a difference. If my text file doesn’t actually contain the word ten, can I replace “ten” with what ever I want so long as it is in my text file? because that is what I did, and what if that word appears multiple times in the text file and I just want to remove up to the first one.

Here is a sample of what my text file looks like.

Poop - Poop
Fri, Jun 20, 2038
Two Tropic game Tang Slime Driven Bait Thyme Messing Graduation Equip.? Late? Here?
LARRY HARRY EN 9:00 AM 75 View/Open
HARRY LARRY EN 10:00 AM 15 View/Open
1-19 of 19

©2038, SBF, Most Rights Reserved.Jimmy Buffet (Talk out)

All I want to keep is everything after “X” and Before “1-19 of 19”

Now the “X” will always be there, but that “1-19 of 19” might be different numbers.

I hope this helps more.

Thanks again

It certainly beachballs it for a long time with your text doesn’t it? I get the impression that TextEdit’s deleting one character at a time, with all the attendant internal housekeeping, before handing back to the script. It actually goes faster (and gives something to watch) if the script itself deletes the characters individually in reverse order!

It certainly helps to know things like that when writing scripts based on them. :wink:

Such things are no problem to script if you know what you want to do. I’ve incorporated user input into the rewrite below, but you can easily “hard wire” any other text you like.

If “X”, “TEN”, or whatever is an entire paragraph, or is at the end of one, you could speed things up by deleting by paragraph rather than by character:

tell application "TextEdit"
	open (choose file)
	display dialog "Up to what text do you want to cut?" default answer ""
	set cutMark to text returned of the result
	if ((count cutMark) is 0) or (cutMark is not in text of document 1) then error number -128
	tell front document
		tell its text
			delete (paragraphs -3 thru -1)
			set i to 1
			repeat until (paragraph i contains cutMark)
				set i to i + 1
			end repeat
			delete (paragraphs 1 thru i)
		end tell
	end tell
end tell

I may be able to figure this out before I get a reply, but How do I specify in apple script a file. Right now you guys have Open (Choose File) but I don’t want to choose a file, I want to have a designated file that the script grabs and does it’s thing to, and I never have to know it happened. I tried changing (Choose File) to the file’s path, but apple script spat back an error saying “expected end but found “/” or expected “,” but found”/".

Hi rublind,

I am not sure why you are getting that error without seeing your code first but here is how
you set the file path.

set theFile to alias "G5:Users:craig:Desktop:text.txt"
tell application "TextEdit"
	open theFile
end tell

A script I use to quickly get full paths to files is below.
Select the file you want the path to and then run the script.
The full path enclosed in quotes will be on your clipboard.
For the most part use HFS. POSIX is for shell scripting.



tell application "Finder"
		set theSelection to a reference to the selection
	on error
		display dialog "You must have something selected." buttons {"OK"} default button 1
		error number -128
	end try
	set thePath to theSelection as alias
	set theChosenPath to button returned of (display dialog "HFS or POSIX." buttons {"POSIX", "HFS"} default button 2)
	if theChosenPath = "POSIX" then
		set thePOSIXpath to POSIX path of thePath
		set thePOSIXpath to "\"" & thePOSIXpath & "\""
		set the clipboard to thePOSIXpath
		set thePath to "\"" & thePath & "\""
		set the clipboard to thePath
	end if
end tell