如何使用 Applescript 创建或替换文件? [英] How can I create or replace a file using Applescript?

查看:33
本文介绍了如何使用 Applescript 创建或替换文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AppleScript 将内容写入文件 /tmp/test.txt.

I'm trying to write content to a file, /tmp/test.txt with AppleScript.

  • 如果文件不存在,我想创建它.
  • 如果确实存在,我想替换内容.

事实证明这非常困难,因为 AppleScript 使用不同的语法来创建新文件和写入现有文件.我还想过删除一个文件,如果它已经存在,然后创建它,但 AppleScript 默认会将内容移到垃圾箱,因此即使这样也相当复杂.

This is proving quite difficult because AppleScript has a different syntax for creating a new file versus writing to an existing file. I also thought about deleting a file if it already exists, and then creating it, but AppleScript will move things to the trash by default and so even that is rather complicated.

我要写入文件的数据里面可能有很多特殊字符,所以我真的不想做类似do shell script "cat " + data + " >/tmp/txt.txt" 因为我不知道 AppleScript 有任何 escapeshellarg.

The data I am going to write to the file may have a lot of special characters in it, so I don't really want to do something like do shell script "cat " + data + " > /tmp/txt.txt" because I am unaware of any escapeshellarg for AppleScript.

以下是我目前所拥有的,但出现错误:

Below is what I have so far but I'm getting an error:

无法将文件Macintosh HD:private:tmp:test.txt"设为类型引用.

Can't make file "Macintosh HD:private:tmp:test.txt" into type reference.

我真的很想通过改进辅助函数 write_to_file 来抽象它,我得到了 here 以便在文件不存在时创建文件.

I would really like to abstract this by improving the helper function write_to_file that I got here so that it would take care of creating the file if it does not exist.

my write_to_file("hello", "/tmp/test.txt", false)

on write_to_file(this_data, target_file, append_data)
    try

        if not (exists POSIX file target_file) then
            make new document file at ("/tmp/" as POSIX file as alias) with properties {name:"test.txt", text:the_data}
        end if

        set the target_file to the target_file as POSIX file
        set the open_target_file to open for access file target_file with write permission
        if append_data is false then set eof of the open_target_file to 0
        write this_data to the open_target_file starting at eof
        close access the open_target_file
        return true
    on error e
        try
            display dialog e
            close access file target_file
        end try
        return false
    end try
end write_to_file

如何使用 Applescript 创建或替换文件?

推荐答案

尝试:

my write_to_file("hello", "/tmp/test.txt", true)

on write_to_file(this_data, target_file, append_data)
    try
        tell application "System Events" to exists file target_file
        if not the result then do shell script "> " & quoted form of target_file
        set the open_target_file to open for access target_file with write permission
        if append_data is false then set eof of the open_target_file to 0
        write this_data to the open_target_file starting at eof
        close access the open_target_file
        return true
    on error e
        try
            display dialog e
            close access target_file
        end try
        return false
    end try
end write_to_file

这篇关于如何使用 Applescript 创建或替换文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