Become a MacRumors Supporter for $25/year with no ads, private forums, and more!

Metalmarco

macrumors newbie
Original poster
Mar 2, 2008
3
0
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

Moderator emeritus
Jul 24, 2002
25,611
889
Harrogate
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
 

numero

macrumors regular
Jul 23, 2002
106
3
OR
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
 

Metalmarco

macrumors newbie
Original poster
Mar 2, 2008
3
0
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

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

mysterytramp

macrumors 65816
Jul 17, 2008
1,334
4
Maryland
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
 

Typenow

macrumors newbie
Jun 30, 2010
3
0
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:
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
889
Harrogate
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

macrumors newbie
May 23, 2012
5
0
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

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

kryten2

macrumors 65816
Mar 17, 2012
1,009
66
Belgium
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

macrumors newbie
Aug 23, 2012
1
0
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"
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
889
Harrogate
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"

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"
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.