• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Checking on the existence of a file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Checking on the existence of a file


  • Subject: Re: Checking on the existence of a file
  • From: Christopher Stone <email@hidden>
  • Date: Fri, 26 Oct 2012 20:41:15 -0500

On Oct 26, 2012, at 17:54, Axel Luttgens <email@hidden> wrote:
OK then, even if I know I'll open another bag of bones. ;-)
______________________________________________________________________

Hey Axel,

What's wrong with that?  :)

There's the pure Standard Additions way, for example in a very academic manner:

The most straightforward way to write plain text to be sure.

The write command is pretty flexible in the path formats it accepts:

------------------------------------------------------------------------------------------------

do shell script "open ~/Downloads"

set filePath1 to do shell script "echo ~/Downloads/Posix_Path.txt"
set filePath2 to POSIX file (do shell script "echo ~/Downloads/Posix_File_Path.txt")
set filePath3 to ((path to downloads folder as text) & "HFS_Path.txt")

set fileList to {filePath1, filePath2, filePath3}

repeat with ndx in fileList

  

  try

    

    set fRef to open for access ndx with write permission
    write "This is sample text" to fRef
    close access fRef

    

  on error
    try
      close access fRef
    end try
  end try

  

end repeat

# It will also accept an alias if the file already exists.

------------------------------------------------------------------------------------------------

My general purpose write handlers.

------------------------------------------------------------------------------------------------
--» Write_To_File()
------------------------------------------------------------------------------------------------
--   Purpose: Write to a file as a specific data-type.
--  Modified: 2012-10-26 : 20:00
------------------------------------------------------------------------------------------------
on Write_To_File(srcData, targetFile, startPosition, dataType)
  try
    set fileRef to open for access targetFile with write permission
    if startPosition = 0 then
      set eof of fileRef to 0
    else if startPosition = eof then
      set startPosition to get eof of fileRef
    end if
    write srcData to fileRef as dataType starting at startPosition
    close access fileRef
  on error errMsg number errNum
    try
      close access fileRef
    end try
    display dialog "Error: " & errMsg & return & "Error Number: " & errNum
  end try
end Write_To_File

------------------------------------------------------------------------------------------------
--» write_to_file_overwriting()
------------------------------------------------------------------------------------------------
--   Purpose: Write text to a file and overwrite any content.
--  Modified: 2012-10-26 : 20:00
------------------------------------------------------------------------------------------------
on write_to_file_overwriting(srcText, targetFile)
  try
    set fileRef to open for access targetFile with write permission
    set eof of fileRef to 0
    write srcText to fileRef
    close access fileRef
  on error errMsg number errNum
    try
      close access fileRef
    on error errMsg number errNum
      beep
      display dialog "Error: " & errMsg & return & "Error Number: " & errNum
    end try
  end try
end write_to_file_overwriting
------------------------------------------------------------------------------------------------

set filePath1 to do shell script "echo ~/Downloads/Text_Overwriting.txt"
set filePath2 to POSIX file (do shell script "echo ~/Downloads/Write_as_List.txt")
set filePath3 to ((path to downloads folder as text) & "Write_Text_Appending.txt")

write_to_file_overwriting("Write to file with overwriting", filePath1)

Write_To_File({"one", "two", "three"}, filePath2, 0, list)

repeat 3 times
  Write_To_File("Write to file with appending." & linefeed & linefeed, filePath3, eof, text)
end repeat

# Writing as list allows you to read as list.

------------------------------------------------------------------------------------------------

Writing text via the shell is also very straightforward:

------------------------------------------------------------------------------------------------

set _file to quoted form of (do shell script "echo ~/Downloads/Write_Text_with_the_Shell.txt")
set _text to "Now is the time for all good men to come to the aid of their country."
do shell script "echo " & quoted form of _text & " >> " & _file

# '>' overwrites and '>>' appends.

------------------------------------------------------------------------------------------------

--
Best Regards,
Chris

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Re: Checking on the existence of a file (From: Eric Robertson <email@hidden>)
 >Re: Checking on the existence of a file (From: Axel Luttgens <email@hidden>)

  • Prev by Date: Re: New File Here
  • Next by Date: Time anomaly
  • Previous by thread: Re: Checking on the existence of a file
  • Next by thread: Re: Checking on the existence of a file
  • Index(es):
    • Date
    • Thread