Applescript 将文件复制到特定文件夹并重命名为x.txt如何打开x.txt并粘贴";X:“;

Applescript 将文件复制到特定文件夹并重命名为x.txt如何打开x.txt并粘贴";X:“;,applescript,Applescript,编辑:regulus6633制作了一个比我下面的大纲好得多的脚本,如果模板文件不是完全空的(我认为这最初是导致错误的),它可以完美地工作。谢谢 此脚本应(1)将x.txt复制到特定文件夹,并将其重命名为新名称,(2)打开它,(3)在所有大写字母中粘贴“新名称”,以及(4)插入“:”然后返回(&return)”。第一部分工作正常,但我很难理解(2)、(3)和(4)。到目前为止我编写的代码粘贴在下面 tell application "Finder" display dialog

编辑:regulus6633制作了一个比我下面的大纲好得多的脚本,如果模板文件不是完全空的(我认为这最初是导致错误的),它可以完美地工作。谢谢

此脚本应(1)将x.txt复制到特定文件夹,并将其重命名为新名称,(2)打开它,(3)在所有大写字母中粘贴“新名称”,以及(4)插入“:”然后返回(&return)”。第一部分工作正常,但我很难理解(2)、(3)和(4)。到目前为止我编写的代码粘贴在下面

 tell application "Finder"
        display dialog "new_name_dialogue" default answer " "
        set new_name to (text returned of result)
        set Selected_Finder_Item to (folder of the front window) as text
        duplicate file "Q:x:7:n7:GTD scripting:template folder:x.txt" to "Q:X:7:SI:SIAG1"
        set Path_Of_X to "Q:X:7:SI:SIAG1:" & "x.txt" as string
        set name of file Path_Of_X to (new_name as text) & ".txt"
#[something that let's me open the file is needed here]
#[something that pastes "new_name" & ":" in ALL CAPS]
#[something that inserts two lineshifts]
    end tell
这里需要一些可以让我打开文件的东西


在所有大写字母中粘贴新名称的东西

有一个非常好的第三方脚本添加()可以用于类似的事情(只有在下载了脚本添加的情况下才有效)


插入两个换行符的东西

这里需要一些可以让我打开文件的东西


在所有大写字母中粘贴新名称的东西

有一个非常好的第三方脚本添加()可以用于类似的事情(只有在下载了脚本添加的情况下才有效)


插入两个换行符的东西


通常,由于您处理的是txt文件,因此不需要在应用程序中“打开”该文件并粘贴文本。我们可以直接从applescript读取和写入文本文件。因此,我们从模板文件中读取文本,添加我们想要的任何文本,然后将新文本写入新文件。如果您想打开并查看新文件,可以在之后再打开并查看。我在代码的“文本编辑”部分这样做了

你可以在脚本的末尾看到,我有一些子程序,可以编写一个文本文件,也可以将文件名更改为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

通常,由于您处理的是txt文件,因此不需要在应用程序中“打开”该文件并粘贴文本。我们可以直接从applescript读取和写入文本文件。因此,我们从模板文件中读取文本,添加我们想要的任何文本,然后将新文本写入新文件。如果您想打开并查看新文件,可以在之后再打开并查看。我在代码的“文本编辑”部分这样做了

你可以在脚本的末尾看到,我有一些子程序,可以编写一个文本文件,也可以将文件名更改为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

哇!非常感谢,它制作精美,可读性很强(即使对于新手来说也是如此),而且非常有效!(我原来的模板文件x.txt完全是空的,我不得不在其中添加一个换行符)哇!非常感谢,它制作精美,可读性很强(即使对于新手来说也是如此),而且非常有效!(我原来的模板文件x.txt完全是空的,我必须在其中输入一行移位才能工作)
return & return
-- 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