Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8945789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:20:14+00:00 2026-06-15T12:20:14+00:00

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

  • 0

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

  • If the file doesn’t exist, I want to create it.
  • If it does exist, I want to replace the contents.

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.

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:

Can’t make file “Macintosh HD:private:tmp:test.txt” into type reference.

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

How can I create or replace a file using Applescript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:20:15+00:00Added an answer on June 15, 2026 at 12:20 pm

    Try:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
i want to parse a xhtml file and display in UITableView. what is the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.