An Applescript Text Editor

Hi, all! I’m new to Macscripter.net, but not to AppleScript. I’ve been using it for years.

This is an AppleScript text editor I made in about half an hour. It can edit existing files, but it cannot create new ones.

(* AppleScript TextEdit (ALPHA) *)
set theFile to choose file
set theText to read theFile
display dialog "" default answer theText buttons {"Close", "Save", "Save As..."} default button "Save" with title "Editing " & theFile & "..."
if the button returned of the result is "Save As..." then
	set theNewText to the text returned of the result
	set theNewFile to choose file name
	open for access theNewFile with write permission
	write theNewText to theNewFile starting at 0
	close access theNewFile
else if the button returned of the result is "Save" then
	set theNewText to the text returned of the result
	open for access theFile with write permission
	write theNewText to theFile starting at 0
	close access theFile
end if

P.S. Could somebody tell me if this has been done before? :confused:

Model: Macbook
AppleScript: 2.0.1
Browser: Safari 533.22.3
Operating System: Mac OS X (10.5)

hi,

you may create new ones within a few changes …

set theFile to (path to desktop as text) & "sample.scpt"
set theText to "display dialog \"Some text\"
beep 3







"
display dialog "" default answer theText buttons {"Close", "Save", "Save As..."} default button "Save" with title "Editing " & theFile & "..."
if the button returned of the result is "Save As..." then
	set theNewText to the text returned of the result
	set theNewFile to ((choose file name) as text) & ".scpt"
	open for access file theNewFile with write permission
	write theNewText to file theNewFile starting at 0
	close access file theNewFile
else if the button returned of the result is "Save" then
	set theNewText to the text returned of the result
	open for access file theFile with write permission
	write theNewText to file theFile starting at 0
	close access file theFile
end if

Gee, the way I had it, there was a seperate script for that! :expressionless:

set theFile to choose file name
open for access theFile with write permission
set theText to read theFile
display dialog "" default answer theText buttons {"Close", "Save", "Save As..."} default button "Save" with title "Editing " & theFile & "..." with icon note
if the button returned of the result is "Save As..." then
	set theNewText to the text returned of the result
	set theNewFile to choose file name
	write theNewText to theNewFile starting at 0
	close access theNewFile
else if the button returned of the result is "Save" then
	set theNewText to the text returned of the result
	write theNewText to theFile starting at 0
	close access theFile
end if

Model: Macbook
AppleScript: 2.0.1
Browser: Safari 533.22.3
Operating System: Mac OS X (10.5)

EDIT: I am talking about editing .txt files here.

Something liek this? If the user press cancel when opening a file it assumes you want to create a new file

try
	set theFile to choose file
	set theText to read theFile
	set theTitle to "Editing " & theFile & "..."
	set theButtons to {"Close", "Save", "Save As..."}
on error
	--the user pressed cancel so a new document is created
	set theText to string id {13, 13, 13, 13, 13, 13, 13, 13}
	set theFile to missing value
	set theTitle to "Untitled document"
	set theButtons to {"Close", "Save As..."}
end try

display dialog "" default answer theText buttons theButtons default button 2 with title theTitle

if the button returned of the result is "Save As..." then
	set theNewText to the text returned of the result
	set theNewFile to choose file name
	open for access theNewFile with write permission
	write theNewText to theNewFile starting at 0
	close access theNewFile
else if the button returned of the result is "Save" then
	set theNewText to the text returned of the result
	open for access theFile with write permission
	write theNewText to theFile starting at 0
	close access theFile
end if

Yeeeaaahhhh, I guess… :expressionless:

EDIT: I didn’t actually read the code until now. However, I have not tested it, but somehow it feels wrong.

ANOTHER EDIT: I figured out what’s wrong. Here is an updated version:

try
   set theFile to choose file
   set theText to read theFile
   set theTitle to "Editing " & theFile & "..."
   set theButtons to {"New File", "Save", "Save As..."}
on error
   --the user pressed cancel so a new document is created
   set theText to string id {13, 13, 13, 13, 13, 13, 13, 13}
   set theFile to missing value
   set theTitle to "Untitled document"
   set theButtons to {"Close", "Save As..."}
end try

display dialog "" default answer theText buttons theButtons default button 2 with title theTitle

