I’ve been able to parse a date from a text file as a string. The date looks like this “07/30/2004 06:25:49”. As far as I can tell this is the proper format for applescript but the script editor always complains Can’t make "0
Hi,
How are you using the date string? This works:
set t to “07/30/2004 06:25:49”
set d to date t
gl,
That’s exactly how I’ve tried to do it. Perhaps there’s some weird encoding thing going on?
Hi Greg,
Sounds like a bug. Maybe you could try removing the leading zero.
gl,
I’ve tried that and other variations. It’s pretty weird. At least I know I what I thought I was doing was right.
Personally, I’d rather have consistancy than updates with new bugs.
I don’t know why Apple is doing this. I guess they think they can make more money with quick updates than fixing what was broke with updates. To me, you lose your base. Your base might not switch, but they might just get fed up with that kind of stuff and maybe change to fishing or something.
gl,
Hi.
Every way I’ve tried corrupting a date string to make a date specifier fail produces the error “Invalid date and time .” (where “.” represents the string used). However, trying to use a date specifier with a valid date string inside an application ‘tell’ block produces various errors, depending on the application:
Finder: “Finder got an error: Can’t get date …”
System Events: “System Events got an error: Can’t make date . into type reference.”
Script Editor: “Script Editor got an error: Can’t make date . into type reference.”
DragThing: DragThing got an error: kMOSLClassHasNoElementsOfThisTypeErr"
If you’re using ‘date t’ in a ‘tell’ block, and there’s no way to avoid that, try bopping ‘my’ or ‘tell me to’ in front of it. That prevents any attempt to interpret ‘date’ as one of the application’s own elements:
tell application "Finder"
set t to "07/30/2004 06:25:49"
set d to my date t
-- Or:
tell me to set d to date t
end tell
That didn’t work either, NG. Here’s the workaround script I came up with:
[code]set t to “07/30/2004 06:25:49”
set OldDelims to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to " "
set t1 to text items of t
set AppleScript’s text item delimiters to OldDelims
set datestr to item 1 of t1
set timestr to item 2 of t1
set OldDelims to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to “/”
set t1 to text items of datestr
set AppleScript’s text item delimiters to OldDelims
set daystr to (item 2 of t1) as integer
set monstr to (item 1 of t1) as integer
set yearstr to (item 3 of t1) as integer
set OldDelims to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to “:”
set t1 to text items of timestr
set AppleScript’s text item delimiters to OldDelims
set hourstr to (item 1 of t1) as integer
set minstr to (item 2 of t1) as integer
set secstr to (item 3 of t1) as integer
set mydate to current date
set day of mydate to daystr
set month of mydate to monstr
set year of mydate to yearstr
set hours of mydate to hourstr
set minutes of mydate to minstr
set seconds of mydate to secstr
log mydate[/code]
Hello
Have you checked which date format is selected in the International System Preferences ?
I would not be surprised that it is NOT the one used in your string which is “mm/dd/yyyy”.
Maybe it is set to “dd/mm/yyyy”.
On the other hand, it seems that you may shorten your workaround.
set t to "07/30/2004 06:25:49"
set OldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set t1 to text items of t
set {datestr, timestr} to items 1 thru 2 of t1
set AppleScript's text item delimiters to "/"
set t1 to text items of datestr
set AppleScript's text item delimiters to OldDelims
set date0 to "01/01/1904"
set mydate to date (date0)
set day of mydate to (item 2 of t1) as integer
set month of mydate to (item 1 of t1) as integer
set year of mydate to (item 3 of t1) as integer
set mydate to date timestr of mydate
log mydate
Yvan KOENIG (from FRANCE dimanche 29 octobre 2006 17:29:49)
There’s some sort of weird encoding or something going on here. When I past the data in a blank MacRoman text document in TextWrangler and view invisibles, i get these upside down question marks. These are even preventing me from properly pasting stuff into these replies (the browser ignores everything after upside down question mark).
Hi, Greg. It sounds as though your text file may actually contain Unicode text. How are you reading it? If you’re using the File Read/Write read command, what happens if you read the file as Unicode text?
set mytext to (read file "bah:blah:blah" as Unicode text)
as Unicode text isn’t a coercion here, but a parameter of read. It tells the command to interpret the file as though it contained Unicode text. If the file does contain Unicode, that might make the date conversion work. read by itself assumes that the file contains plain text, and interprets each byte as an individual character. If the file contains Unicode, you’ll get alternating values of ASCII character 0 and the characters you can see, unless you use the as Unicode text parameter.
I’m fuzzy on the whole unicode text thing. I started messing with it and reading the file as 8 or 16 UTF makes the rest of my script not work. I have parts where the script parses text and even though I can see the target text is there, the script doesn’t. I’ve converted the file to ASCII using TextWrangler but there still seems to be some “invisible” spaces.
At least I can get the proper date format now. If only I could get the files im creating to have their created and modified dates set to this date, I would be set.
Man, AS can sure be a pain sometimes.
tell application "Finder" to set modification date of (choose file) to date "Friday, September 1, 2006 12:00:00 AM"
The modification code doesn’t like what I’ve got. I’ve got AS to reconguized the date but the script editor compalins.
get date "Friday, July 30, 2004 6:25:49 AM"
"Finder got an error: Can't get date \"Friday, July 30, 2004 6:25:49 AM\"."
See post #7 in this thread.
I’ve tried that in several variations, all I get is variations on the error messages. It seems to me that I have coerced the string into the date.
tell current application
get date (date "Friday, July 30, 2004 6:25:49 AM")
"Can't make date (date \"Friday, July 30, 2004 6:25:49 AM\") into type reference."
I’ve even tried to push it through this sub (that usally works through date issues) and the darned thing doesn’t want to budge.
on SubDate(TheString)
return (date TheString) as date
end SubDate
of course you get error messages.
get date (date theString) and (date theString) as date are wrong expressions.
You can only coerce a string to a date with the get date theString form
and the string must be in the right order of the system international date format settings
Yeah, I’m understanding that as I read through the online applescript docs. But even get date doesn’t want to coerce the value into a date.
have you tried it outside a tell block or with tell me as Nigel suggested
I’ve tried Nigel’s suggestions for inside a tell and tried outside a tell etc and so forth. I think I will give up on the finder and see what I can do with setfile via as shell script instead.