如何使用AppleScript将文件复制到同一文件夹中的另一个文件

时间:2014-03-19 23:17:56

标签: applescript

我有以下AppleScript:

to copyMovieTemplate(sermonCode)
    local iMovieProject
    set iMovieProject to ((iMovieSermonsPath as text) & sermonCode & ".rcproject")

    display dialog ("Copying " & (sermonTemplate) & " to " & (iMovieProject as text))

    tell application "Finder"
        duplicate file sermonTemplate to file iMovieProject without replacing
    end tell
end copyMovieTemplate

sermonTemplateiMovieSermonsPath变量为aliases

global sermonTemplate
global iMovieSermonsPath

set sermonTemplate to alias ((iMovieSermonsPath as text) & "Sunday Service Template.rcproject")
set iMovieSermonsPath to alias ((iMovieProjects as text) & "Sermons:")

当我运行此脚本时,我收到一条错误,告诉我Finder无法将目标文件复制到目标文件中:

  

Finder收到错误:无法设置文件“Macintosh HD:用户:vitabile:电影:iMovie Projects.localized:Sermons:14-0101-01.rcproject”来文件“Macintosh HD:用户:vitabile:电影: iMovie Projects.localized:Sermons:14-0101-01.rcproject“。

我做错了什么?这份副本的正确方法是什么?

修改1

我甚至试图删除without replacing条款,但没有区别。

修改2

经过一些搜索后,我发现了this post,加上评论中的反馈,所以我重写了我的代码:

-- Copy the Sunday Sermon Template file to the iMove Projects folder
to copyMovieTemplate(sermonCode)
    -- Convert the sermonCode into a file name for the iMovie project
    set iMovieProject to sermonCode & ".rcproject"

    display dialog ("Copying " & (sermonTemplate as text) & " to " & iMovieProject as text)

    tell application "Finder"
        try
            set theCopy to duplicate alias sermonTemplate to folder alias iMovieSermonsPath
            set name of theCopy to iMovieProject
        on error errStr
            display dialog ("Unable to copy the Sunday Sermon Template project to " & (iMovieSermonsPath as text) & iMovieProject & ".")
            display dialog "Error: " & errStr
            error number -128
        end try
    end tell
end copyMovieTemplate

这仍然无效,但错误已更改:

  

Finder收到错误:无法将别名“Macintosh HD:用户:vitabile:Movies:iMovie Projects.localized:Sermons:”转换为整数类型。

使用系统事件不起作用,因为我收到同样的错误。

2 个答案:

答案 0 :(得分:0)

关于set iMovieSermonsPath to alias (iMovieProjects as text) & "Sermons:":您在上一个问题(what is the correct path to the iMovie Projects folder to use in an AppleScript)中犯了同样的错误:

代码不是创建单个别名,而是无意中创建了一个列表,其第一个条目是路径为iMovieProject as text的文件夹的别名,其第二个元素是字符串文字& #34;布道:"

要解决此问题,会将构建路径字符串的整个表达式括在括号中,然后再将其传递给alias

set iMovieSermonsPath to alias ((iMovieProjects as text) & "Sermons:")

答案 1 :(得分:0)

我终于找到了我的问题。我为iMovie Projects文件夹构建的路径最后没有包含冒号。也就是说,我正在构建一条看起来像这样的路径:

  

Macintosh HD:用户::电影:iMovie Projects.localizedSermons:

应该看起来像:

  

Macintosh HD:用户::电影:iMovie Projects.localized:Sermons:

一旦我添加了缺失的冒号,它就开始起作用了。

我也摆脱了别名,只保留了text。我不知道这是否有所作为。