Create files and save to a mounted Volume gives error

Hello All,

I am absolutely new to apple script here. I have a basic task of creating a file in Text edit so i found at a MAC website and have been trying to tweak it but to no luck

    on writeTextToFile(theText, theFile, overwriteExistingContent)
	tell application "TextEdit"
		activate
		try
			-- Convert the file to a string
			set theFile to theFile as string
			
			-- Open the file for writing
			set theOpenedFile to open for access file theFile with write permission
			
			-- Clear the file if content should be overwritten
			if overwriteExistingContent is true then set eof of theOpenedFile to 0
			
			-- Write the new content to the file
			write theText to theOpenedFile starting at eof
			
			-- Close the file
			close access theOpenedFile
			
			-- Return a boolean indicating that writing was successful
			return true
			
			-- Handle a write error
		on error
			
			-- Close the file
			try
				close access file theFile
			end try
			
			-- Return a boolean indicating that writing failed
			return false
		end try
	end tell
end writeTextToFile


set this_story to "Hello This Text is blocked"
set Filepath to "Volumes:VolumeName:" as text
set theFile to (((path to Filepath) as string) & "MY STORY.txt")
writeTextToFile(this_story, theFile, true)

This gives me a error cant make \Volume:VolumeName into type constant.
Could any body help in let me know whats wrong here ? I have tried POSIX file command as well but it wasnt helping me out

A couple of quick things. First, to get all of your code inside the code block, there are two simple methods… after pasting your code, select it and click the preformatted text button above — looks like a < / > (or type command-e), or type three backticks (‘`’ — the same key as the tilde (~)) before and after the code.

Second, you have some smart quotes in your code… basically every quote in your last four lines. You can only use straight quotes, i.e. " in applescript.

Third, the ‘constant’ error is because ‘path to’ is only for predetermined locations. You cannot provide your own. You can use (path to desktop) or (path to downloads folder), etc…. So you will have to provide the path yourself. You almost have it already but your syntax is incorrect (see below).

Fourth, you’re mixing up HFS (use :) and posix (use \) file specifiers. If you’re using HFS, then you don’t begin a path with ‘Volumes’… just use the volume name. You don’t need to coerce the volume string to text because it already is text. Nor do you need to coerce the filePath because it also is already text. FYI, text and string mean the same thing within applescript (although it is possible for an application to define them differently but I’m not aware of any that do).

Finally, you are using the write commands within a textedit block but they don’t really have anything to do with each other (except that they both offer means to edit text). As such, I’ve removed the tell block. Ostensibly, the standard practice with applescript is to place handlers below the rest of the code so I’ve done so here.

set this_story to "Hello this text is blocked" & linefeed
set filePath to "VolumeName:"
set theFile to filePath & "my story.txt"

writeTextToFile(this_story, theFile, true)

on writeTextToFile(theText, theFile, overwriteExistingContent)
	try
		-- Open the file for writing
		set theOpenedFile to open for access file theFile with write permission
		-- Clear the file if content should be overwritten
		if overwriteExistingContent is true then set eof of theOpenedFile to 0
		-- Write the new content to the file
		write theText to theOpenedFile starting at eof
		-- Close the file
		close access theOpenedFile
		-- Return a boolean indicating that writing was successful
		return true
		
		-- Handle a write error
	on error
		-- Close the file
		try
			close access file theFile
		end try
		-- Return a boolean indicating that writing failed
		return false
	end try
end writeTextToFile

NB I’m running Sierra so it’s possible that some things have changed in the last decade.

1 Like

Thank you so so much your code works Apologies for not giving more context in the question but i need to work with Text Edit as part of automation task. We are testing out various editors in MacOS with a software. I have a below Snippet that works for Text Edit but was looking for something more generic so that I can add passin more Editors via arguments of function

tell application "TextEdit"

activate

make new document with properties {text:"Hello this is written via Apple Script"}

delay 5

save document 1 in file ("MyDISK:" & "file.txt")

close document  1

end tell

Hey @rahul_2088

If I’m understanding your challenge correctly I don’t think you’ll be able to do exactly what you’re thinking. Every app has it’s own dictionary and therefore the code you write to work with each application will be unique to the application itself.

Below is an example of an approach you might want to try.

on run
	testEditor("TextEdit", "MyDISK:SomeFolder:TextEdit_Test.txt")

	testEditor("BBEdit", "MyDISK:SomeFolder:BBEdit_Test.txt")
end run

on testEditor(editorName, saveLocation)
	if editorName = "TextEdit" then
		tell application "TextEdit"
			activate
			make new document with properties {text:"Hello this is written via Apple Script"}
			save document 1 in file saveLocation
			close document 1
		end tell
		
	else if editorName = "BBEdit" then
		# CODE FOR BBEdit
		
	else if editorName = "XYZEditor" then
		# CODE FOR ANOTHER EDITOR
		
	end if
end testEditor

To follow up on Shai1’s example, here would be the code for BBEdit and for another editor, SubEthaEdit (which is free). The code is very similar but not identical to textedit.

	else if editorName = "BBEdit" then
		# CODE FOR BBEdit
		tell application "BBEdit"
			activate
			make new document with properties {text:"Hello this is written via Apple Script"}
			save document 1 to file saveLocation
			close window 1
		end tell
		
	else if editorName = "SubEthaEdit" then
		# CODE FOR ANOTHER EDITOR
		tell application "SubEthaEdit"
			activate
			make new document with properties {contents:"Hello this is written via Apple Script"}
			save document 1 in file saveLocation
			close document 1
		end tell

Note as well that whichever editor you use, you can still have applescript (or the shell) create a text file in a script and then open the file in the editor of your choice.