Run shell command in AppleScript

musat
Contributor III

Hello,

We are in the process of switching from Windows 7 tablets to MAcBook Airs. I'm working on writing an Applescript that will migrate a user's backup data from the tablet into the correct folders on the Mac. I've got the script pretty well ironed our, but now see that I need the variable for the logged in user. I have tried

set user to do shell script "/usr/bin/logname"

and

set user to do shell script "/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'"

but they both result in an error on the JSS of:

Script result: /private/tmp/MacPCDataRestore.scpt: execution error: No user interaction allowed. (-1713)

Any ideas on how to accomplish this in an Applescript? I could do this all as a shell script, but I wanted to be able to pop up dialog boxes letting the user know what was happening. So the other option would be how to display messages from a shell script.

Thanks, Tim

1 ACCEPTED SOLUTION

musat
Contributor III

Talkingmoose, that still returns "root" as the user.

What I settled on was:

set user to do shell script "/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'"

and that seems to work.

View solution in original post

9 REPLIES 9

nessts
Valued Contributor II

i would learn to do it in perl or bash, applescript has gotten pretty good about not letting you do much without going through hoops left and right. Applescripts must pretty much run as the user in control of the desktop display. you can use cocoa dialog to display messages out of bash scripts much easier than you can get root to run an applescript.

either that or change what is running this apple script to a launchagent or give the user something to click on.

clifhirtle
Contributor II

set current_user to (do shell script "whoami")

talkingmoose
Moderator
Moderator

No need to delve into shell scripting if you're already working in AppleScript. This by itself is enough in AppleScript:

(system info)

will give you a dump of a lot of information. You'll notice both account name and display name are included. To get them you'd use:

short user name of (system info)

or

long user name of (system info)

musat
Contributor III

Talkingmoose, that still returns "root" as the user.

What I settled on was:

set user to do shell script "/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'"

and that seems to work.

talkingmoose
Moderator
Moderator

How are you running this script?

Use a shell script when you need to run something while no user is logged in to the computer. Use AppleScript when you need to run under a particular user's credentials.

Although you can throw AppleScripts into Casper Admin and push them via a policy or Casper Remote I don't recommend doing that. Use shell scripts instead.

tlarkin
Honored Contributor

Casper runs script as root. So detecting who owns /dev/console is a neat trick to use when you want to run something as that user, or perhaps set the current user as a variable.

Another method is to save the applescript as an application and then have the current user open it via a shell script using `sudo -u` and detecting who owns /dev/console to then input the current user into that command.

I just did this recently for some exchange migration settings in outlook. There is no way to do it via shell script but they do have Applescript dictionaries. So, I wrote a script that opens my applescript app as the current user, so the current user could set the exchange server. Sort of a backwards way, but it worked.

musat
Contributor III

I was running this script through a Casper Policy, so it was being run as root. I could have created a policy that copied the script to the desktop and then have the user manually run it, but that was one step too many for many of our users. Tacking that line to the beginning of my Applescript allowed me to have a one-click icon in Self-Service that people can use to run this.

Additionally, the error I was receiving (about "No user interaction allowed") was due to the "display dialog" line in my script. I have changed all of my "display dialogs" to output to a log file instead.

perrycj
Contributor III

@tlarkin I am running into a similar problem. I need to run an installed applescript as the user who is logged in but i can't do it through casper as it only runs it in the casperssh account.

What shell script do you use to launch the applescript and do you deploy that shell script through casper/casper policy? Nothing I try works (although the applescript works perfect) but I can't get it to run though a policy or even through remote.

Snickasaurus
Contributor

@perrycj
You could:
1 Copy the Applescript over to a list of machines with ARD
2 Push it out in a Casper package to a "Scripts" folder or the like
Then, use the shell script as a policy to call/run the Applescript on each client.
I have been working on a hidden scripts folder populated with quite a few that I've written over the last year that will hopefully replace several utility apps I've been using.