Unable to access the file in shared folder using text edit

I have a file in shared folder I need to update that file and save it in a volume. Below is the one I have come up with so far .

set shareFolderPath to POSIX path of (path to home folder as text) & “share/”

set testFilePath to shareFolderPath & “dummy.txt”

tell application “TextEdit”

activate

open file testFilePath

set text of document 1 to “This is updated content.”

set helloFolderPath to POSIX path of “/Volumes/HELLO/”

set helloFilePath to helloFolderPath & “newdummy.txt”

save document 1 in file helloFilePath

close document 1

end tell

error “TextEdit got an error: Can’t get document 1. Invalid index.” number -1719 from document 1 with a prompt saying the text doesnt exist (But it does)

I keeping getting this error could any one help in telling me whats wrong in the above script ?

@rahul_2088. The following worked on my Ventura computer. I changed the helloFolderPath just for testing but the script also worked for me on an external volume.

set shareFolderPath to (path to home folder as text) & "share:"
set testFilePath to shareFolderPath & "dummy.txt"
set helloFolderPath to (path to desktop as text) & "HELLO:"
# set helloFolderPath to "Store:Test:HELLO:" -- this worked in my testing
set helloFilePath to helloFolderPath & "newdummy.txt"

tell application "TextEdit"
	activate
	open file testFilePath
	set existingText to text of document 1
	set newText to text of document 1 & " This is updated contents."
	set text of document 1 to newText
	save document 1 in file helloFilePath
	close document 1
end tell

FWIW, the following is similar to the above but doesn’t use TextEdit. It updates both the source and destination text files.

set shareFolderPath to (path to home folder as text) & "share:"
set testFilePath to shareFolderPath & "dummy.txt"
set helloFolderPath to (path to desktop as text) & "HELLO:"
set helloFilePath to helloFolderPath & "newdummy.txt"
set theString to read file testFilePath
set newString to theString & "This is updated contents."

repeat with aFile in {testFilePath, helloFilePath}
	set openedFile to (open for access file aFile with write permission)
	try
		set eof openedFile to 0
		write newString to openedFile as «class utf8»
		close access openedFile
	on error errMsg number errNbr
		close access openedFile
		error errMsg number errNbr
	end try
end repeat

@peavine thank you for the reply when I run your script. I get
The document “ dummy.txt” could not be exported as “ newdummy.txt”. The file doesn’t exist.
Is there something wrong here. These are few requirements that i need to run some save as new file to a EXTERNAL Volume called HELLO these scenarios for a automation so I need to use editor like Text edit

I retested my TextEdit script with both paths (on the desktop and on an external drive) and it worked as expected. Did you try the script while saving the new text file to the desktop (create HELLO folder first)?

set shareFolderPath to (path to home folder as text) & "share:"
set testFilePath to shareFolderPath & "dummy.txt"
set helloFolderPath to (path to desktop as text) & "HELLO:"
# set helloFolderPath to "Store:Test:HELLO:" -- this worked in my testing
set helloFilePath to helloFolderPath & "newdummy.txt"

tell application "TextEdit"
	activate
	open file testFilePath
	set existingText to text of document 1
	set newText to text of document 1 & " This is updated contents."
	set text of document 1 to newText
	save document 1 in file helloFilePath
	close document 1
end tell

BTW, if you are saving the new text file to an external volume (and not a folder on that volume), I believe helloFolderPath should be set to “HELLO:” I tested this with a volume on an external drive and it worked OK.

1 Like