使用AppleScript在用户主页中创建已安装卷的别名

时间:2015-09-11 19:27:28

标签: applescript alias volumes

在Mac OS 10.10上,我正在尝试创建已安装卷的别名,并将别名放在用户的主文件夹中,以便以后可以将其放入扩展坞。该卷是一个名为satffdata $的SMB共享(它是一个隐藏的Windows共享,因此最后是$)。我尝试了不同的路径变体,例如明确定义它并使用内置别名,但无济于事。

请参阅下面的代码,然后输出。我得到的错误代码和我读过的内容表明问题可能是它将路径视为字符串,但是我无法明确定义启动磁盘名称或用户配置文件,因为这些可能在脚本由我们的Casper Suite系统自动运行。

tell application "Finder"
    activate

    try
        display dialog "Setting your StaffData$ (P Drive) Shortcut. Press OK to continue."
        mount volume "smb://servername/staffdata$"
        set staffData to POSIX path of "/Volumes/staffdata$"
        set homeDir to POSIX path of (path to home folder)
        display dialog staffData
        display dialog homeDir
        make new alias to staffData at homeDir
        set name of result to "staffdata$"

    end try
end tell

输出如下:

tell application "Finder"
    activate
    display dialog "Setting your StaffData$ (P Drive) Shortcut. Press OK to continue."
        --> {button returned:"OK"}
    mount volume "smb://servername/staffdata$"
        --> error number -10004
end tell
tell application "Script Editor"
    mount volume "smb://servername/staffdata$"
        --> file "staffdata$:"
end tell
tell application "Finder"
    display dialog "/Volumes/staffdata$"
        --> {button returned:"OK"}
    display dialog "/"
        --> {button returned:"OK"}
    make new alias to "/Volumes/staffdata$" at "/"
        --> error number -10000
end tell

如果您有任何问题,我会尽快回复。谢谢!

1 个答案:

答案 0 :(得分:0)

脚本的主要问题是,Finder不接受POSIX路径。

Finder的字典有一个属性home代表主文件夹,disk个对象可以通过名称轻松指定

试试这个

display dialog "Setting your StaffData$ (P Drive) Shortcut. Press OK to continue."
try
    set volumeName to "staffdata$"
    mount volume "smb://servername/" & volumeName
    tell application "Finder"
        if not (exists alias file volumeName of home) then
            make new alias file to disk volumeName at home
            set name of result to volumeName
        end if
    end tell
on error e
    display dialog e
end try