与其使用 Droplet 并让用户将文件拖到 Droplet 上,不如直接制作一个安装程序,这样用户只需双击安装程序?这会更容易,也可能会避免你的问题。我还在您的代码中添加了一些错误处理,因为使用运输代码这样做是明智的。我们还告诉用户发生了什么。
注意:您的代码中也有错误。outputFolder 是一个字符串。Finder 需要一个文件说明符。要使字符串成为说明符,您可以在字符串路径前添加单词“文件”或“文件夹”。您的代码可能有效,但编写它的正确方法是使用说明符。其他应用程序可能不采用字符串路径,但它们都会采用说明符......所以养成使用它们而不是字符串的习惯。
try
-- create the output folder if necessary
set outputFolder to (path to application support folder from user domain as text) & "iWork:Pages:Templates:My Templates:"
do shell script "/bin/mkdir -p " & quoted form of POSIX path of outputFolder
-- find the templates on the dmg disk
set myPath to path to me
tell application "Finder"
set myContainer to container of myPath
set templateFiles to (files of myContainer whose name extension is "template") as alias list
end tell
-- copy the templates to the output folder
-- NOTE: the script will error if any of the templates already exist
-- therefore we use a repeat loop and duplicate each file separately with a try block
-- around it to avoid errors in case some templates have already been installed.
tell application "Finder"
repeat with aTemplate in templateFiles
try
duplicate aTemplate to folder outputFolder
end try
end repeat
end tell
-- tell the user everything was OK
tell me to activate
display dialog "The templates were successfully installed! You may now use them in Pages." buttons {"OK"} default button 1 with title "Templates Installer" with icon note
on error
tell me to activate
display dialog "There was an error installing the templates. Please manually install them by copying them to the following folder." & return & return & (POSIX path of outputFolder) buttons {"OK"} default button 1 with title "Templates Installer"
end try