0

my following code returns an error

tell current application
    set b to "Tuesday 12 April 2016 at 18:06:10"
    date b
end tell

If I would write the string, defined in b, directly before date, it is working, but my date string is defined by some variables, so b is just an example for the string, how it looks like.

But why does AppleScript returns the error, only because of the string is defined in a variable?

  • 1
    Please add the actual content of your script! – klanomath Apr 12 '16 at 16:51
  • What exactly are you trying to do, set the date on the system? If so, you need to use the do shell script date ... where ... is properly formatted information the date command expects. – user3439894 Apr 12 '16 at 19:48
0

Try:

set dateString to "Tuesday, April 12, 2016 at 12:00:00 AM"
set dateReference to date dateString

tell current application
    -- insert commands here
end tell

Another approach:

set y to 2001
set m to 6
set d to 12
set h to 12
set min to 12
set s to 12

set cd to current date
tell cd
    set its year to y
    set its month to m
    set its day to d
    set its hours to h
    set its minutes to min
    set its seconds to s
end tell
  • Doesn't work: error "Can’t get date \"Tuesday, April 12, 2016 at 12:00:00 AM\"." number -1728 – hi-everyone Apr 12 '16 at 17:05
  • Is this still inside your tell block or have you removed it? – adayzdone Apr 12 '16 at 17:15
  • Yes, it is still inside the tell block. Something other is also done there, but outside the tell block it is working? Why? – hi-everyone Apr 12 '16 at 17:17
  • Date is from standard additions, not from the application – adayzdone Apr 12 '16 at 17:21
  • I know it is no application, but how to tell it it to run the commands? – hi-everyone Apr 12 '16 at 17:24
0

Your syntax isn't quite right.

First, you don't need to "tell" anything. Leave the "tell" and "end tell" off. Dates are not specific to any particular app so you don't need to "tell." This may be the entire problem.

Second, if you do something like this:

set a to current date

You will get a result like this (not exactly like this, because it is particular to your date and time preferences):

date "Tuesday, April 12, 2016 at 10:39:44 AM"

That's the "result" of the one-line script above. Notice the commas.

Your script where you set "b" does not include commas and that may be where it goes wrong. I think if you run a one-line script like I showed above you will see the proper syntax. Once you see it, you can change your string in the line where you set b to match the syntax the system expects. If you are using the proper syntax (that is, matching what you get when you ask for current date) then your problem is exclusively that you've wrapped your date commands in the "tell" block, and the app you're "telling" doesn't understand how to do it. Leave the "tell" wrapper off. Get your date stuff done outside of that. Your date "b" will still work later on in your script, in a tell block. But you have to do your date work outside of a tell block.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .