Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
 
Thread Tools Rate Thread Display Modes
Old 07-02-2012, 12:48 PM   #1
honda03v6
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
honda03v6 is offline   Reply With Quote
Old 07-02-2012, 01:09 PM   #2
hayne
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
hayne is offline   Reply With Quote
Old 07-02-2012, 05:33 PM   #3
ganbustein
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
say instead:
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"
where I've highlighted in red the parts you should pay special attention to.


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:"
ganbustein is offline   Reply With Quote
Old 07-02-2012, 06:26 PM   #4
hayne
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
hayne is offline   Reply With Quote
Old 07-03-2012, 07:53 AM   #5
honda03v6
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
honda03v6 is offline   Reply With Quote
Old 07-03-2012, 08:17 AM   #6
honda03v6
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!
honda03v6 is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting 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

Forum Jump



All times are GMT -5. The time now is 04:39 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.