How to use passed arguments in Apple Script

I’m attempting to execute something in Apple Script based on information passed on from the OverSight App (https://objective-see.org/products/oversight.html).

Oversight gives me the option to Execute an action (script, binary, etc.) when it logs that e.g. the webcam is turned on, and it allows me to “Pass Arguments” with the execution. These are the arguments OverSight will pass:


I am an absolute rookie at Apple Script so please bear with me…

How would I go about extracting say the state of and use it in an IF ELSE scenario within my Apple Script?

Any help is appreciated. Thanks.

You can get the arguments by using an explicit run handler like so

 on run args
   if class of args is list then -- arguments passed come in as a list
       -- do your thing here to parse the list of arguments
   end if
end run

In the way this run handler is declared, the value of args will always be a list class object, so testing this condition is superfluous.

But what I wanted to add to this was that I get the impression from the OP that this script will probably need to be executable, as it looks like it either might be getting run from the command line or within a shell process. If so, there’s a few more steps that are worth mentioning so that the OP can get it working.

I tend to save all of my scripts in plain text format, so they can be easily read and edited on the command line. For this reason, I get annoyed with Script Editor, as it writes its .applescript plain text files using macroman text encoding instead of UTF-⒏ So if you can use a plain text editor (such as TextEdit, which you can set to write UTF-8 encoded plain text files), this saves some bother later that would otherwise give rise to errant characters preventing execution by a program expecting a UTF-8 encoded file.

The very first line of the script should be this shebang:[format]#!/usr/bin/env osascript[/format]

It must be the very first line, and contain only that. An alternative shebang is:[format]#!/usr/bin/osascript[/format]

Either of these should be perfectly valid, but use only one of them, not both.

The rest of the script is then much as Rob intimated. Here’s a very short example of what your AppleScript could look like:

#!/usr/bin/env osascript
--------------------------------------------------------------------------
use scripting additions
--------------------------------------------------------------------------
### IMPLEMENTATION:
on run argv
 		set the text item delimiters to linefeed
 		set msg to argv as text
 		display alert msg
end run
--------------------------------------------------------------------❮!END❯

Any line that begins with two consecutive hyphens (–) or a single hash (#)—regardless of any other characters that come after these—are ignored by the AppleScript compiler and treated as comments. The run handler is the bit doing the work. As Rob stated, the parameters passed to the script and into the variable I’ve named argv (you can name it something else if you wish) get assigned as individual items that form a list (similar to an array). They will be assigned in the order that they get passed by OverSight, so you should expect that the value of argv will be something like this:[format]{“-d”, “camera”, “-event”, “on”, “-process”, “52124”}[/format]
i.e. 6 items, in that order.

My script simply elects to take the list of items and coerce it into a string. To do this, you first have to set the text item delimiters to something sensible, as this is what is used to join the separate list items together to make a single piece of text. In this case, it’s joining each argument with a linefeed character, which then is easily displayed in an alert dialog box by the final command. The joined text displayed in the message would look like this:[format]“-d
camera
-event
on
-process
52124”[/format]

(without the quotation marks). Here’s a slightly different script to answer your specific query about accessing particular parameters passed to the script:

#!/usr/bin/env osascript
--------------------------------------------------------------------------
use scripting additions
--------------------------------------------------------------------------
### IMPLEMENTATION:
on run argv
 		set {device, state} to {item 2, item 4} of argv
 		if the device ≠ "camera" then return false
 		if the state = "off" then return display notification ¬
 		 		"The camera is no longer active."

 		display alert "The camera has been activated."
end run
--------------------------------------------------------------------❮!END❯

What this script does is pick out the two arguments that are of interest to you, which we’re working on the assumption that these will be the second and fourth items in the list assigned to argv. It saves these values in variables device and state, respectively. It checks the value of device, and in this case, it’s querying whether it is anything other than the string “camera”, because if it is, then there’s no reason to proceed and the script terminates by returning the value false. You won’t get to see this value, as it will be sent back to the OverSight parent process, but you don’t need to see it.

If it turns out we are dealing with the camera, then what happens next depends whether the camera went from being on to being turned off, in which case I’ve chosen to view this as less significant, so have chosen to notify the user by a fleeting notification that appears where notifications normally appear then disappear. However, in the event that camera goes from being off to being turned on, this gets sent to the user as an alert that pops up on screen and physically needs to be clicked in order to dismiss it.

Hopefully this gives you a starting point you can build from.

IMPORTANTLY you will want to save your .applescript file and then go to the command line (e.g. using the Terminal.app) to make it executable with the following command:[format]chmod +x /path/to/file.applescript[/format]