【问题标题】:Applescript creation dateApplescript 创建日期
【发布时间】:2026-01-15 12:50:01
【问题描述】:

我正在尝试制作执行以下操作的 Automator 服务:

  • 输入是选定的文件
  • 创建新文件夹并复制所选文件
  • 帮助* Applescript 获取创建日期/时间并过滤掉超过一分钟前制作的任何文件(一个名为 input[contains image path] 被传递给这个脚本)
  • 重命名文件
  • 调整文件大小

除了第 3 步之外,我一切正常,这很重要,因为如果该服务在同一个文件夹中再次运行,则已经调整大小的文件将再次调整大小。为了解决这个问题,我想创建一个 Applescript 过滤掉 40 秒/1 分钟前创建的任何内容。

到目前为止,我有这个,它返回一个错误:

on run {input, parameters}


set theFileDate to (creation date of input as string)
display dialog theFileDate

return input
end run

我正在尝试显示对话框,以便我可以验证代码是否正常工作并查看日期/时间的格式

【问题讨论】:

    标签: applescript automator


    【解决方案1】:

    您必须使用 Finder 的脚本字典来访问创建日期属性。

    on run {input, parameters}
        tell application "Finder"
            set theFileDate to (creation date of input as string)
        end tell
        display dialog theFileDate
        return input
    end run
    

    【讨论】:

    • 不,我在工作流程中尝试了您的脚本。它没有用。称为“输入”的参数是一个列表。你需要一个循环(见下面我的回答)。此外,将日期转换为字符串可以显示它,比较日期,您不希望将日期作为字符串,您希望使用诸如“大于”之类的关系运算符来比较某种日期或数字或“>”或类似的东西。
    • @Kaydell 我的意思是,您需要将该行包装在 tell application "Finder" 块中,以获取日期,这是他实际上在检索时遇到的唯一问题。我只是这样写它来证明它会输出一个正确的日期——我不打算为他写整个脚本。
    • 我认为您的代码无法运行。 1.输入是一个列表,所以你确实需要一个循环。 2. 要获得创建日期,我相信您需要对单个文件执行“获取信息”。您是否尝试让您的代码运行?我无法让它运行。这是我可以获取文件日期的方法:“获取(选择文件的信息)的创建日期”
    • @Kaydell input 可以是任何自动操作。如果是多个文件,是的,您需要遍历每个文件。除此之外,它确实独立运行(我的意思是不在on run 块内)。如果您不相信我,请在 AppleScript Editor 中运行它。 d.chend.me/SM5z
    • "输入可以是任何东西,如果它是一个自动操作。"好的,我意识到这一点,但是当我设置 Automator 操作来处理文件和文件夹时,我将服务限制为仅接受来自 Finder 的选定文件和文件夹,或者我将第一个操作设置为“获取选定的 Finder 项目”,然后我知道我的工作流程会得到什么。我不希望最终用户通过让他们选择任何内容,让他们说一句话,然后运行我的服务来获得神秘的错误消息。您开发的软件的每个部分都有其前置条件和后置条件。
    【解决方案2】:

    我处理了您的代码。我相信这更接近。

    -- This script will (when completed) filter the list of Finder items in the input parameter
    -- returning as an output only those files that meet the specified criteria.
    on run {input, parameters}
        -- if no items were selected, tell the user and cancel the workflow
        if ((count of input) < 1) then
            display alert "This workflow will do nothing because no items were selected in the Finder"
            set CANCEL_WORKFLOW to -128
            error CANCEL_WORKFLOW
        end if
        -- select the items to be output by this action
        set output to {}
        repeat with thisItem in input
            -- display the thisItem's path name and creation date
            display dialog (thisItem as text)
            set theFileDate to (creation date of (get info for thisItem))
            display dialog theFileDate as text
            ------ replace the next line of code with a compare of theFileDate to current date -----
            set addThisItem to true
            -----------------------------------------------------------------------------------------------
            -- add items that meet the criteria to the output (which is a list)
            if addThisItem then
                set output to output & thisItem
            end if
        end repeat
        -- return the output of this action to be the input of the next action
        return output
    end run
    

    让我知道这是怎么回事。

    -- 凯德尔
    kaydell@yahoo.com
    http://learnbymac.com

    【讨论】:

      【解决方案3】:

      或者。

      您只需在文件处理完毕后设置标签索引颜色。并过滤掉那些不是标记颜色的。

      在此示例中,我过滤掉标签索引为 Purple

      的项目

      以及颜色处理过的项目为紫色,然后在第二次运行时被忽略。

      【讨论】:

      • 我不想添加标签颜色,因为这会引起很多混乱,尽管我已经尝试过滤掉包含“- Resized”的项目,因为我已经在工作流程结束。您几乎可以只使用自动过滤器来做到这一点,但您可以通过“不包含”进行过滤,这正是我所需要的。我开始了一个新问题,因为我被困在这个新的过滤任务上。虽然现在我知道我应该把它贴在这里。对不起,我是新来的。 [链接]*.com/questions/17910790/…
      【解决方案4】:

      试试这个。请注意,您可以将“60”更改为“40”或任何其他所需的秒数。

      on run {input, parameters}
          set filterDate to (current date) - 60
          set filteredFiles to {}
      
          repeat with i from 1 to count of input
              set thisFile to item i of input
              if (class of thisFile) is text then set thisFile to thisFile as alias
              set creationDate to (creation date of (get info for thisFile))
              if creationDate is greater than or equal to filterDate then
                  set end of filteredFiles to (item i of input)
              end if
          end repeat
      
          return filteredFiles
      end run
      

      【讨论】: