Simple Applescript: Create a .txt file

Discussion in 'Mac Programming' started by Metalmarco, Mar 2, 2008.

  1. macrumors newbie

    Joined:
    Mar 2, 2008
    #1
    Hi everyone, I hope i'm writing in the right section of the forum.
    I need a very simple script, that creates a txt file on the Desktop called "LastUpdate.txt", containing the current date/time.

    I know it's a very simple thing to program, but i'm completly noob in applescript so I really need your help.

    Thanks in advance.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Creating files and writing to them from AppleScript is a little complicated. It's easier to use AppleScript for what it's really intended for: automating existing applications. The script below will leverage TextEdit to do what you want. Just replace the "robbie" (my username) with your username in the script.

    Code:
    tell application "TextEdit"
    	activate
    	make new document
    	set theDate to current date
    	set text of document 1 to theDate as text
    	save document 1 in "/Users/robbie/Desktop/LastUpdate.txt"
    end tell
    
     
  3. thread starter macrumors newbie

    Joined:
    Mar 2, 2008
    #3
    Thanks a lot, Robbie, that worked perfectly!
     
  4. macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #4
    Or you could use the shell.

    Code:
    do shell script "date > $HOME/Desktop/LastUpdate.txt"
    If you want a running record of when the updates were ran do this (double >> )

    Code:
    do shell script "date >> $HOME/Desktop/LastUpdate.txt"

    --numero
     
  5. thread starter macrumors newbie

    Joined:
    Mar 2, 2008
    #5
    Thanks, it's much more easy with a shell script :D
     
  6. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #6
    Sorry for bumping this old thread, but it popped up on a Google search when I was double-checking some syntax.

    robbieduncan's right. It's easier to use Applescript for what it was intended: automating apps. Sometimes, however, it makes sense to do file management all in Applescript. (inside a loop, perhaps?) Here's one way:

    Code:
    set theFile to choose file
    
    set theNewFile to choose file name
    
    try
    	set fileRef to (open for access theFile with write permission)
    on error errMsg number errNum
    	display dialog ("Open for Access, Error Number: " & errNum as string) & return & errMsg
    end try
    
    set filesEOF to get eof fileRef
    
    try
    	set dataIn to read fileRef for 1000
    on error errMsg number errNum
    	display dialog ("Read, Error Number: " & errNum as string) & return & errMsg
    end try
    
    try
    	close access fileRef
    on error errMsg number errNum
    	display dialog ("Close, Error Number: " & errNum as string) & return & errMsg
    end try
    
    set dataOut to dataIn
    
    try
    	set fileRef to (open for access theNewFile with write permission)
    on error errMsg number errNum
    	display dialog ("Open for Access, Error Number: " & errNum as string) & return & errMsg
    end try
    
    set eof of fileRef to 0
    
    try
    	write dataOut to fileRef
    on error errMsg number errNum
    	display dialog ("Write, Error Number: " & errNum as string) & return & errMsg
    end try
    
    set eof of fileRef to (length of dataOut)
    
    try
    	close access fileRef
    on error errMsg number errNum
    	display dialog ("Close, Error Number: " & errNum as string) & return & errMsg
    end try
    
    display dialog "Done"
    A couple of notes:

    Yes, I used the try-on error construct too often. I did some file management in Pascal many years ago, and this was the "proper" way according to the old Inside Macintosh manuals.

    choose file returns an alias. choose file name returns a furl. I wouldn't be surprised if the file reading/writing commands get more finicky in future AS versions.

    An easy rewrite of this script would be to eliminate this line:

    Code:
    set dataOut to dataIn
    
    It was easier to handle it that way than to change variables.

    Comments and criticisms, bouquets and brickbats welcome.

    mt
     
  7. macrumors newbie

    Joined:
    Jun 30, 2010
    #7
    Doesn't work in 10.7?

    I was in need of a way to create a blank text file using applescript so that i could then add data to it when the Applescript detects a new song... Anyways, I tried it and when Textedit went to save it said I didn't have permission to export it as name.txt. It then tells me I should go a change the permissions by clicking on the file and going to more info... that would be fine, but there is no file to select. I tried adding more permissions by going to more info on textedit.app but that didn't work. Anyways, any help would be great!
    Thanks.:apple:
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    What was the path you tried to save the file in?
     
  9. macrumors newbie

    Joined:
    May 23, 2012
    #9
    So I used this script here. Looked pretty straight forward to me. I made one slight adjustment however, and changed the line
    to
    Code:
    save document 1 in path to desktop
    Now I thought "in" path to desktop did not sound quite right, but I gave it a shot anyway.

    That did not work so well, as I no longer have any of my desktop files after it gave some error to the effect of "your desktop file is locked do you want to overwrite anyway?" I said no don't overwrite. Then all my desktop file disappeared. Any chance someone has the slightest clue as to how I might get them back?

    Thanks in advance! God Bless you all!
     
  10. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #10
    I made the same mistake some time ago and also said no don't overwrite. If you have a Time Machine backup now would be a good time to use it.
     
  11. macrumors newbie

    Joined:
    Aug 23, 2012
    #11
    Save variable to txt file

    Thank you for the above code. I've been able to use to save the date, which has been handy for some actions, but I'm having difficulty saving a variable to a txt file using a shell script. Below is what I have tried:

    Code:
    set theNewMail to {theSender & " at " & theAddress & " regarding " & theSubject}
    				
    do shell script theNewMessage > "$HOME/Desktop/Test.txt"
     
  12. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    date is a unix command. The value you are assigning to theNewMessage (which I assume should really be theNewMail) is not a valid unix command. Try using the echo command:

    Code:
    set theNewMail to {theSender & " at " & theAddress & " regarding " & theSubject}
    				
    do shell script echo theNewMessage > "$HOME/Desktop/Test.txt"
     

Share This Page