PDA

View Full Version : Simple Applescript: Create a .txt file




Metalmarco
Mar 2, 2008, 04:14 PM
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.



robbieduncan
Mar 2, 2008, 04:22 PM
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.


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

Metalmarco
Mar 2, 2008, 08:33 PM
Thanks a lot, Robbie, that worked perfectly!

numero
Mar 2, 2008, 09:03 PM
Or you could use the shell.

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

If you want a running record of when the updates were ran do this (double >> )

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


--numero

Metalmarco
Mar 3, 2008, 01:21 AM
Or you could use the shell.

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

If you want a running record of when the updates were ran do this (double >> )

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


--numero

Thanks, it's much more easy with a shell script :D

mysterytramp
Jul 23, 2011, 08:14 AM
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:

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:

set dataOut to dataIn


It was easier to handle it that way than to change variables.

Comments and criticisms, bouquets and brickbats welcome.

mt

Typenow
Jul 24, 2011, 06:21 PM
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:

robbieduncan
Jul 25, 2011, 04:25 AM
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:

What was the path you tried to save the file in?

GregInChrist
May 23, 2012, 06:14 PM
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.


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


So I used this script here. Looked pretty straight forward to me. I made one slight adjustment however, and changed the line save document 1 in "/Users/robbie/Desktop/LastUpdate.txt" to 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!

kryten2
May 23, 2012, 07:07 PM
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.

SuperheroSmith
Aug 23, 2012, 10:32 AM
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:

set theNewMail to {theSender & " at " & theAddress & " regarding " & theSubject}

do shell script theNewMessage > "$HOME/Desktop/Test.txt"

robbieduncan
Aug 23, 2012, 10:41 AM
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:

set theNewMail to {theSender & " at " & theAddress & " regarding " & theSubject}

do shell script theNewMessage > "$HOME/Desktop/Test.txt"

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:

set theNewMail to {theSender & " at " & theAddress & " regarding " & theSubject}

do shell script echo theNewMessage > "$HOME/Desktop/Test.txt"