Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Getting the current user from AppleScript

Getting the current user from AppleScript
Thread Tools
okandeel
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Cairo, Egypt
Status: Offline
Reply With Quote
Jul 11, 2001, 05:30 AM
 
Assalamu 3alaikum,
I am trying to retreive the current user using AppleScript. My purpose is to access his preferences folder and add certain files to it. Since I have no idea who the logged on user can be, I have to know that first and thus know where I can get the preferences folder. How can I do that with AppleScript. Or is there a way in which I can access the folder without knowing the user who is currently logged? I am not an AppleScript expert so I need help. I want to write the script under Mac OS X of course. Thanks a lot
     
acur128
Junior Member
Join Date: May 2001
Status: Offline
Reply With Quote
Jul 11, 2001, 10:13 PM
 
OS X uses "domains" to distinguish the various /Library folders... (system domain, local domain, network domain or user domain)
It seems to work fairly well. To get the name of the current user, you could use:

set user_name to name of (info for (path to "cusr" from user domain))

The (path to "cusr" from user domain) gets the current user's home directory as an alias - something like:
alias "DiskName:Users:UserName:"

To get the preferences folder for the current user, though, you don't really need to know who the current user is; you could use:

set user_preferences to (path to preferences from user domain)

This would return something like:
alias "DiskName:Users:UserName:Libraryreferences:"

Some, but by no means all, of the /Library folders can be accessed in this way.
     
okandeel  (op)
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Cairo, Egypt
Status: Offline
Reply With Quote
Jul 12, 2001, 07:06 AM
 
Are u sure about these lines? They don't seem to work with me at all!!!
I tried to make as much logical modifications as I can to make these expressions work, but all in vain. I like the idea of getting the preferences folder without knowing who the current user is, but I just can't get the expression to work. It seems to be syntactically faulty. Are u sure these expressions are right?
     
acur128
Junior Member
Join Date: May 2001
Status: Offline
Reply With Quote
Jul 12, 2001, 07:37 AM
 
As sure as I can be... they work for me with OS X 10.0.4, Applescript 1.6.
But they'd only work in OS X Script Editor & OS X Applets (not Classic), since the domain stuff comes from the OS X Standard Additions scripting addition.

I guess a couple things to try...
- make sure the "StandardAdditions.osax" scripting additions is installed in /System/Library/ScriptingAdditions/ (this is installed by the default OS X install, so it should be there)

- perhaps try adding the word "folder" to the (path to preferences...) line:

set user_preferences to (path to preferences folder from user domain)

Either with or without "folder" *should* work to "get" the folder, but (should have mentioned) the "Finder" appears to need the word "folder" to know what you're talking about, so if you want to relay the preferences folder alias to a "Finder" task, use the above. Alternatively, the preferences folder has the "user domain" as its default, so "from user domain" isn't formally necessary unless you want to refer to another preferences folder in the same script. So this should also work:

set x to (path to preferences folder)

The above line should also return the current user's preferences folder. Otherwise, not sure what's amiss - hope something works!

Edit Note: You may want to try a test to see if it's working the way it should - maybe just add a test folder to the preferences folder:

tell application "Finder" to make folder at (path to preferences folder) with properties {name:"Test"}

The above should add a new folder named "Test" to the current user's preferences folder.

[ 07-12-2001: Message edited by: acur128 ]
     
okandeel  (op)
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Cairo, Egypt
Status: Offline
Reply With Quote
Jul 12, 2001, 08:24 AM
 
You won't believe why it wasn't working!!! It was because I wrote the script like this
tell "Finder
...
...
end tell

when I should have written it like this
tell application "Finder"!!!!!

I guess this is an example of what an unexperienced AppleScript write would normally face!!!! But thanks a lot for your responses!!! It's working
fine now!! :-))
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 06:56 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,