Después de leer tu pregunta, este es un ejemplo de cómo lo codificaría.
-- # The variables for the target file's fully qualified pathname and custom filename needs to be global as they are called from both the handlers and other code.
global theCustomRichTextFilePathname
global customFilename
-- # The createCustomRTFDocument handler contains a custom template for the target RTF document.
on createCustomRTFDocument()
tell current application
set customRTFDocumentTemplate to «data RTF 7B5C727466315C616E73695C616E7369637067313235325C636F636F61727466313530345C636F636F617375627274663736300A7B5C666F6E7474626C5C66305C6673776973735C6663686172736574302048656C7665746963613B7D0A7B5C636F6C6F7274626C3B5C7265643235355C677265656E3235355C626C75653235353B7D0A7B5C2A5C657870616E646564636F6C6F7274626C3B3B7D0A5C706172645C74783732305C7478313434305C7478323136305C7478323838305C7478333630305C7478343332305C7478353034305C7478353736305C7478363438305C7478373230305C7478373932305C7478383634305C7061726469726E61747572616C5C7061727469676874656E666163746F72300A0A5C66305C66733336205C636630205C0A7D»
try
set referenceNumber to open for access theCustomRichTextFilePathname with write permission
write customRTFDocumentTemplate to referenceNumber
close access referenceNumber
on error eStr number eNum
activate
display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title "File I/O Error..." with icon caution
try
close access referenceNumber
end try
return
end try
end tell
end createCustomRTFDocument
-- # The openDocument handler opens and set the bounds of the theCustomRichTextFilePathname document while placing the cursor on the second line.
on openDocument()
try
tell application "TextEdit"
open file theCustomRichTextFilePathname
activate
tell application "System Events"
set displayedName to get displayed name of file theCustomRichTextFilePathname
if displayedName contains ".rtf" then
tell application "TextEdit"
set bounds of window (customFilename & ".rtf") to {160, 22, 883, 639}
end tell
key code 125
else
tell application "TextEdit"
set bounds of window customFilename to {160, 22, 883, 639}
end tell
key code 125
end if
end tell
end tell
on error eStr number eNum
activate
display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with icon caution
return
end try
end openDocument
-- # Get the name for the RTF document, ensuring it is not blank.
repeat
set customFilename to the text returned of (display dialog "Save as:" with title "Do you want to create a new, blank TextEdit RTF document?" default answer "")
if customFilename is "" then
beep
display alert "The filename cannot be empty!" message "Please enter a name to continue..."
else
exit repeat
end if
end repeat
-- # Concatenate the default location (the User's Desktop) with the chosen filename while adding the proper file extension.
set theCustomRichTextFilePathname to ((path to desktop) & customFilename & ".rtf") as string
-- # Check to see if the target file already exists. If it does not exist, create and open it. If it does exist, either open it or overwrite it and open it, based on decision made.
tell application "Finder"
try
if exists file theCustomRichTextFilePathname then
tell current application
display dialog "The file \"" & POSIX path of theCustomRichTextFilePathname & "\" already exists!" & return & return & "Do you want to overwrite the file?" & return & return & "If yes, the file will be placed in the Trash." buttons {"No", "Yes"} default button 1 with title "File Already Exists..." with icon caution
if the button returned of result is "No" then
-- # The file already exists, chose not to overwrite it, just open the document.
my openDocument()
else
-- # The file already exists, chose to overwrite it, then open the document.
tell application "Finder"
delete the file theCustomRichTextFilePathname
end tell
my createCustomRTFDocument()
my openDocument()
end if
end tell
else
-- # The file does not already exist. Create and open the document.
tell current application
my createCustomRTFDocument()
my openDocument()
end tell
end if
on error eStr number eNum
activate
display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with icon caution
return
end try
end tell
Cómo crear la plantilla de documento RTF personalizada, customRTFDocumentTemplate
, utilizada en on createCustomRTFDocument()
handler:
En TextEdit, cree un nuevo documento de texto enriquecido y configure la fuente y el tamaño que desee, luego agregue cualquier contenido predeterminado, en este caso una nueva línea. Una vez hecho esto, presione ⌘A para seleccionar todo, luego ⌘C para copiar esta información en el Portapapeles .
Ahora en Script Editor, use el siguiente comando para obtener datos de clase RTF del Portapapeles .
get the clipboard as «class RTF »
Lo que se devuelva se utilizará como Plantilla de documento RTF personalizada asignándola a una variable en el script real, por ejemplo:
set customRTFDocumentTemplate to «data RTF 7B5C727466315C616E73695C616E7369637067313235325C636F636F61727466313530345C636F636F617375627274663736300A7B5C666F6E7474626C5C66305C6673776973735C6663686172736574302048656C7665746963613B7D0A7B5C636F6C6F7274626C3B5C7265643235355C677265656E3235355C626C75653235353B7D0A7B5C2A5C657870616E646564636F6C6F7274626C3B3B7D0A5C706172645C74783732305C7478313434305C7478323136305C7478323838305C7478333630305C7478343332305C7478353034305C7478353736305C7478363438305C7478373230305C7478373932305C7478383634305C7061726469726E61747572616C5C7061727469676874656E666163746F72300A0A5C66305C66733336205C636630205C0A7D»
Tenga en cuenta que la plantilla anterior tiene la fuente predeterminada, Helvetica, con un tamaño establecido en 18 y una línea en blanco como contenido. Si desea una fuente diferente, entonces Helvetica, establezca la fuente y el tamaño antes de agregar contenido, una línea en blanco en este caso, luego use la información de arriba para obtener los datos de RTF class del Portapapeles .
También tenga en cuenta que en el on openDocument()
handler , la lista bounds
se establece en {160, 22, 883, 639}
no {160, 10, 883, 639}
como el segundo elemento en la lista bounds
no debe ser menor que la altura de la barra de menú.