Topics

How to script the launching of a script at a specific time...

Jean-Christophe Helary <jean.christophe.helary@...>
 

If there is an Applescript way to open a specific file at a given time that does not involve Calendar, I'm all for it, but right now I'm trying to focus on Calendar because that's where I manage all my jobs. So...

I know I can use Calendar's "open file alert" to open a file at a specific time. It works fine when I manually create an event.

What I want to do is to script the creation of that event, and for that I need to specify the filepath property of the "open file alert" but *that* doesn't work. I end up with an event that does *not* have a filepath and that won't open the file.

Conversely, when I manually create the alert, it does work *but* the event as explored in SD does not provide any value for the filepath property, so I have no way to see what I'm doing wrong.

Specifically:

tell application "Calendar"
activate
set job to make new event in calendar "test" with properties {allday event:true, status:confirmed, start date:(date "Saturday, December 2, 2017 0:00:00"), end date:(date "Sunday, December 3, 2017 0:00:00"), description:"Test", summary:"launch file", location:"Jean-Christophe", url:""}
set filealert to make new open file alarm of job with properties {filepath:"/path/to/file", trigger date:(date "Friday, December 1, 2017 16:26:00")}
switch view to week view
show job
end tell

Plus, that code *sometimes* triggers a Calendar error that goes like this:


I take any suggestions that solves that Calendar issue (most probably caused to my poor coding skills), or any solution that bypasses Calendar but stays reasonably within what Applescript allows :-)


Jean-Christophe Helary
-----------------------------------------------
@brandelune http://mac4translators.blogspot.com


Shane Stanley
 

On 2 Dec 2017, at 1:09 pm, Jean-Christophe Helary <jean.christophe.helary@...> wrote:

I know I can use Calendar's "open file alert" to open a file at a specific time. It works fine when I manually create an event.

What I want to do is to script the creation of that event, and for that I need to specify the filepath property of the "open file alert" but *that* doesn't work.
As noted elsewhere, you can't do that since macOS 10.9. The filepath property is a vestige of bygone carefree days.

--
Shane Stanley <sstanley@...>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>

Jean-Christophe Helary <jean.christophe.helary@...>
 

Ok, so the only way to actually launch a script at a given date/time is to use launchd I guess ?

On Dec 2, 2017, at 12:13, Shane Stanley <sstanley@...> wrote:

What I want to do is to script the creation of that event, and for that I need to specify the filepath property of the "open file alert" but *that* doesn't work.

As noted elsewhere, you can't do that since macOS 10.9. The filepath property is a vestige of bygone carefree days.


Jean-Christophe Helary
-----------------------------------------------
@brandelune http://mac4translators.blogspot.com


Shane Stanley
 

On 2 Dec 2017, at 2:19 pm, Jean-Christophe Helary <jean.christophe.helary@...> wrote:

Ok, so the only way to actually launch a script at a given date/time is to use launchd I guess ?
You can still do it via Calendar.app -- you just have to set the file manually. Otherwise it's probably a matter of launchd or a third-party utility.

--
Shane Stanley <sstanley@...>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>

Jean-Christophe Helary <jean.christophe.helary@...>
 

I just wrote an article on that issue. Thank you all for your help.
(any comments are welcome)

https://mac4translators.blogspot.jp/2017/12/automation-scheduling-file-and.html

On Dec 2, 2017, at 12:19, Jean-Christophe Helary <jean.christophe.helary@...> wrote:

Ok, so the only way to actually launch a script at a given date/time is to use launchd I guess ?

On Dec 2, 2017, at 12:13, Shane Stanley <sstanley@...> wrote:

What I want to do is to script the creation of that event, and for that I need to specify the filepath property of the "open file alert" but *that* doesn't work.

As noted elsewhere, you can't do that since macOS 10.9. The filepath property is a vestige of bygone carefree days.

Jean-Christophe Helary
-----------------------------------------------
@brandelune http://mac4translators.blogspot.com


2551phil
 

Good work! I shall retweet that.

BTW, you don’t need to specify both Program and Program Arguments in your plist. If you leave out the Program key (and the osascript string that follows it), launchd will take the first item in the Program Arguments array as the Program (which you correctly have as the path to osascript).

The way you’re doing it, you’re technically passing /usr/bin/osascript as the first argument to /usr/bin/osascript, which you don’t really want to do.


Best


Phil
@sqwarq


On 7 Dec 2017, at 12:35, Jean-Christophe Helary <jean.christophe.helary@...> wrote:

I just wrote an article on that issue. Thank you all for your help.
(any comments are welcome)

https://mac4translators.blogspot.jp/2017/12/automation-scheduling-file-and.html

On Dec 2, 2017, at 12:19, Jean-Christophe Helary <jean.christophe.helary@...> wrote:

