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

时间:2011-09-29 20:02:50

标签: applescript

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

此脚本应该(1)将x.txt复制到特定文件夹,将其重命名为new_name,(2)打开它,(3)在所有大写字母中粘贴“new_name”,以及(4)插入“:”然后返回&返回。第一部分是工作,但我很难搞清楚(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

2 个答案:

答案 0 :(得分:1)

通常,因为您正在处理txt文件,所以您无需在应用程序中“打开”该文件并粘贴文本。我们可以直接从applescript读取和写入文本文件。因此,我们从模板文件中读取文本,添加我们想要的任何文本,然后将新文本写入新文件。如果您想打开并查看新文件,则可以在此之后执行此操作。我在代码的“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

答案 1 :(得分:0)

  

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

tell application "TextEdit" to open Path_Of_X

  

在所有大写中粘贴new_name的东西

有一个非常好的第三方脚本添加(Satimage OSAX),您可以将其用于这样的事情(这只有在您下载脚本添加时才有效)...

set UPPER_CASE to uppercase new_name & ":"

  

插入两个换档的东西

return & return