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