TextEdit - Delete First Paragraph

simple question, how do I refer to the first paragraph of a textedit document, and how do I delete it?

I have tried:

open filePath
	tell application "TextEdit"
		activate
		tell front document
			
			delete paragraph 1
		end tell
	end tell

but I get errors…

david

Hi dklucznik,

You script works here.

Edited: maybe there’s something wrong with your file path variable. What kind of errors?

gl,
kel

Model: MBP
AppleScript: AS 2.2.4
Browser: Safari 536.28.10
Operating System: Mac OS X (10.8)

for what it’s worth, this is how i prompt…

 	tell application "Finder"
	set my_dialog_result to display dialog "Job Number?" default answer ""
	set JobNum to the text returned of my_dialog_result
	set fileName to JobNum & ".txt"
	set filePath to "Datafile:DigitalPress:" & fileName
	
	open filePath
	tell application "TextEdit"
		activate
		tell front document
			
			delete paragraph 1
		end tell
	end tell
end tell

and that generates the error:

TextEdit got an error: Can’t get document 1. Invalid index.

david

Hi,

I recommend to check first for the existence of the file
and then let TextEdit open the file


set my_dialog_result to display dialog "Job Number?" default answer ""
set JobNum to the text returned of my_dialog_result
set filePath to "Datafile:DigitalPress:" & JobNum & ".txt"
tell application "Finder" to set fileExists to exists file filePath
if fileExists then
	tell application "TextEdit"
		activate
		open filePath
		delete paragraph 1 of front document
	end tell
else
	display dialog "File " & quote & filePath & quote & "does not exist" buttons {"Cancel"} default button "Cancel"
end if


ok that works… thanks…
next question, if I add a SAVE and a CLOSE my paragraph gets deleted, but the file doesn’t close…
if I remove the SAVE and put CLOSE FRONT DOCUMENT instead, the file opens, but doesn’t delete the paragraph, but doe close. So all I see is the file open for a brief second and then close. Is it possible that it’s closing so fast that it isn’t getting time to delete the paragraph? That doesn’t really make sense though.

if fileExists then
	tell application "TextEdit"
		activate
		open filePath
		delete paragraph 1 of front document
		save
		close
	end tell
else
	display dialog "File " & quote & filePath & quote & "does not exist" buttons {"Cancel"} default button "Cancel"
end if

Hi,

when you’re performing save and close operations with keyboard shortcuts or the mouse
you will notice that always a document is closed and saved, not the application.


tell application "TextEdit"
	activate
	open filePath
	tell front document
		delete paragraph 1
		save
		close
	end tell
end tell


. because the direct communication with TextEdit allows it to send an OK to the script when the document’s open and ready. I also find that certain “You don’t have permission” errors can be avoided if a full file specifier’s used instead of just the path.

Another precaution ” though not really necessary here ” would be to use the reference returned by the (TextEdit’s) ‘open’ command instead of ‘front document’:


set my_dialog_result to display dialog "Job Number?" default answer ""
set JobNum to the text returned of my_dialog_result
set filePath to "Datafile:DigitalPress:" & JobNum & ".txt"
tell application "Finder" to set fileExists to exists file filePath
if fileExists then
	tell application "TextEdit"
		activate
		tell (open file filePath) -- NB. 'open' + file specifier. Use the result instead of 'front document'.
			delete paragraph 1
			save
			close
		end tell
	end tell
else
	display dialog "File " & quote & filePath & quote & "does not exist" buttons {"Cancel"} default button "Cancel"
end if

TextEdit’s ‘close’ command has a ‘saving’ parameter which doesn’t seem to work here because TextEdit doesn’t notice that the document’s been edited. :confused:

awesome… thanks… that works…
david

ok… now on to my next two problems…

first, once I have deleted that first paragraph from the file and saved and closed it, I then need to copy the file to a specific folder location on the network. so…

tell application "Finder"
	duplicate filePath to folder "Jobs:4colors:"
end tell

That appears to copy the file over, I see it in the directory, but it’s grayed out. And I get a message:

error “Finder got an error: Connection is invalid.” number -609

That’s one issue…

The second item, every one of these files starts out with “order_num” I want to check first to make sure that is the start of the file. If it isn’t there, that means the first paragraph has already been deleted, and I can just move the file over.

THoughts?
david

Here’s what I ended up with. I am not totally convinced it will work all the time…

set my_dialog_result to display dialog "Job Number?" default answer ""
set JobNum to the text returned of my_dialog_result
set filePath to "Datafile:DigitalPress:" & JobNum & ".txt"
tell application "Finder" to set fileExists to exists file filePath
if fileExists then
	tell application "TextEdit"
		activate
		tell (open file filePath) -- NB. 'open' + file specifier. Use the result instead of 'front document'.
			set myWord to first word of paragraph 1
			if myWord = "order" then
				delete paragraph 1
				--display dialog "deleted header"
				save
				close
			else
				close
			end if
		end tell
	end tell
