Just when I thought I was done writing my last AppleScript, I was forced to write another one! And this time it had to be launched by a login hook.
I needed a script that would display a warning to users, and the one thing I do find handy about AppleScript is its ability to easily create dialog boxes. I also find a login hook best suited for deploying warnings because the desktop won’t appear until the login hook process is finished, meaning that it can’t be as easily ignored 🙂 I’m evil, I know.
If you find yourself in the same boat, some helpful tips are after the jump.
Login hooks can’t launch an AS by calling it directly, so you need to create a shell script that uses /usr/bin/osascript to call it.
If you’re calling your AS with osascript and you need to display dialog boxes, make sure you create them from within the context of an application such as System Events otherwise you’ll get “execution error: No user interaction allowed. (-1713)”. You should also quit System Events once you’re finished because it’s good practice not to leave unnecessary processes running. This looks like the following:
tell application "System Events"
display dialog "I'm sorry, Dave. I'm afraid I can't do that."
tell application "System Events" to quit
In a login hook script the $1 variable returns the short name of the user logging in. If you need that short name for anything in your AS, you can pass it when you’re calling your AS with osascript. This is important to note because if you try and calculate the short name of the user in your AS, you’ll most likely end up getting “root” as the answer because login hooks run as root.
/usr/bin/osascript /path/to/applescript.scpt $1
Expanding on above, if you’re passing any parameters you’ll need to add the run handler to your AS in order to use them. From the osascript man page, “[a]ny arguments following the script will be passed as a list of strings to the direct parameter of the ‘run’ handler.” This looks like the following:
on run (argv)
# Everything else here
display dialog "The short user name is " & item 1 of argv
I hope these tips have helped!