Ok, so the only way to actually launch a script at a given date/time is to use launchd I guess ?

On Dec 2, 2017, at 12:13, Shane Stanley <sstanley@...> wrote:

What I want to do is to script the creation of that event, and for that I need to specify the filepath property of the "open file alert" but *that* doesn't work.

As noted elsewhere, you can't do that since macOS 10.9. The filepath property is a vestige of bygone carefree days.

Jean-Christophe Helary
-----------------------------------------------
@brandelune http://mac4translators.blogspot.com



Jean-Christophe Helary <jean.christophe.helary@...>
 



On Dec 7, 2017, at 17:34, 2551phil <2551phil@...> wrote:

Good work! I shall retweet that.

Wow, thank you Phil :-)

BTW, you don’t need to specify both Program and Program Arguments in your plist.

I know. I wrote about that in the comments. It is just that the sample files I got all had both, so I though it was better to keep the data just to be on the safe side.

If you leave out the Program key (and the osascript string that follows it), launchd will take the first item in the Program Arguments array as the Program (which you correctly have as the path to osascript).

The way you’re doing it, you’re technically passing /usr/bin/osascript as the first argument to /usr/bin/osascript, which you don’t really want to do.

Are you sure? That's not what I understood from the docs. My understanding was that if Program Arguments is there, Program is ignored. And the first element of Program Arguments is the argument to exec.


Jean-Christophe Helary
-----------------------------------------------
@brandelune http://mac4translators.blogspot.com


2551phil
 


On 7 Dec 2017, at 15:41, Jean-Christophe Helary <jean.christophe.helary@...> wrote:

My understanding was that if Program Arguments is there, Program is ignored.


Well, the docs say this:

  Program <string>
     This key maps to the first argument of execv(3) and indicates the abso-
     lute path to the executable for the job. If this key is missing, then the
     first element of the array of strings provided to the ProgramArguments
     will be used instead. This key is required in the absence of the
     ProgramArguments key.

     ProgramArguments <array of strings>
     This key maps to the second argument of execvp(3) and specifies the argu-
     ment vector to be passed to the job when a process is spawned. This key
     is required in the absence of the Program key.  IMPORTANT: Many people
     are confused by this key. Please read execvp(3) very carefully!


You may be right (I’d assume so, if your test scripts work), but I don’t see a statement that quite supports what you said. But at best it’s redundant to use Program when you’ve specified Program Args and correctly specified the path to the executable in the first item of the array. At worst, I dunno…


Best


Phil
@sqwarq

Jean-Christophe Helary <jean.christophe.helary@...>
 

Phil,

You're right. I was confused. I'll correct the post later today.

Jean-Christophe 

On Dec 7, 2017, at 17:50, 2551phil <2551phil@...> wrote:


On 7 Dec 2017, at 15:41, Jean-Christophe Helary <jean.christophe.helary@...> wrote:

My understanding was that if Program Arguments is there, Program is ignored.


Well, the docs say this:

  Program <string>
     This key maps to the first argument of execv(3) and indicates the abso-
     lute path to the executable for the job. If this key is missing, then the
     first element of the array of strings provided to the ProgramArguments
     will be used instead. This key is required in the absence of the
     ProgramArguments key.

     ProgramArguments <array of strings>
     This key maps to the second argument of execvp(3) and specifies the argu-
     ment vector to be passed to the job when a process is spawned. This key
     is required in the absence of the Program key.  IMPORTANT: Many people
     are confused by this key. Please read execvp(3) very carefully!


You may be right (I’d assume so, if your test scripts work), but I don’t see a statement that quite supports what you said. But at best it’s redundant to use Program when you’ve specified Program Args and correctly specified the path to the executable in the first item of the array. At worst, I dunno…


Best


Phil
@sqwarq

Jean-Christophe Helary
-----------------------------------------------
@brandelune http://mac4translators.blogspot.com


Jean-Christophe Helary <jean.christophe.helary@...>
 

On Dec 7, 2017, at 17:34, 2551phil <2551phil@...> wrote:

Good work! I shall retweet that.

BTW, you don’t need to specify both Program and Program Arguments in your plist. If you leave out the Program key (and the osascript string that follows it), launchd will take the first item in the Program Arguments array as the Program (which you correctly have as the path to osascript).

The way you’re doing it, you’re technically passing /usr/bin/osascript as the first argument to /usr/bin/osascript, which you don’t really want to do.
Ok, I found where I got this idea. It's in the http://www.launchd.info link:

When you provide both keys, the value of Program is the executable to be started. The first string in ProgramArguments will show up as argv[0] in the executable. If this makes no sense to you, just think that launchd will ignore it.
And indeed, it did not make sense to me :) But since it works I thought my comments in the code were not entirely wrong.

