a quick way to create text files from a dialog box

I wanted to make a larger dialog box where you can enter text (i.e. with ‘default answer’) and figured out that I could do that by using return characters as the default answer. The more return characters I used the larger the box got. After figuring this out it occurred to me that I could write an applescript that would essentially be an easy way to create a text document with this technique… so that’s what I did. The following is the result and it works pretty well.

Anyway, maybe you’ll find it useful and please post any improvements to it!

-- this is an easy way to create a quick text file using applescript by typing something or from the clipboard contents
-- the text is entered into a big display dialog so you can easily see what you are writing
-- Note: that using the backspace key when creating the note causes some visual problems but still works
-- Note: you can scroll in the dialog box with a scroll wheel or using the mouse up/down arrows
-- Note: a bunch of return characters are used to make the dialog box larger and the script will automatically remove the return characters from the end of the text for you

property applicationName : "Quick Note"
property dialogText : "Enter some text or get the text from the clipboard:"
property defaultLocation : path to desktop folder

property btn1 : "Clipboard Contents"
property btn2 : "Quit"
property btn3 : "Save"

-- create the text
set defaultAnswer to ""
repeat
	tell me to activate
	tell me to display dialog dialogText default answer defaultAnswer & return & return & return & return & return & return & return & return & return & return & return & return & return & return & return & return & return & return & return & return & return & return & return buttons {btn1, btn2, btn3} cancel button 2 with title applicationName
	set {theText, theButton} to {text returned, button returned} of result
	if theButton is btn1 then
		set defaultAnswer to (the clipboard as text)
	else
		exit repeat
	end if
end repeat
set theText to removeTrailingReturns(theText)
if theText is "" then return -- we don't save anything if there is no text to save

-- get the file path where you want the file saved
set defaultName to currentdate_for_filename() & ".txt"
choose file name default location defaultLocation default name defaultName
set fileSpec to result

-- get the container of the fileSpec so we can save that property for where to save the note next time
set defaultLocation to containerFolderAlias_ofPath(fileSpec)

-- write the file
set theResult to writeTo(theText, fileSpec, false, string)
if theResult is false then display dialog "There was an error writing the file!" buttons {"OK"} default button 1 with icon stop




(*============== SUBROUTINES ===============*)
on writeTo(this_data, target_file, append_data, mode) -- append_data is true or false, mode is string etc. (no quotes around either)
	try
		set target_file to target_file as Unicode text
		if target_file does not contain ":" then set target_file to POSIX file target_file as Unicode text
		set the open_target_file to open for access file target_file with write permission
		if append_data is false then set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof as mode
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end writeTo

on removeTrailingReturns(someText)
	repeat while someText ends with return
		set someText to text 1 thru -2 of someText -- lop off last character.
	end repeat
	return someText
end removeTrailingReturns

on containerFolderAlias_ofPath(thePath)
	try
		set pathString to thePath as Unicode text
		set text item delimiters to ":"
		set containerFolder to (text items 1 thru -2 of pathString) as text
		set text item delimiters to ""
		set containerFolder to containerFolder as alias
	on error
		set containerFolder to path to desktop folder
	end try
	return containerFolder
end containerFolderAlias_ofPath

on currentdate_for_filename()
	set now to current date
	set A to makeItTwoDigits(year of now)
	set b to makeItTwoDigits(month of now as integer)
	set c to makeItTwoDigits(day of now)
	set d to makeItTwoDigits(hours of now)
	set e to makeItTwoDigits(minutes of now)
	set f to makeItTwoDigits(seconds of now)
	set currentdate_for_filename to A & b & c & "T" & d & e & f
	return currentdate_for_filename
end currentdate_for_filename

on makeItTwoDigits(A)
	set twoDigits to A as Unicode text
	if (count of twoDigits) is less than 2 then set twoDigits to "0" & twoDigits
	if (count of twoDigits) is greater than 2 then set twoDigits to text -2 thru -1 of twoDigits
	return twoDigits
end makeItTwoDigits

I have a similar script (same idea, first posted in the bbs by Kai Edwards) which is a logger (a personal twitter), except that if the document (which is date stamped) already exists, the new material is time-stamped and added to the earlier material. One addition to yours that I found useful:

to cleanText(tText)
	-- remove extraneous returns from end of tText originally added to extend the text box in the dialog and make it accept returns in the text. Otherwise typed additions might follow several blank lines from the previous entry.
	repeat while tText ends with return
		set tText to text 1 thru -2 of tText -- lop off last character.
	end repeat
	return tText
end cleanText

Well, you know what they say… “Ideas are never new, they’re just old ideas recycled” or something like that. :smiley:

I already had a handler to remove the trailing returns, but I liked the code of the handler you listed better so I changed my code to use it. The idea to use the date as the file name is a good one too so I added that to the script also”it uses the time stamp as well as the date stamp so there’s no conflict to worry about. Do you have a link to that old thread, I did a quick search and didn’t find it. I’d like to see if it evolved into anything else. I wonder what else everyone came up with!

It’s in ScriptBuilders, Regulus – it’s targeted to Journler, though it could easily be changed to TextEdit. It’s called Journal Logger

Edit:

or did you mean the thread with the dialog box?

Hi. Run these two lines in an applescript all by themself…

choose file name --default location defaultLocation default name defaultName
set fileSpec to result as text

Choose the name and location that you want it to be. Copy the result from the bottom of AppleScript Editor and replace those 2 lines in the code with this… obviously enter your copied text into the path…

set fileSpec to "copy:your:result:here"