if the button returned of the result is "Save As..." then
   set theNewText to the text returned of the result
   set theNewFile to choose file name
   open for access theNewFile with write permission
   write theNewText to theNewFile starting at 0
   close access theNewFile
else if the button returned of the result is "Save" then
   set theNewText to the text returned of the result
   open for access theFile with write permission
   write theNewText to theFile starting at 0
   close access theFile
end if

Nope my code works better. Newfile is never presented until you want to save and that’s wrong :)… The titles of the buttons in the open dialog can’t be altered so I’ve chosen to make the cancel butten act as ‘new file’.

But that doesn’t make sense! Wouldn’t you want the CLOSE button to CLOSE the script?

P.S. Revised edition of my version allowing you to choose whether to edit a file OR make a new one.

(* AppleScript TextEdit version 0.0.1 (BETA)

You are free to use and modify this code as long as you give credit.
*)
display dialog "Please make a selection." with title "AppleScript Text Editor v0.0.1 BETA" buttons {"Cancel", "New File", "Edit File"} default button 3 with icon note
if the button returned of the result is 3 then
	set theFile to choose file
	set theText to read theFile
	display dialog "" default answer theText buttons {"Close", "Save", "Save As..."} default button "Save" with title "Editing " & theFile & "..."
	if the button returned of the result is "Save As..." then
		set theNewText to the text returned of the result
		set theNewFile to choose file name
		open for access theNewFile with write permission
		write theNewText to theNewFile starting at 0
		close access theNewFile
	else if the button returned of the result is "Save" then
		set theNewText to the text returned of the result
		open for access theFile with write permission
		write theNewText to theFile starting at 0
		close access theFile
	end if
else
	set theFile to choose file name
	open for access theFile with write permission
	set theText to read theFile
	display dialog "" default answer theText buttons {"Close", "Save", "Save As..."} default button "Save" with title "Editing " & theFile & "..." with icon note
	if the button returned of the result is "Save As..." then
		set theNewText to the text returned of the result
		set theNewFile to choose file name
		write theNewText to theNewFile starting at 0
		close access theNewFile
	else if the button returned of the result is "Save" then
		set theNewText to the text returned of the result
		write theNewText to theFile starting at 0
		close access theFile
	end if
end if

EDIT: I plan to have other features (like displaying the number of bytes, among other things) in a future release. Stay glued to your monitor :wink:

That’s how it should work indeed but the code in post #5 didn’t make sense at all. Also some minor adjustments how the code work more efficient the result should be the same:

(* AppleScript TextEdit version 0.0.1 (BETA)

You are free to use and modify this code as long as you give credit.
*)
set x to display dialog "Please make a selection." with title "AppleScript Text Editor v0.0.1 BETA" buttons {"Cancel", "New File", "Edit File"} default button 3 with icon note

if button returned of x = "Edit File" then
	set theFile to choose file
	set theText to read theFile
	set theTitle to "Editing " & theFile & "..."
	set theButtons to {"New File", "Save", "Save As..."}
else if button returned of x = "New File" then
	--the user pressed cancel so a new document is created
	set theText to string id {13, 13, 13, 13, 13, 13, 13, 13}
	set theFile to missing value
	set theTitle to "Untitled document"
	set theButtons to {"Close", "Save As..."}
else
	return --needed when someone localize this script
end if

set x to display dialog "" default answer theText buttons theButtons default button 2 with title theTitle
set newText to text returned of x

if the button returned of x is "Save As..." then
	set theFile to choose file name
else if the button returned of x is "Close" then
	return
end if

try
	set fd to open for access theFile with write permission
	write newText to fd starting at 0
	close access fd
on error x
	--if something went wrong, make sure the file is closed
	close access theFile
	display alert "File Error:" message "Can't write data to the file because of the following error:" & return & x as critical
end try

There is a grammar error on line 19 of your code. However, it is in a comment, so it doesn’t affect operation.

(It bothers me when someone makes a spelling/grammar error online :wink: )

Version 0.0.2 is available!

New features:

¢ More efficient handling of creation of new files

¢ Length of file is now displayed above the file’s text

¢ Error handling

Thanks to DJ Bazzie Wazzie for error handling and new file creation handling.

