Create variable alias to desktop on account creation

tell application “Finder” to make new alias at desktop to folder ((\test-my.sharepoint.com@SSL\DavWWWRoot\personal\s-test_student_test_edu\Documents) & “Storage”)
Having no real experience per say with apple script we are trying to create an alias to the above location on login, and this is what I have found so far. The user account portion s-test_student_test_edu would have to be the currently logged in user however, is there a %username% variable that I could use in this case?

Hi Leif,

It has been a while, but I got this:

tell application "System Events"
	set cur_user to (current user)
	set home_folder to ((home directory of cur_user) as POSIX file) as alias
end tell

I’m thinking that there might be a better way.

gl,
kel

It might be better like this, but almost the same:

tell application "System Events"
	set cur_user to (current user)
	set home_folder to (POSIX file (home directory of cur_user)) as alias
end tell

gl,
kel

Hi,

desktop in Finder terminology points always to the desktop folder of the current user

A Finder alias is called alias file in AppleScript.
The string path must begin with a disk name and be separated by colons, for example


tell application "Finder"
	make new alias file at desktop to folder (("DavWWWRoot:personal:s-test_student_test_edu:Documents:") & "Storage")
end tell

still simpler


tell application "System Events"
	set home_folder to alias (path of home folder)
end tell

It throws an error on my machine but it can be done simpler:

path to home folder -->result: alias to your home folder

What is a wrongdoer in your proposed instruction is the structure of the path to the original document.

The Finder accepts only HFS paths which means “path:to:the:folder:containing:the:file”
If you are given an Unix pathname you will have to use POSIX file “/to/the/folder/containing/teh/file”
I dropped the first word assuming that it’s the volume name.

What you inserted in your instruction (using backslash as separator) is neither an HFS pathname nor an Unix one.

Yvan KOENIG (VALLAURIS, France) jeudi 1 mai 2014 16:17:56

I don’t remember seeing ‘home directory of current user’ before. Maybe Apple is losing Standard Additions?

No the proper way is actually:

path to home folder from user domain

but that’s actually an understatement because there is only one home folder. Other domains can be used when using path to preferences for example. The path to command isn’t deprecated (yet) so that should be the code you should use IMO.

I found this in another forum and it worked for me:

set theTargetFolder to ((path to Desktop Folder) & “name of target folder”) as string

. although there was an earlier ‘path to’ parameter for this, which still works in Mavericks:

path to current user folder

Hi.

The problem with this arrangement is that (path to desktop folder) returns an alias. Concatenating text to this produces a list containing the alias and the text ” {b]{alias “MacBook Pro HD:Users:nigelgarvey:Desktop:”, “name of target folder”}[/b] ” which is then coerced to text. This is both inefficient and subject to the current value of AppleScript’s text item delimiters, as in this demo:

set AppleScript's text item delimiters to "HELLO!"
set theTargetFolder to ((path to desktop folder) & "name of target folder") as string
--> "HD Name:Users:username:Desktop:HELLO!name of target folder"

It would be better to phrase the line this way:

set theTargetFolder to (path to desktop folder as text) & "name of target folder"

Here, (path to desktop folder as text) returns a text path directly and concatenating text to this produces yet more text without the intermediate list and without involving delimiters:

set AppleScript's text item delimiters to "HELLO!"
set theTargetFolder to (path to desktop folder as text) & "name of target folder"
--> "HD Name:Users:username:Desktop:name of target folder"

Thank you Nigel!

I needed the simplest way to create a variable which returned the path to a logo on the current user’s desktop. Or in a folder in the user’s home folder. I chose desktop because the script seems to run a little faster and it’s easier to explain to the end user. I found this forum but none of the other solutions did what I was looking for. The way I wrote the variable originally did what I needed it to do on Mavericks and Snow Leopard. But I liked your explanation of why this is better so I changed it. I tested both these scripts on Mavericks and Snow Leopard and they worked.

– I used this to return the path to the folder on the desktop:
set theTarget to (path to desktop as text) & “Logo_1,5w.ai”
– result: “Macintosh HD:Users:username:Desktop:Logo_1,5w.ai”

– I used this to return the path to the file in a folder in the Documents folder:
set theTarget to (path to home folder as text) & “Documents:Logos:Logo_1,5w.ai”
– result: “Macintosh HD:Users:username:Documents:Logos:Logo_1,5w.ai”

This is fine. But there is also a ‘documents folder’ parameter if you want to use it:

set theTarget to (path to documents folder as text) & "Logos:Logo_1,5w.ai"