$3 equivalent for AppleScript

cbrewer
Valued Contributor II

Using my first AppleScript on login and wondering if there's an equivalent to using $3 in bash. Right now I'm finding the current user with a do shell script to look at the owner of console. Is there a better way?

6 REPLIES 6

talkingmoose
Moderator
Moderator

Use these:

short user name of (system info)
long user name of (system info)

cbrewer
Valued Contributor II

I've used those for other scripts, but I was under the impression they wouldn't work for a login script. Are AppleScripts run as the user and not as root?

talkingmoose
Moderator
Moderator

What's your end goal? How are you wanting to use AppleScript at login?

AppleScript was really designed to be used interactively with GUI applications. GUI applications run under the Finder. The Finder runs only under a user's login. Yes, AppleScript can do some things that don't require a GUI but not much to be very useful.

Shell scripts were designed to run command line (non-GUI) applications. Shell scripts don't require the Finder to be running, which means the console can be sitting at the login window.

I wouldn't suggest ever adding AppleScripts to Casper Admin to be run by Casper as the root account.

Hope that makes sense.

cbrewer
Valued Contributor II

End goal is creating a finder alias for a given user on login. I've been using symbolic links in the past, but Lion has some issues with using symbolic links from the GUI. Only way I know of to make a legit Finder alias via a script is with AppleScript. To make the alias I need to know the username of user who is logging in.

Right now I'm using:

set loggedInUser to do shell script "/usr/bin/who | awk '/console/{print $1}'"

talkingmoose
Moderator
Moderator

Ooh, yeah, that one's sticky.

The Finder creates aliases, which means an instance of the Finder must be running for AppleScript to control.

My solution would be a combination AppleScript + launchd item. The launchd item would run at login and call the AppleScript. The script would then create the alias. The downside is that this must be deployed and will not simply run as a command line in a JSS policy.

Modify this as necessary and save it as an AppleScript application:

tell application "Finder"
    make alias to /path/to/my/file.txt at desktop
end tell

Alternatively, put this into a shell script so that the user doesn't see an AppleScript actually launch:

do shell script "tell application "Finder" make alias to /path/to/my/file.txt at desktop"

Modify this launchd agent to point to your AppleScript or shell script on the user's computer:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>pvt.talkingmoose.LaunchMyScript</string>
    <key>ProgramArguments</key>
    <array>
        <string>open</string>
        <string>-a</string>
        <string>/Library/Talkingmoose/MyScript.app</string> <!-- or MyScript.sh -->
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Use Composer to package the two items and place them in these locations:

/Library/LaunchAgents/pvt.talkingmoose.LaunchMyScript.plist
/Library/Talkingmoose/LaunchMyScript.app # or /Library/Talkingmoose/LaunchMyScript.sh

Probably not what you were hoping to do but AppleScript doesn't have a built-in way to switch out of the current user to another user.

cbrewer
Valued Contributor II

Hey thanks for putting so much time into this. I've already got a script that is getting the job done for me. I'm running it at login through a JSS policy and it seems to work fine. I was really just looking for a better way to grab the current user shortname. Here's what I got:

set loggedInUser to do shell script "/usr/bin/who | awk '/console/{print $1}'"

--Create Staff Link
tell application "Finder"
    if exists ("/Volumes/staff") as POSIX file then
        make alias file to ("/Volumes/staff/" & loggedInUser) as POSIX file at ("/Users/" & loggedInUser & "/Documents") as POSIX file with properties {name:"Network Docs"}
    end if
end tell