Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Pigopl

macrumors newbie
Original poster
Aug 17, 2009
2
0
I hope someone can help me. I am a newbie to Applescript and am trying to translate some Excel VBA macros.

I have hit a problem in creating a date object from a string variable, which I build at run time.

set theDate to date theStringWithAppropriateDateFormat
works if I type it into Script Editor, but as soon as I enclose it in a tell block, I get an error and theDate is null.

i.e.
set theDate to date ("1" & "/" & "6" & "/" & "2009") (*the appropriate format for my regional settings *)
set aDateObject to date theDate

gives me "Can’t get date (date "Monday, 1 June 2009 00:00:00")."

Any ideas - this is driving me mad :confused:

Thanks,

Tim
 
Manipulating a string to create a date has built-in hazards ... Is there any chance the script will be run by someone else with different date settings? 1 June becomes Jan 6!

You might find this useful:

Code:
set dateVar to the current date
set the month of dateVar to 6
set the day of dateVar to 17
dateVar

returns: date "Wednesday, June 17, 2009 7:27:35 AM"

Essentially, copy "the current date" into a variable creates a date object. Then use "year", "month" and "day" to change the date.

The thing to watch out for is that if you do things in the wrong order, the date will inadvertently change out from under you. Let's say the current date is in February and you want to change it to July 31?

If you change the day first, AppleScript will think you're trying to say February 31 and automatically kick you into March 3 (or 2 in leap years). then when you change the month to July, you'll end up with July 3. NG.

So change the year first, then the month, then the day and you won't have any issues.

And the short answer to your question:

Code:
set theDate to date ("1" & "/" & "6" & "/" & "2009")
set aDateObject to date theDate

you don't need "date" in the second line. This works:

Code:
set theDate to date ("1" & "/" & "6" & "/" & "2009") (*the appropriate format for my regional settings *)
set aDateObject to theDate

mt
 
Tell block messes up the simple code

mysterytramp,

Thanks very much for your swift reply.

I have used that workaround, but it seems very cack-handed to have to do it this way.

Sorry for the error in the example code.

Code:
tell application "Microsoft Excel"
	set strDate to ("1" & "/" & "6" & "/" & "2009")
	set theDate to date strDate -- the appropriate format for my regional settings
	set aDateObject to theDate
	display dialog (aDateObject as string)
end tell

The code works above works fine in Script Editor without the tell, but as soon as you tell an application it breaks. (this is not particular to MS Excel for Mac; TextEdit equally has the same problem)

Any idea what is going on here - is there some fundamental Applescript thing that I just don't undertstand?

Thanks for your brain-cycles,

Tim
 
I don't really know Applescript, so this may be completely worthless, but is there a way to set the variable globally, outside the tell block and then to still access it within the scope of a tell block later in the script?
 
I bet you're using Snow Leopard?

Technically, you should limit your tell blocks to info that the application needs/provides. Date info is provided by AppleScript, not Excel, so it should be outside the tell block.

SL is more strict about this, and it could be Excel is even more strict than SL. I was worried about some Finder scripts, but I've not seen many script breakages on this issue.

If you rewrite your script, as GorillaPaws suggested, with the date handling lines outside the Excel tell block, you should be in pretty good shape. You won't need to assign it to a global variable though.

Code:
set strDate to ("1" & "/" & "6" & "/" & "2009")
set theDate to date strDate -- the appropriate format for my regional settings
set aDateObject to theDate
display dialog (aDateObject as string)

tell application "Microsoft Excel"
	-- put Excel stuff here
end tell

(BTW, whatever you use aDateObject for, you can probably use theDate and save yourself a var)

I have used that workaround, but it seems very cack-handed to have to do it this way.

As "cack-handed" as it might appear, you're actually doing it the "right" way. You're assigning the "year" field, the "month" field and the "day" field of a date record, not assuming that whoever comes along behind you has the same date preference.

mt
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.