Dopo aver letto la tua domanda, ecco un esempio di come lo codificherei.
-- # 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
Come creare il modello di documento personalizzato RTF, customRTFDocumentTemplate
, utilizzato nel on createCustomRTFDocument()
gestore :
In TextEdit creare un nuovo documento Rich Text e impostare il carattere e la dimensione come desiderato, quindi aggiungere qualsiasi contenuto predefinito, in questo caso una nuova riga. Fatto ciò, premi ⌘A per selezionare tutto, quindi ⌘C per copiare queste informazioni negli Appunti .
Ora in Script Editor usa il seguente comando per ottenere i dati della classe RTF dagli appunti .
get the clipboard as «class RTF »
Ciò che viene restituito verrà utilizzato come modello di documento RTF personalizzato assegnandolo a una variabile nello script effettivo , ad esempio:
set customRTFDocumentTemplate to «data RTF 7B5C727466315C616E73695C616E7369637067313235325C636F636F61727466313530345C636F636F617375627274663736300A7B5C666F6E7474626C5C66305C6673776973735C6663686172736574302048656C7665746963613B7D0A7B5C636F6C6F7274626C3B5C7265643235355C677265656E3235355C626C75653235353B7D0A7B5C2A5C657870616E646564636F6C6F7274626C3B3B7D0A5C706172645C74783732305C7478313434305C7478323136305C7478323838305C7478333630305C7478343332305C7478353034305C7478353736305C7478363438305C7478373230305C7478373932305C7478383634305C7061726469726E61747572616C5C7061727469676874656E666163746F72300A0A5C66305C66733336205C636630205C0A7D»
Si noti che il modello precedente ha il carattere predefinito, Helvetica, con dimensioni impostate su 18 e una riga vuota come contenuto. Se vuoi un font diverso da Helvetica, imposta sia il carattere che la dimensione prima di aggiungere il contenuto, una riga vuota in questo caso, quindi usa le informazioni sopra per ottenere è come dati della classe RTF da appunti .
Inoltre, nel on openDocument()
gestore , la bounds
lista è impostata su {160, 22, 883, 639}
non {160, 10, 883, 639}
come secondo elemento nella lista bounds
non dovrebbe essere inferiore all'altezza della barra dei menu.