Launch multiple Scripts from PLIST LaunchAgent

I’m currently making a seperate plist LaunchAgent for each script I want to run at login. I want to combine them all into one plist. Is this possible?

Hi Gokussl,

Did you try adding another script to the plist? What happened?

What kind of scripts are you writing about (AppleScipts or unix script)?

Edited: note that from one shell script you can launch multiple scripts.

gl,
kel

I did , I added two more scripts. It didn’t load any of the Applescripts. I also tried adding the osacript in between each of them. It still caused it to not load.

Is it possible to do another section?

<?xml version="1.0" encoding="UTF-8"?> Label com.refresh-loader ProgramArguments osascript /Scentsy/StartupScripts/ScentsyMail/ScentsyMailRO.scpt [b] /Scentsy/test/S1RO.scpt /Scentsy/test/S2RO.scpt[/b] RunAtLoad StartInterval 18000

I’m quite sure you can’t run compiled scripts (AppleScripts) from the launch agent. I think you need a shell script to do it.

ps. I’m reviewing this stuff too. I can’t find a few posts. Can’t remember if you can use osascript in the plist.

gl,
kel

This doesn’t look right:

gl,
kel

Hi,

the program argument of a launchd agent can contain only one script path.
But you can run a script which launches the others.

osascript is a shell command

you can

Okay…

So launch the .sh script from the LaunchAgent plist and just have all the applescripts launch from that?

Example:


#!/bin/sh

osascript script1.scpt
osascript script2.scpt


If one of these .scpt’s has displaydialogs or needs interaction , will it still work the same way when launching it from the shell?

Don’t you mean launch daemons?

You can, I’m using it too but there are some guidelines to follow. First of all AppleScript needs to be able to connect to the a window server, to do that it means that the user who is running the script needs to be a console user. If the user of the process (running the script) is not a console user, the script won’t launch. Secondly, you can’t use any user interaction by the script itself when launched by osascript. Osascript is a command line utility that is not connected to the window server (note: it still requires a console user to launch it). So commands to show dialogs in an application’s context is therefore not possible. Avoid any of these commands (like display dialog) or send user interaction to another running application that is connected to the window server as an application or an agent (not a daemon) but not yourself. I prefer applications like Dock, SystemUIServer or loginwindow who are always running, only difference is that Dock and SystemUIServer only runs when an user is logged in.

It’s not the plist that is the problem, the manual about osascript says the following:

It’s clear that the only one file can be executed by AppleScript.

[applescript=Gokussl]So launch the .sh script from the LaunchAgent plist and just have all the applescripts launch from that?



That is indeed the way to go, you can also make a plain AppleScript who loads the files using a shebang.

[code]#!/usr/bin/osascript
run script file "script1.scpt"
run script file "script2.scpt"
run script file "script3.scpt"[/code]
Safe it is as plain text (no extension needed) and set the executable flag to true (chmod +x). The program arguments values in your plist will only contains the path to the script, without osascript. Because the script includes a shebang (hashbang), the second to last line of the script is send to the osascript compiler. It's something the program loader of your system recognizes when a program is loaded. When a text file is set as executable, begins with '#!' and is send to the program loader, it will read the first line as the path to the interpreter and the file you wanted to load is the first argument for the given interpreter. Therefore the script have to support that lines starting with a hash are comments (supported since AppleScript 2.0).

DJ,

That’s no longer the case. From the 10.9 release notes:

Ah, thanks for the heads up (also glad to hear they have improved scripting addition’s event calling). :cool:

Lets recap!

So what you’re saying is if I setup my script launching structure with a single plist connected to a loader.sh file with the osascript command launching my applescripts. This will only work properly on 10.9 because my display dialogs will not work in 10.7 and 10.8?

Exactly, therefore you can take my first advice and send the display dialogs to SystemUIServer:

tell application "SystemUIServer" to display dialog "hello world!"

The code above is safe to use through all OS X versions on the command line.

I appreciate the help. I was able to get my test environment setup with 2 launchagents. One for Startup and one for Refresh. These both linked to .sh scripts that launch the corresponding applescripts.

Its much cleaner now. Thanks