JC

2551phil
 

If it’s correct, it means what you think it means, but I’d still argue that unless that's officially documented, it is better not to rely on it.



Best


Phil
@sqwarq


On 7 Dec 2017, at 16:37, Jean-Christophe Helary <jean.christophe.helary@...> wrote:



On Dec 7, 2017, at 17:34, 2551phil <2551phil@...> wrote:

Good work! I shall retweet that.

BTW, you don’t need to specify both Program and Program Arguments in your plist. If you leave out the Program key (and the osascript string that follows it), launchd will take the first item in the Program Arguments array as the Program (which you correctly have as the path to osascript).

The way you’re doing it, you’re technically passing /usr/bin/osascript as the first argument to /usr/bin/osascript, which you don’t really want to do.

Ok, I found where I got this idea. It's in the http://www.launchd.info link:

When you provide both keys, the value of Program is the executable to be started. The first string in ProgramArguments will show up as argv[0] in the executable. If this makes no sense to you, just think that launchd will ignore it.

And indeed, it did not make sense to me :) But since it works I thought my comments in the code were not entirely wrong.

JC


Shane Stanley
 

On 7 Dec 2017, at 4:35 pm, Jean-Christophe Helary <jean.christophe.helary@...> wrote:

I just wrote an article on that issue.
May I ask what version of the OS you are running?

--
Shane Stanley <sstanley@...>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>

Jean-Christophe Helary <jean.christophe.helary@...>
 

Well, thank you very much for pointing that out. I'll edit the post anyway so that it sticks to the documentation.

Jean-Christophe

On Dec 7, 2017, at 18:43, 2551phil <2551phil@...> wrote:

If it’s correct, it means what you think it means, but I’d still argue that unless that's officially documented, it is better not to rely on it.



Best


Phil
@sqwarq


On 7 Dec 2017, at 16:37, Jean-Christophe Helary <jean.christophe.helary@...> wrote:



On Dec 7, 2017, at 17:34, 2551phil <2551phil@...> wrote:

Good work! I shall retweet that.

BTW, you don’t need to specify both Program and Program Arguments in your plist. If you leave out the Program key (and the osascript string that follows it), launchd will take the first item in the Program Arguments array as the Program (which you correctly have as the path to osascript).

The way you’re doing it, you’re technically passing /usr/bin/osascript as the first argument to /usr/bin/osascript, which you don’t really want to do.

Ok, I found where I got this idea. It's in the http://www.launchd.info link:

When you provide both keys, the value of Program is the executable to be started. The first string in ProgramArguments will show up as argv[0] in the executable. If this makes no sense to you, just think that launchd will ignore it.

And indeed, it did not make sense to me :) But since it works I thought my comments in the code were not entirely wrong.

JC



Jean-Christophe Helary
-----------------------------------------------
@brandelune http://mac4translators.blogspot.com


Jean-Christophe Helary <jean.christophe.helary@...>
 



On Dec 7, 2017, at 20:07, Shane Stanley <sstanley@...> wrote:

On 7 Dec 2017, at 4:35 pm, Jean-Christophe Helary <jean.christophe.helary@...> wrote:

I just wrote an article on that issue.

May I ask what version of the OS you are running?

10.13.1


Jean-Christophe Helary
-----------------------------------------------
@brandelune http://mac4translators.blogspot.com


Jean-Christophe Helary <jean.christophe.helary@...>
 

Phil,

I've at last update the article. Thank you for your input.

Jean-Christophe 


On 2017/12/07, at 18:43, 2551phil <2551phil@...> wrote:

If it’s correct, it means what you think it means, but I’d still argue that unless that's officially documented, it is better not to rely on it.



Best


Phil
@sqwarq


On 7 Dec 2017, at 16:37, Jean-Christophe Helary <jean.christophe.helary@...> wrote:



On Dec 7, 2017, at 17:34, 2551phil <2551phil@...> wrote:

Good work! I shall retweet that.

BTW, you don’t need to specify both Program and Program Arguments in your plist. If you leave out the Program key (and the osascript string that follows it), launchd will take the first item in the Program Arguments array as the Program (which you correctly have as the path to osascript).

The way you’re doing it, you’re technically passing /usr/bin/osascript as the first argument to /usr/bin/osascript, which you don’t really want to do.

Ok, I found where I got this idea. It's in the http://www.launchd.info link:

When you provide both keys, the value of Program is the executable to be started. The first string in ProgramArguments will show up as argv[0] in the executable. If this makes no sense to you, just think that launchd will ignore it.

And indeed, it did not make sense to me :) But since it works I thought my comments in the code were not entirely wrong.

JC



Jean-Christophe Helary
-----------------------------------------------
@brandelune http://mac4translators.blogspot.com