|
|
#1 |
|
Prospect
Join Date: Jul 2012
Posts: 3
|
Replace Existing Mapped Alias
New to Applescript and am in need of some help. This script creates an alias for our network drives just fine, but if the user deletes this "Network Drives" folder in the sidebar and needs it back again they have to run the script again but the issue is it makes another copy of the alias' again in this folder, which I do not want to happen I just want to have one set all the time no matter how often they run this script. I tried playing around with set tPath and set tName but I can't seem to get it right. Thanks in advance.
tell application "Finder" try make new folder at (path to startup disk as text) & "Applications" with properties {name:"Network Drives"} on error error_message display dialog "A Network Drives folder already exists in Applications. This will re-create a Network Drives shortcut in your sidebar" end try tell application "Finder" set tPath to (path to startup disk as text) & "Applications:Network Drives" set tName to "smb://network/1" if exists alias (tPath & tName) then delete alias (tPath & tName) make new alias at (path to startup disk as text) & "Applications:Network Drives" to mount volume "smb://network/1" end tell tell application "Finder" activate delay 1 select (path to startup disk as text) & "Applications:Network Drives" delay 1 tell application "System Events" to tell process "Finder" to keystroke "t" using command down close window 1 end tell end tell |
|
|
|
|
|
#2 |
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 31,939
|
I haven't spent the time to look in detail at what you are doing, but usually you need to test to see if the link (or file or whatever) exists already and if so delete it before recreating it.
__________________
hayne.net/macosx.html |
|
|
|
|
|
#3 |
|
MVP
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,009
|
Aliases can be tricky to work with. For the most part, referring to an alias is the same as referring to what the alias points to. If you want to refer to an alias file itself, you have to refer to it as an "alias file" rather than as an "alias". And, of course, you also have to refer to it using its name, not the name of what it's pointing to. To make it easy to find the alias file later by name, create it with a name that you control.
Also, when appending to a foldername, you have to get an intervening colon in there somewhere. That is, when you write (tPath & tName), either tName should begin with a colon or, more usefully, tPath should end with one. It's easier to keep this straight if you follow the convention that a folder's path always ends with a colon. (All the path to ... as text constructs already do this.) Instead of: Code:
set tPath to (path to startup disk as text) & "Applications:Network Drives" set tName to "smb://network/1" if exists alias (tPath & tName) then delete alias (tPath & tName) make new alias at (path to startup disk as text) & "Applications:Network Drives" to mount volume Code:
set tPath to (path to startup disk as text) & "Applications:Network Drives:"
set tName to "smb://network/1"
set aName to "network1 alias" -- or whatever. The name of the alias file itself
set aPath to tPath & aName
if exists alias file aPath then delete alias file aPath
make new alias file at tName with properties {name:aName} to mount volume "smb://network/1"
As an aside, it really seems to me that "/Applications" is not the best place to be storing things that are not applications. Maybe "/Library" would be a better place to create your "Network Drives" folder. That is: set tPath to (path to startup disk as text) & "Library:Network Drives:" |
|
|
|
|
|
#4 |
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 31,939
|
By the way, you might want to look at the AppleScript written by yellow in this older thread: http://hintsforums.macworld.com/showthread.php?t=37778
It seems to have some useful error checking etc. Read through to the end of the thread to get all the improvements by yellow and other people.
__________________
hayne.net/macosx.html |
|
|
|
|
|
#5 |
|
Prospect
Join Date: Jul 2012
Posts: 3
|
Thanks for the help I now understand why aliases are very tricky to work with.
ganbustein, I have done as you stated but I am getting this error - Can’t make "smb://network/1" into type item. This is happening on this line of the script - make new alias file at tName with properties {name:aName} to mount volume "smb://network/1" Any thoughts on this error? Thanks again |
|
|
|
|
|
#6 |
|
Prospect
Join Date: Jul 2012
Posts: 3
|
I got it straightened out:
make new alias file at tPath with properties {name:aName} to mount volume tName Just switched out tName with tPath also added tName at the end instead of showing the full network path thanks again for your help! |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|