Saving a file with date as name

I am trying to learn to save my file using the date as the file name.

This is my script:

set DataHoje to (current date) as string
tell application “Tex-Edit Plus”
activate
make new window
set insertion point before contents of window 1 to “123456”
save window 1 in file ((path to desktop as text) & ((DataHoje) as text) & “.txt”) as string with creator �class TBB6�
end tell

Can anyone help me?

Thanks!

Hello fundidor,

In reference to your question, try thinking about it from a different perspective.

Instead of scripting a save file with the current date, search for a script which someone else has already written as a folder attachment that renames any files saved to a particular folder with the current date, etc… There’s sure to be at least one if not many such scripts on Macscripter Code Exchange (http://macscripter.net/exchange/), and I believe you may also find at least one of such on your computer in the scripts folder. I hope this helps.

                                        Sincerely,


                                      Variable as the shade

Hi,

set DataHoje to (current date) as string

This is something like what is returned from your first line:

“Wednesday, January 28, 2004 7:34:44 PM”

So, in the first place, you cannot use colons in folder/file names. You can get the date without time with:

set DataHoje to date string of (current date)

gl,

Here’s a handler (with a required sub-handler) to give you a simple date string formatted as “yyyymmdd”.

on getDateStamp()
	set today to current date
	set m to my getMonthNum(today)
	if m < 10 then set m to "0" & m as text
	set d to day of today
	if d < 10 then set d to "0" & d as text
	set wholeYear to year of today as text
	set dateString to wholeYear & m & d as text
	set dateStamp to dateString as text
end getDateStamp

on getMonthNum(inDate)
	set monthNameList to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
	set monthName to month of inDate as text
	repeat with x from 1 to count of monthNameList
		set compareMonth to item x of monthNameList
		if monthName = compareMonth then
			set monthNum to x
			exit repeat
		end if
	end repeat
	return monthNum
end getMonthNum

Try this instead:

set the iDay to day of the (current date) as integer
set the iMonth to month of the (current date) as integer
set the iYear to year of the (current date) as integer

no need for handlers and should work internationally… Simple!

Ray

Hi folks!

Thanks very much for your kind replies!

I am surprised how fast I got your help. I was really all by myself trying to figure out how it is done, only reading my manuals…

Ray,

You tip works fine for me except that iMonth will only work if I set it as text. It won’t accept as interger.

I need to convert “January” to “01”.

Would you explain me how it can be done the simplest way?

Thanks again!

The second handler in my post can get you the month number. You just send one parameter, the date you’re using to get your date string. The date string handler I posted also formats single digits into two digits, i.e 4 becomes 04.

Dear Joseph,

By reading your handler I imagine it can do what I need. But the question is that I am an absolute starter!

I copy your handler, paste on script editor, run the script, and get no results at the result’s window.

I try adding a line at the of your script: display dialog monthNum

and get “monthNum is not defined”

It seems that I don’t know how to place your handler inside my script. Would you be so kind to further explain me?

Thanks!

Sorry. Handlers are subroutines that are called from other parts of the script. A handler might or might not take parameters, which are contained in the parentheses. The getDateStamp() handler doesn’t require any parameters (which is why the parentheses are empty). So to call it from the script you would use:

set dateStamp to getDateStamp()

And the variable ‘dateStamp’ will now contain “20040129” for example.

In your code this can all be done in the save statement:

save window 1 in file ((path to desktop as text) & my getDateStamp() & ".txt") as string with creator «class TBB6»

The term ‘my’ is required for any handlers that you want to call from within a ‘tell application’ block since without it AppleScript thinks the handler belongs to the targeted app.

I was looking my code over and I decided to update it. I wrote those handlers so many years ago and I haven’t really gone back over them to apply what I know now. So here’s the updated code, nothing new, just less of it.

on getDateStamp()
	set today to current date
	set m to my getMonthNum(today)
	if m < 10 then set m to "0" & m as text
	set d to day of today
	if d < 10 then set d to "0" & d as text
	return (year of today as text) & m & d as text
end getDateStamp

on getMonthNum(inDate)
	set monthNameList to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
	repeat with i from 1 to count of monthNameList
		if month of inDate as text = item i of monthNameList then return i
	end repeat
end getMonthNum

Hello Joseph!

Thanks for your help. Now my script works as I expect.

Without your help I wouldn’t figure out the “()” missing…

This simple script shows me how it is done:

set dateStamp to getDateStamp()
on getDateStamp()
set today to current date
set m to my getMonthNum(today)
if m < 10 then set m to “0” & m as text
return m as text
end getDateStamp
on getMonthNum(inDate)
set monthNameList to {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”}
repeat with i from 1 to count of monthNameList
if month of inDate as text = item i of monthNameList then return i
end repeat
end getMonthNum
display dialog dateStamp

My whole script that I call from within file maker is so:

set the iDay to day of the (current date) as integer
on getDateStamp()
set today to current date
set m to my getMonthNum(today)
if m < 10 then set m to “0” & m as text
return m as text
end getDateStamp

on getMonthNum(inDate)
set monthNameList to {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”}
repeat with i from 1 to count of monthNameList
if month of inDate as text = item i of monthNameList then return i
end repeat
end getMonthNum
tell application “Tex-Edit Plus”
activate
select insertion point before contents of window 1
paste
replace window 1 looking for “^t” replacing with “”
replace window 1 looking for “,” replacing with “”
replace window 1 looking for “.” replacing with “”
replace window 1 looking for “-” replacing with “”
replace window 1 looking for “/” replacing with “”
replace window 1 looking for “Á” replacing with “A”
replace window 1 looking for “É” replacing with “E”
replace window 1 looking for “Í” replacing with “I”
replace window 1 looking for “Ó” replacing with “O”
replace window 1 looking for “Ç” replacing with “C”
replace window 1 looking for “Õ” replacing with “O”
replace window 1 looking for “Ô replacing with “A”
replace window 1 looking for “Ê” replacing with “E”
replace window 1 looking for “Ô” replacing with “O”
replace window 1 looking for “” replacing with “A”
save window 1 in file ((path to desktop as string) & “SCE” & ((iDay) as text) & my getDateStamp() & “A.seq”) as string with creator «class TBB6»
quit
end tell
say “Arkeevoh salvoh noh desktop!” using “Trinoids” –
display dialog “Seu Arquivo de remessa foi salvo na area de trabalho!” buttons {“OK”} default button 1 giving up after 10


Would you indicate me an Applescript book for a starter as me?

Hi fundidor,

In response to your comment:

ither one of the following returns the month’s number for me:

set the iMonth to month of the (current date) as integer

or

set the iMonth to month of the (current date) as number

whilst

set the iMonth to month of the (current date) 

just returns the month’s name.

I am on Mac OS 10.3… maybe there is a difference in Applescript versions we are each using?.. the about panel says it is AppleScript version 1.9.2

Cheers, Ray

I work on an OS9 box at work and an OSX box at home so I tend to keep my scripts workable in both environments but they are completely geared for OS9.

And like I mentioned in the later post, I haven’t looked at my date scripts in years and years (when things work, I don’t fiddle with them much). I finally convinced my work to migrate to OSX :D, so I’ll be abandoning OS9 scripting for good after that.

I haven’t explored the new features of AS in X very much so I didn’t even know the date object is now smart enough to return a month number. Thanks.