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?
This will keep you from having to deal with TextEdit.
Adjust as necessary to fit your exact needs.
Cheers,
Craig
--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)
try
close access file the_path
end try
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
try
close access file_ref
display dialog errMsg
end try
end try
end writeToEOFileEraseFirst
This will keep you from not having to deal with TextEdit. 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
save
close
end tell
end tell
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
Tome
Frankandbeans
chledding
Repers
Costmers
Gigglyflops
X
Fri, Jun 20, 2038
Two Tropic game Tang Slime Driven Bait Thyme Messing Graduation Equip.? Late? Here?
TWO WORDS
COULD BE UP TO TEN WORDS
LARRY HARRY EN 9:00 AM 75 View/Open
TWO WORDS
MULTIPLE WORDS
HARRY LARRY EN 10:00 AM 15 View/Open
1-19 of 19
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.
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
save
close
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”/".
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.
Cheers,
Craig
tell application "Finder"
try
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
else
set thePath to "\"" & thePath & "\""
set the clipboard to thePath
end if
end tell