2016-07-24 174 views
1

在这里很少有AppleScript用户,所以这可能是非常基本的东西,但我似乎无法制作一个非常简单的脚本来创建新的别名文件工作。这是脚本的全部内容:使别名脚本失败

set source_file to "/path/to/test.txt" 
set alias_file to "/path/to/test.txt alias" 
tell application "Finder" to make new alias at alias_file to source_file 

我试过它,没有“新”。我用文件名前面的“POSIX文件”和文件名后面的“作为POSIX文件”作为强制命令来试用它。我试着用“at * to *”和“to * at *”。以防万一目的地需要成为一个包含的文件夹我已经尝试过了。绝对所有的变化产生相同的错误信息:

execution error: Finder got an error: AppleEvent handler failed. (-10000) 

这并没有告诉我很多。

我明显地用“/ path/to /”替换了实际的文件路径,但我可以保证ls /path/to/test.txt确认源路径有效,并且ls "/path/to/test.txt alias"确认目标路径不存在。

万一它很重要,我运行Mac OS X 10.11.5。为make的Finder.sdef进入肯定看起来像它应该做我想做的:

make v : Make a new element make 

new type : the class of the new element 

at location specifier : the location at which to insert the element 

[to specifier] : when creating an alias file, the original 
    item to create an alias to or when creating a file viewer window, 
    the target of the window 

[with properties record] : the initial values for the properties of 
    the element → specifier : to the new object(s) 

我真正想要做的是用命令行osascript和我真的,真的要执行这个要做的是从Python调用osascript单线程,所以文件路径将是内联的,而不是变量。但是我首先移动到命令行,然后移动到脚本编辑器,因为我无法使其工作,并且每个调用此代码片段的方法都会产生相同的错误消息。所以希望当/如果我得到一个脚本的工作,我将能够从Python的osascript调用等效的代码。 :}

回答

2

你肯定是在正确的轨道上使用POSIX file因为AppleScript represents file paths differently than POSIX does(本质上,冒号而不是正斜杠)。

您可以手动将所有路径转换为AppleScript路径,但为了保持文件路径的可读性(并且让读者知道它们确实是文件的源代码),我认为将它们转换为更好的解决方案路径)。

但是,POSIX file的问题在于它返回文件引用,而不是make new alias命令正在查找的文本路径。为了解决这个问题,所有你需要做的就是投返回的文件引用作为text使alias开心:

set source_file to (POSIX file "/path/to/test.txt") as text 
set alias_file to (POSIX file "/path/to/test.txt alias") as text 
tell application "Finder" to make new alias at alias_file to source_file 

还有一个问题,虽然:在make new alias at x to y命令期望y是一个文件和x路径成为别名应该放置的目录的路径,但是您要将文件的路径传递给xy。目标(“at”)路径应该是/path/to/ - make new alias命令将自动命名别名{original filename} alias。所以,总结:

set source_file to (POSIX file "/path/to/test.txt") as text 
set alias_dir to (POSIX file "/path/to/") as text 
tell application "Finder" to make new alias at alias_dir to source_file 

这可能有点冗长,但我希望它有帮助!

+1

它非常有帮助。解决方法和原因。谢谢! – larryy

+0

@larryy太棒了!很高兴我能帮上忙。 – AstroCB

+1

更正:1.冒号分隔的HFS路径是旧的OSX之前的宿醉。AS支持那些向后兼容(和AS AS开发者是懒惰的),并且还支持标准的POSIX路径。 2. Finder期望_object说明符_(例如home文件夹中的“文件”文件的'file“test.txt文件),但会在大部分用例中接受HFS路径字符串(Finder也是旧的OSX之前的宿醉)或”别名“ /'POSIX文件'说明符并转换它们; 'get'和'set'是例外。 3. HFS路径基本上存在缺陷 - 它们无法区分名称相同的卷 - 因此请尽可能使用POSIX路径。 – foo