Swiftpack.co - Package - jpmhouston/RenameCommand
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

RenameCommand

A library making it easy to make a swift command-line program for renaming files according to your own rules.

Details

This package exports a struct RenameOptions conforming to the ParsableArguments protocol from Apple's ArgumentParser. It's intended to be used with @OptionGroup() and your own ParsableCommand and provides a runRename() function you can call within your own run(), implementing all the boilerplate file system and string processing involved in a command that renames files. Your code is little more than your custom regular expressions or any such manipulation of the base filename.

runRename() takes a function argument with a inout name String (and file extension String), you provide this function which changes name as desired. This is called for every file passed on the command line, with the directory omitted and file extension separated, and the file gets renamed accordingly. Leave name unchanged (or change to empty string) to do nothing to the file.

RenameOptions defines arguments --verbose/-v, --quiet/-q, --dry-run, --try (not to mention the defaults provided by ArgumentParser, --help/-h and --generate-completion-script). The difference between --dry-run and --try are that the former fails as usual if the file arguments aren't found, the latter will allow any file argument as if they were files that existed; both show the would-be results of the rename without carrying it out.

It works well with swift-sh, also the sharplet/Regex package which RenameCommand extends with an overload of its String extension functions allowing you to more conveniently specify case insensitive. See below.

Example

With swift-sh installed, this simple Swift "script" source file "myrename" (no ".swift" extension needed) is all you need to give you a fully functional custom file renaming command:

#!/usr/bin/swift sh
import ArgumentParser // apple/swift-argument-parser
import RenameCommand // @jpmhouston
import Regex // @sharplet

struct RenameMoviesCommand: ParsableCommand {
    static let configuration = CommandConfiguration(abstract: "Renames my ripped movies from their old name format to how I prefer them now.")
    @OptionGroup() var options: RenameCommand.RenameOptions
    
    func run() throws {
        try options.runRename() { name, _ in
            name.replaceAll(matching: #"\."#, with: " ")
            name.replaceFirst(matching: " 720p", .ignoreCase, with: "")
            name.replaceFirst(matching: " 1080p", .ignoreCase, with: "")
            name.replaceFirst(matching: " ([0-9][0-9][0-9][0-9])$", with: " ($1)")
        }
    }
}

RenameMoviesCommand.main()

The functions replaceFirst and replaceAll are from sharplet/Regex. If your script uses this package too, you're also able to pass options such as .ignoreCase to those shortcut functions as shown rather than having to construct a Regex yourself to provide those options.

Thanks to the magic of swift-sh, after a chmod a+x myrename and moving it to somewhere in the shell command path like /usr/local/bin, you can then do:

$ myrename --help
OVERVIEW: Renames my ripped movies from their old name format to how I prefer them now.

USAGE: myrename [<files> ...] [--quiet] [--verbose] [--dry-run] [--try]

ARGUMENTS:
  <files>                 Files to rename. 

OPTIONS:
  -q, --quiet             Suppress non-error output. 
  -v, --verbose           Verbose output (overrides "--quiet"). 
  --dry-run               Show what would be renamed (overrides "--quiet", no files are changed).
  --try                   Try hypothetical file names (overrides "--quiet", no files are changed).
  -h, --help              Show help information.

$ myrename ~/Movies/Die.Hard.1988.720p.mp4
'Die.Hard.1988.720p.mp4' renamed to 'Die Hard (1988).mp4'

Tip for fish shell users

If you use the fish shell, add this selection function and you can rename the current Finder selection with simply this:

$ myrename (selection)

(exercise for the reader: make something similar that works in other shells)

See Also

Github

link
Stars: 0

Releases

RenameCommand 0.4.0 - 2020-12-23T07:18:50

Added file extension parameter to the rename function, added try option, minor fixes.

Added the file extension parameter in case the function needs to rename differently depending on the extension. The try option is a little like dry-run but doesn't require the file to exist. Removed Linux platform for now because it seemed to stop working as of Xcode 12. Minor fixes to file path and extension parsing that were probably wrong before. Updated the readme to include a clearer description of using swift-sh and of the Regex additions.

RenameCommand 0.2.1 - 2020-08-05T10:30:21

MIT license, new details to Package.swift

The info added to Package.swift was as suggested by Swift Package Index. The language version of Swift 5.x was somewhat arbitrary, and platform macOS 10.10 was picked because this documentation says it's the minimum for macOS and SPM. These choices might be bad for some reason, but its too late on a Tuesday evening to fully contemplate such issues. (Does swift-argument-parser strictly require Swift 5.2 or not? Can this really work on Macs running OS X 10.10 Yosemite?)

RenameCommand 0.2.0 - 2020-08-05T10:12:22

Minor improvements, expand example

The option "silent" is now "quiet" to conform with many other command line programs. Improved the replace functions from Regex I overload for easier case insensitivity, make less wordy and more capable by allowing any regex options to be passed after the pattern (breaking change). Expanded the example in the readme to demonstrate using the script.

RenameCommand 0.2.0 (botched) - 2020-06-08T00:44:59

Minor improvements, expand example

The option "silent" is now "quiet" to conform with many other command line programs. Improved the replace functions from Regex I overload for easier case insensitivity, make less wordy and more capable by allowing any regex options to be passed after the pattern (breaking change). Expanded the example in the readme to demonstrate using the script.

N.B.: tag is 0.2 instead of 0.2.0, so it isn't seen by Swift Package Index. Might fix when creating new "main" branch.

RenameCommand 0.1.2 - 2020-05-26T23:10:11

Swift Package Manager mistake corrected

Accidentally committed Package.resolved removed and added to the gitignore file

RenameCommand 0.1.1 - 2020-05-23T22:12:55

Compatibility with ArgumentParser 0.0.6

Fixed compatibility with ArgumentParser 0.0.6 and multiple arguments error on "dry-run" option. Added note to the readme about workaround for swift-sh issue #111 with recent versions of Swift Package Manager from Xcode 11.4 and later (this package will remain pre-release until that gets fixed).

RenameCommand 0.1.0 - 2020-03-20T22:47:56

First release