Quick Action to run shell scrip fails

this is driving me crazy. I have this very simple shortcut that changes the creation date of a file. if I run it from shortcuts, it runs perfectly fine. But if I run it as a quick action from Finder it fails with exit error: 2.


Any ideas on how to set it so it runs from quick actions?


[Edited by Moderator]



MacBook Air, macOS 15.6

Posted on Aug 4, 2025 4:40 PM

Reply
Question marked as Top-ranking reply

Posted on Aug 5, 2025 10:03 AM

Quick Actions require that you select the appropriate input in the Finder, then choose the appropriate Quick Action name from the Quick Actions menu. Using that "file path" variable does not work in Zsh and what you want is a command line argument as I show in this example that does work.



This Shortcut works whether you run it from the Shortcuts panel, or as a Quick Action from a selected file. The $1 is the full UNIX path of the file in either case. No need to select Run as Administrator.


SetFile has been deprecated since early September 2014 (Xcode 6). I do not base tool automation on it because Apple is famous for quietly removing tools and features from the operating system. In this case, they have given 11 years warning.


I have Swift code here that does the same thing as the SetFile -d if you want to see and modify it.


Tested: macOS Sequoia v15.6

6 replies
Question marked as Top-ranking reply

Aug 5, 2025 10:03 AM in response to awaldraff

Quick Actions require that you select the appropriate input in the Finder, then choose the appropriate Quick Action name from the Quick Actions menu. Using that "file path" variable does not work in Zsh and what you want is a command line argument as I show in this example that does work.



This Shortcut works whether you run it from the Shortcuts panel, or as a Quick Action from a selected file. The $1 is the full UNIX path of the file in either case. No need to select Run as Administrator.


SetFile has been deprecated since early September 2014 (Xcode 6). I do not base tool automation on it because Apple is famous for quietly removing tools and features from the operating system. In this case, they have given 11 years warning.


I have Swift code here that does the same thing as the SetFile -d if you want to see and modify it.


Tested: macOS Sequoia v15.6

Aug 5, 2025 12:46 PM in response to awaldraff

As I mentioned, I tested the Shortcut that I posted previously on macOS Sequoia v15.6 without any errors and the designated creation date was applied to the selected document. Recheck your Shortcut.


I haven't had time to tweak the Swift code to be interactive like SetFile, but it does contain a function and examples of using that function that allow you to change either or both of creation and modification dates (and times).


A quick invocation after adding a file path and date/time info:

swift xSetFile.swift



/*
  xSetFile.swift

  Does what SetFile -d does and gives one the option of changing creation date,
  modification date, or both depending upon what is passed to the setFileDates
  function.

  Presently lacking interactive capability, one can extend this to that functionality
  by using CommandLine.arguments functionality.
*/

import Foundation

func setFileDates(filePath: String, creationDate: Date?, modificationDate: Date?) {
    let fileManager = FileManager.default
    var attributes: [FileAttributeKey: Any] = [:]

    if let creationDate = creationDate {
        attributes[.creationDate] = creationDate
    }

    if let modificationDate = modificationDate {
        attributes[.modificationDate] = modificationDate
    }

    do {
        try fileManager.setAttributes(attributes, ofItemAtPath: filePath)
        print("Successfully updated file dates for: \(filePath)")
    } catch {
        print("Error setting file dates for \(filePath): \(error.localizedDescription)")
    }
}

// Example usage:
let filePath = "/path/to/file" // Replace with the actual path to your file

// Set a specific creation date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
if let newCreationDate = dateFormatter.date(from: "01/01/2023 10:00:00") {
    setFileDates(filePath: filePath, creationDate: newCreationDate, modificationDate: nil)
}

// Set a specific modification date
if let newModificationDate = dateFormatter.date(from: "02/15/2024 14:30:00") {
    setFileDates(filePath: filePath, creationDate: nil, modificationDate: newModificationDate)
}

// Set both creation and modification dates
if let newCreationDate = dateFormatter.date(from: "03/20/2025 09:15:00"),
   let newModificationDate = dateFormatter.date(from: "03/20/2025 11:45:00") {
    setFileDates(filePath: filePath, creationDate: newCreationDate, modificationDate: newModificationDate)
}


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Quick Action to run shell scrip fails

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.