В общем, поскольку вы имеете дело с TXT-файлом, вам не нужно «открывать» файл в приложении и вставлять текст. Мы можем читать и писать в текстовые файлы прямо из applecript. Таким образом, мы читаем текст из файла шаблона, добавляем к нему любой текст, который хотим, а затем записываем новый текст в новый файл. Если вы хотите открыть и просмотреть новый файл, вы можете сделать это после. Я сделал это в разделе «TextEdit» кода.
Вы можете увидеть в конце скрипта, у меня есть подпрограммы для написания текстового файла, а также для изменения имени файла на CAPS. Поэтому попробуйте следующее ...
-- initial variables
set templateFile to "Q:x:7:n7:GTD scripting:template folder:x.txt"
set copyFolder to "Q:X:7:SI:SIAG1:" -- notice this path ends in ":" because it's a folder
-- get the new name
display dialog "new_name_dialogue" default answer ""
set newName to (text returned of result)
set newPath to copyFolder & newName
-- get the text of the template file
set templateText to read file templateFile
-- add the file name in CAPS, a colon, and 2 returns at the beginning of templateText
set capsName to upperCase(newName)
set newText to capsName & ":" & return & return & templateText
-- write the newText to newPath
writeTo(newPath, newText, text, false)
-- open the new file in textedit
tell application "TextEdit" to open file newPath
(*============== SUBROUTINES ==============*)
on writeTo(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as text
if targetFile does not contain ":" then set targetFile to POSIX file targetFile as text
set openFile to open for access file targetFile with write permission
if apendData is false then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeTo
on upperCase(theText)
set chrIDs to id of theText
set a to {}
repeat with i from 1 to (count of chrIDs)
set chrID to item i of chrIDs
if chrID ≥ 97 and chrID ≤ 122 then set chrID to (chrID - 32)
set end of a to chrID
end repeat
return string id a
end upperCase