3

既に作成されている TextEdit ファイルに書き込もうとしています。ファイルは rwxrwxrwx モードなので、パーミッションの問題はありません。

しかし、コードを実行すると、次のエラーが表示されます。

error "Network file permission error." number -5000 from file "/Users/me/Desktop/A directory/file.txt" to «class fsrf»

ここに私のコード:

-- Writing in the TextEdit file
set file_URLs_content to "HEEEELLOOOOOO"
tell application "TextEdit"
    set theFile to "/Users/me/Desktop/A directory/file.txt"
    set file_ref to (open for access file theFile with write permission)
    set eof file_ref to 0
    write file_URLs_content to file_ref
    close access file_ref
end tell

私のfile.txtはまだ空ですが、どうすればこのエラーを回避できますか?

4

3 に答える 3

4

TextEdit でテキストを書くときにエラーを回避する方法は、それがテキスト エディターであることを覚えておくことです。エラーを生成せずにテキスト ドキュメントを作成して保存する方法は既にわかっています。アクセスのために (エラーが発生しやすい) open を使用する必要はありません。(エラーが発生しやすい) シェル スクリプトを使用する必要はありません。あなたがしなければならないことは、TextEdit に依頼して、好きな内容でテキスト ドキュメントを作成し、好きな場所に保存するだけです。TextEdit は、ファイル アクセス エラー (アクセスのために開くなど) を生成したり、誤ってフォルダーを上書きしたり (シェル スクリプトなど) することなく、それを行う方法を知っています。

tell application "TextEdit"
    activate
    set theDesktopPath to the path to the desktop folder as text
    set file_URLs_content to "HEEEELLOOOOOO"
    make new document with properties {text:file_URLs_content}
    save document 1 in file (theDesktopPath & "file.txt")
    close document 1
end tell

この方法の利点は、記述が高速で簡単であること、エラーが発生しにくいこと、出力として得られるテキスト ファイルが、TextEdit で手動で作成したテキスト ファイルと同じプロパティを持つこと、およびスクリプトを簡単に作成できることです。他のアプリを含むように拡張されました。たとえば、テキスト コンテンツが別のアプリまたはクリップボードから取得され、テキスト ファイルが別のアプリで開かれたり、保存後にメールで送信されたりする可能性があります。

AppleScript の最も基本的な機能は、この方法で Mac アプリにメッセージを送信することです。PNG を JPEG に変換する場合は、AppleScript で PNG デコーダーと JPEG エンコーダーを記述せず、PNG ファイルを開いてアクセスし、バイト単位で読み取り、JPEG をバイト単位でエンコードします。PNG 画像を開き、JPEG として特定のファイルの場所にエクスポートするように Photoshop に指示するだけです。「open for access」コマンドは、読み書きするアプリがないファイルを読み書きするための最後の手段です。「do shell script」コマンドは、コマンド ライン アプリを組み込むためのものです。たとえば、Perl で正規表現を実行するなど、その作業を行うための Mac アプリがない場合に使用します。テキストファイルを扱うだけなら、TextEdit だけでなく、

于 2014-08-19T11:07:06.423 に答える
3

そのためにテキストエディットは必要ありません。これを試して:

set the logFile to ((path to desktop) as text) & "log.txt"
set the logText to "This is a text that should be written into the file"
try
    open for access file the logFile with write permission
    write (logText & return) to file the logFile starting at eof
    close access file the logFile
on error
    try
        close access file the logFile
    end try
end try
于 2013-03-04T10:55:51.233 に答える
1

試す:

set file_URLs_content to "HEEEELLOOOOOO"
set filePath to POSIX path of (path to desktop as text) & "file.txt"
do shell script "echo " & quoted form of file_URLs_content & " > " & quoted form of filePath
于 2013-03-04T11:52:42.550 に答える