【问题标题】:How to copy a file to another file in the same folder using AppleScript如何使用 AppleScript 将文件复制到同一文件夹中的另一个文件
【发布时间】:2014-03-19 23:17:56
【问题描述】:

我有以下 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:Users:vitabile:Movies:iMovie Projects.localized:Sermons:14-0101-01.rcproject”设置为文件“Macintosh HD:Users:vitabile:Movies: 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:Users:vitabile:Movies:iMovie Projects.localized:Sermons:”转换为整数类型。

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

【问题讨论】:

  • 查看我对 OP 的第二次编辑。

标签: applescript


【解决方案1】:

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

您的代码没有创建单个别名,而是无意中创建了一个 list,其第一个条目是路径为 iMovieProject as text 的文件夹的别名,其第二个元素是字符串文字“Sermons: "

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

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

【讨论】:

  • 这是一个转录错误。单词alias 之后的整个表达式用括号括起来。我将编辑原始问题以反映事实。我仍然收到问题中提到的错误。
  • @TonyVitabile:知道了。在您的第二个 sn-p 中,您根据 iMovieSermonsPath 定义 sermonTemplate,即使您只是在此之后定义 iMovieSermonsPath
【解决方案2】:

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

Macintosh HD:Users::Movies:iMovie Projects.localizedSermons:

什么时候应该是这样的:

Macintosh HD:Users::Movies:iMovie Projects.localized:Sermons:

一旦我添加了缺少的冒号,它就开始工作了。

我还去掉了别名,将所有内容保留为 text。我不知道这是否会有所不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多