(* AppleScript TextEdit version 0.0.2 (BETA)

You are free to use and modify this code as long as you give credit.
*)
set x to display dialog "Please make a selection." with title "AppleScript Text Editor v0.0.1 BETA" buttons {"Cancel", "New File", "Edit File"} default button 3 with icon note

if button returned of x = "Edit File" then
	set theFile to choose file
	set theEOF to get eof of theFile
	set theDialogText to "(File is " & theEOF & " bytes long)"
	set theText to read theFile
	set theTitle to "Editing " & theFile & "..."
	set theButtons to {"Cancel", "Save", "Save As..."}
else if button returned of x = "New File" then
	--the user pressed cancel so a new document is created
	set theText to string id {13, 13, 13, 13, 13, 13, 13, 13}
	set theDialogText to ""
	set theFile to missing value
	set theTitle to "Untitled document"
	set theButtons to {"Close", "Save As..."}
else
	return --needed when someone localizes this script
end if

set x to display dialog theDialogText default answer theText buttons theButtons default button 2 with title theTitle
set newText to text returned of x

if the button returned of x is "Save As..." then
	set theFile to choose file name
else if the button returned of x is "Close" then
	return
end if

try
	set fd to open for access theFile with write permission
	write newText to fd starting at 0
	close access fd
on error x
	--if something went wrong, make sure the file is closed
	close access theFile
	display alert "File Error:" message "Can't write data to the file because of the following error:" & return & x as critical
end try

Features planned in the next version include using a list instead of buttons on the main screen, among others. Stay glued to your screen :wink:

No harm feelings here, but as defense I would like to say that English isn’t my native language. So be prepared for more innocent grammar errors from me :D…

In the future you could add some sort of main event loop in your application. It should carry the name of main event loop because it’s too small. What you’ll can do with an main event loop is:
¢ When writing to the file went wrong, show the dialog with the unsaved text again
¢ When the user has finished his file, ask him what to do next like quit, new file or open a file.
¢ Check if the file that needs to be opened is not binary; if so show the open dialog again.

No harm feelings? What you’ll can do? :confused:

Version 0.0.3 is available!

Features added:

¢ Main script now in repeat loop

¢ Unsaved text is now copied to the clipboard when an error occurs in saving

¢ Version and release status now are contained in variables

¢ Length of file being edited is now displayed

¢ Forgot to update displayed version in version 0.0.2 (whoops!) This version fixes that.

The next version is coming up soon. Stay glued to your monitor :wink:

(* AppleScript TextEdit version 0.0.3 (BETA)

You are free to use and modify this code as long as you give credit.
*)
set scriptVersion to "0.0.3"
set ReleaseStatus to "BETA"
repeat
	set x to display dialog "Please make a selection." with title "AppleScript Text Editor v" & scriptVersion & ReleaseStatus buttons {"Cancel", "New File", "Edit File"} default button 3 with icon note
	
	if button returned of x = "Edit File" then
		set theFile to choose file
		set theEOF to get eof of theFile
		set theDialogText to "(File is " & theEOF & "
bytes long)"
		set theText to read theFile
		set theTitle to "Editing " & theFile & "..."
		set theButtons to {"Close", "Save", "Save As..."}
	else if button returned of x = "New File" then
		--the user pressed cancel so a new document is created
		set theText to string id {13, 13, 13, 13, 13, 13, 13, 13}
		set theDialogText to ""
		set theFile to missing value
		set theTitle to "Untitled document"
		set theButtons to {"Close", "Save As..."}
	else
		return --needed when someone localizes this script
		quit
	end if
	
	set x to display dialog theDialogText default answer theText buttons theButtons default button 2 with title theTitle
	set newText to text returned of x
	
	if the button returned of x is "Save As..." then
		set theFile to choose file name
	else if the button returned of x is "Close" then
		return
		quit
	end if
	
	try
		set fd to open for access theFile with write permission
		write newText to fd starting at 0
		close access fd
	on error x
		--if something went wrong, make sure the file is closed
		close access theFile
		display alert "File Error:" message "Can't write data to the file because of the following error:" & return & x as critical
	end try
end repeat

EDIT: This time I forgot to update the version in the comment at the top (whoops again!)

… Hello? Anybody?

[echo]

Sigh…