else
	display dialog "File " & quote & filePath & quote & "does not exist" buttons {"Cancel"} default button "Cancel"
end if

tell application "Finder"
	activate
	--display dialog filePath
	duplicate file filePath to folder "Jobs:4colors:"
end tell

OK, as I said… I don’t trust this…
More often than not, if the script has to delete the first paragraph then I get that error:

error “Finder got an error: Connection is invalid.” number -609

It does move a file over, but it’s grayed out. If I run the script again, it does everything correctly and the moved file gets processed.

Do I need to run something that ensures that the file path where I want to move the file to exists? even though I know it already does.

david

I haven’t followed this thread, but if what I have grasped is correct, that the file resides on a different volume, and you access it by an alias, then I find it natural that it won’t work the first time, all of the time, since the network connection has to be resolved.
The skeleton below may resolve it, (hopefully, as I haven’t tested it).

set ctr to 0
repeat
	try
		with timeout of 180 seconds
			tell application "Finder"
				set found_it to exists file "myfile.txt"
			end tell
		end timeout
		exit repeat
	on error e number n
		set ctr to ctr + 1
		if ctr = 3 then
			error "Network must really be down!"
		end if
	end try
end repeat
# ready to process the found file

Here’s the outline of what I am doing. We have a digital press and I make templates on an off-line rip.
As we set up proofs for the customers, I use a data file that has the headers in it. Once the job is approved, i make a template of the job on the RIP. The data file I actually use on the RIP to run the job (not proof it) cannot have headers on it. So I am writing a script that will take a job number, find the data file, check to see if the headers are present. If the header is present, then I delete the header, save the file, and copy it to the hot folder on the RIP. If the header is not present, then I just copy the file over to the hot folder.

The problem seems to be if I have to delete the header, that’s when I can’t copy the file over to the RIP hot folder. If the header has already been deleted, and I just have to move the file over, that’s when it always works the first time, which is why, when I run the script the second time on the same file, it copies over without problem.

Mention was made of accessing the files by an alias… am I not referring to the files correctly in my script? I never have grasped the concept of the alias file name.

The first thing I test for in the script, which I didn’t show, was that the network connection is there…
so I know that the network connection is there.


tell application "Finder"
	
	set volume1_ to "Datafile"
	set volume2_ to "Jobs"
	if (list disks) does not contain volume1_ then
		display dialog "Mounting Datafile"
		
		mount volume "smb://wss3945n/Datafile"
		display dialog "volume is now mounted"
	else
		--display dialog "volume is already mounted"
	end if
	if (list disks) does not contain volume2_ then
		display dialog "Mounting Jobs"
		
		mount volume "smb://192.168.1.176"
		display dialog "volume is now mounted"
	else
		--display dialog "volume is already mounted"
	end if
	
end tell
set my_dialog_result to display dialog "Job Number?" default answer ""
set JobNum to the text returned of my_dialog_result
set JobTemplate to JobNum & ".jlt"
set TemplatePath to "Jobs:4colors:template:" & JobTemplate
set filePath to "Datafile:DigitalPress:" & JobNum & ".txt"
tell application "Finder" to set templateExists to exists file TemplatePath

if templateExists then
	
else
	display dialog "missing template"
end if

tell application "Finder" to set fileExists to exists file filePath
if fileExists then
	tell application "TextEdit"
		activate
		tell (open file filePath) -- NB. 'open' + file specifier. Use the result instead of 'front document'.
			set myWord to first word of paragraph 1
			if myWord = "order" then
				delete paragraph 1
				--display dialog "deleted header"
				save
				close
			else
				close
			end if
		end tell
	end tell
else
	display dialog "File " & quote & filePath & quote & "does not exist" buttons {"Cancel"} default button "Cancel"
end if

tell application "Finder"
	activate
	--display dialog filePath
	duplicate file filePath to folder "Jobs:4colors:"
end tell

Does it work now?

Nope…still does the same thing… if I delete the header, then I get an invalid connection error. If the header was already deleted, then it moves the file over without an error.

david

Bizarre

As TextEdit autoSaves, I assumed that the Save instruction was no longer required.
It appears that I was wrong.

KOENIG Yvan (VALLAURIS, France) mardi 2 juillet 2013 15:53:44

This is a shot in the dark, and may not work:

Please do try to copy the file from the network volume to somewhere on your machine, perform the changes on the local file, then copy the file back to the Network volume.

The reason for trying this, is that I suspect TextEdit to be the culprit, or said more technically, that a file specifier leading to the network path is still open from TextEdit, when Finder tries to save/copy the file, and therefore you get a share/privilege violation.