0

/tmp/test.txtAppleScriptを使用して、コンテンツをファイルに書き込もうとしています。

  • ファイルが存在しない場合は、作成します。
  • 存在する場合は、内容を置き換えたいと考えています。

AppleScript では、新しいファイルを作成するための構文と、既存のファイルに書き込むための構文が異なるため、これは非常に困難です。ファイルが既に存在する場合は削除してから作成することも考えましたが、AppleScript はデフォルトでゴミ箱に移動するため、それもかなり複雑です。

ファイルに書き込もうとしているデータには、多くの特殊文字が含まれている可能性があるため、AppleScriptdo shell script "cat " + data + " > /tmp/txt.txt"について何も知らないので、実際にはそうしたくありません。escapeshellarg

以下は私がこれまでに持っているものですが、エラーが発生しています:

ファイル「Macintosh HD:private:tmp:test.txt」を型参照にできません。

ここでwrite_to_file取得したヘルパー関数を改善して、ファイルが存在しない場合にファイルを作成するようにすることで、これを抽象化したいと思います。

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 を使用してファイルを作成または置換するにはどうすればよいですか?

4

2 に答える 2

2

あなたが犯した最初の間違いは、POSIX パスと HFS パスを混在させたことです。posix パスは /tmp/test.txt であり、同じファイルへの hfs パスは、もちろんブート ボリュームの名前が Macintosh HD である場合、Macintosh HD:tmp:test.txt になります。

open for access file "Volume:Path:to:file.txt"

2 つ目の間違いは、Mac OS X の場合、/tmp フォルダーは一時ファイルを保存する場所ではないことです。そこに書き込むには管理者権限が必要であり、ユーザーはユーザー名とパスワードの入力を求められます。これは /var/folders に保存され、ユーザーごとにログイン時に作成されるフォルダーがあり、ユーザーの切り替えも高速です。このフォルダーへのパスにアクセスするには、一時アイテムへのコマンド パスを使用する必要があります。一時的なアイテムは、スタートアップ間で削除されます。

set theFile to (path to temporary items as string) & "test.txt"

3 番目の間違いは、引用符で囲まれた形式を使用している場合、do シェル スクリプトを使用できることです。AppleScript はシェルと同様に Unicode であるため、do shell script で文字を処理できないということはありません。

do shell script "/bin/echo -n " & (quoted form of "$@%^&*()@") & " > " POSIX path of theFile

最後に、ファイルの存在を確認する必要があるという間違いを犯しました。open for access コマンドを使用している場合、ファイルが存在しない場合にファイルが作成されます。したがって、必要なコードは次のとおりです。

set theFile to (path to temporary items as text) & "test.txt"
writeToFile(theFile, "Hello!", false)
writeToFile(theFile, (linefeed & "Goodbye!" as string), true)

on writeToFile(p, d, a)
    try
        set fd to open for access file p with write permission
        if not a then set eof of fd to 0
        write d to fd starting at (get eof of fd) as «class utf8»
        close access fd
    on error
        close access file p
    end try
end writeToFile

または、UTF-8ファイルも作成するdoシェルスクリプトを使用しますが、指定されたパスはPOSIXパスである必要があります

set theFile to (path to temporary items as text) & "test.txt"
writeToFile(POSIX path of theFile, "goodbye!", false)
writeToFile(POSIX path of theFile, (linefeed & "hello!" as string), true)

on writeToFile(p, d, a)
    set cmd to "/bin/echo -n " & quoted form of d & " >"
    if a then set cmd to cmd & ">"
    do shell script cmd & space & quoted form of p
end writeToFile

お好みに合わせてお選びください

于 2012-12-03T19:56:53.167 に答える
1

試す:

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
于 2012-12-03T05:20:23.220 に答える