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

发布于 2024-12-07 00:40:08 字数 866 浏览 1 评论 0原文

编辑: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

EDIT: regulus6633 has made a script that's a lot better than my outline below, it works perfectly IF you're template file isn't completely empty (I think this caused an error originally). Thanks!

This script is supposed to (1) copy a x.txt to a specific folder rename it to new_name, (2) open it, (3) paste "new_name" in all caps, and (4) insert ":" followed by return & return. The first part is working, but I'm having trouble figuring out (2), (3) and (4). The code I've written so far is pasted below.

 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

子栖 2024-12-14 00:40:08

一般来说,由于您正在处理 txt 文件,因此不需要在应用程序中“打开”该文件并粘贴文本。我们可以直接从 applescript 读取和写入文本文件。因此,我们从模板文件中读取文本,添加我们想要的任何文本,然后将新文本写入新文件。如果您想打开并查看新文件,则可以在之后执行此操作。我在代码的“TextEdit”部分中做到了这一点。

您可以在脚本末尾看到我有子例程来写入文本文件并将文件名更改为大写字母。那么请尝试以下操作...

-- 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

In general since you're dealing with a txt file, you do not need to "open" the file in an application and paste in text. We can read and write to text files directly from applescript. As such we read in the text from the template file, add whatever text we want to that, and then write the new text to a new file. If you then want to open and view the new file you can do that after. I did that in the "TextEdit" section of the code.

You can see at the end of the script I have subroutines to write a text file and also to change the file name to CAPS. So try the following...

-- 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
黑色毁心梦 2024-12-14 00:40:08

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

tell application "TextEdit" to open Path_Of_X

new_name 全部大写粘贴的内容

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

set UPPER_CASE to uppercase new_name & ":"

插入两个换行的东西

return & return

something that lets me open the file is needed here

tell application "TextEdit" to open Path_Of_X

something that pastes new_name in ALL CAPS

There is a really good third party scripting addition (Satimage OSAX) that you can use for things just like this (this will only work if you've downloaded the scripting addition)...

set UPPER_CASE to uppercase new_name & ":"

something that inserts two lineshifts

return & return
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文