Running Applescript via Policy

Anonymous
Not applicable

We're moving AD so need to change domains in Outlook.
I've got an AppleScript that runs in SelfService no problem.

#!/usr/bin/osascript
tell application "System Events"
                     set shortName to name of current user
end tell
tell application "Microsoft Outlook"
                     set domain of exchange account 1 to "domainname"
                     set the user name of exchange account 1 to shortName
end tell

It doesn't run via policy so I've tried to wrap it in a bash script, but no dice... any help much appreciated!

#!/bin/sh
# Discover logged in user
user=`stat -f%Su /dev/console`
sudo -u $user /usr/bin/osascript <<EndMigration
tell application "System Events"
                     set shortName to name of current user
end tell
tell application "Microsoft Outlook"
                     set domain of exchange account 1 to "domainname"
                     set the user name of exchange account 1 to shortName
end tell
EndMigration
5 REPLIES 5

mm2270
Legendary Contributor III

This is a common issue with Applescripts being run by Casper. Since with a normal policy using something like the recurring check-in trigger, its being run in a root context, it has trouble running certain kinds of Applescript commands, for example ones that require user interaction. This is an OS thing, not a problem with anything JAMF has done. Apple has just made it hard to run Applescripts that interact with the logged in user when they are called from a different account on the Mac. Note by "interact" I even mean when calling AS to get information on the current user account.

Just looking at it quickly, In your script, when you use something like set shortName to name of current user my guess is it may be getting something like "root" or your local Casper service account instead of the actual logged in user, so that could be part of the problem.

One thing that might help. Its possible to grab information in bash and pass it on to Applescript. Usually you do this by setting a param in AS by echoing back what the bash script already captured. For example:

#!/bin/bash

loggedInUser=$(stat -f%Su /dev/console)

/usr/bin/osascript << EOF
set userName to do shell script "echo "$loggedInUser""
....
EOF

The above will capture the user with bash and then sets up a parameter in Applescript for that string by using the do shell script command to echo back the bash variable. You can now use the parameter userName in the rest of your Applescript as needed.

I don't know if that's enough to make your script work though, since you're also calling Microsoft Outlook and asking it to do things, which may also fail unless those commands get run as the user. I see you're calling your entire Applescript function as the user with sudo -u. but I've often found that just doesn't work that well.

Anonymous
Not applicable

Thanks for helping out @mm2270 ! Much appreciated. Now my AppleScript isn't playing ball!

Seems like it can't read the end of my AppleScript.

265:265: syntax error: Expected “end” or “end tell” but found end of script. (-2741)

mm2270
Legendary Contributor III

@RDD , you might need to post the full script here. My snippet above was only that, a snippet, that would need to be worked into your own script. If you can post what you have, maybe we can figure out where its going wrong.

Look
Valued Contributor III

I am pretty certain you don't need to use the shell script function to bring a variable into osascript inside a shell/bash script you can just call it directly within the osascript part with something like.

set userName to "$loggedInUser"

You will of course still need to specify the variable beforehand.

bentoms
Release Candidate Programs Tester

You can just use an AppleScript, then she'll put from there if/as needed.