Skip to content

Commit 05b5e7f

Browse files
committed
[feat]: overwrite option
1 parent b2e6565 commit 05b5e7f

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

Sources/ScriptToolkit/FileExtension.swift

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public extension File {
2323
////////////////////////////////////////////////////////////////////////////////
2424
// MARK: - Duplication
2525

26-
@discardableResult public func createDuplicate(withName newName: String, keepExtension: Bool = true) throws -> File {
26+
@discardableResult public func createDuplicate(withName newName: String, keepExtension: Bool = true, overwrite: Bool = true) throws -> File? {
27+
if !overwrite && FileManager.default.fileExists(atPath: newName) { return nil }
28+
2729
guard let parent = parent else {
2830
throw OperationError.renameFailed(self)
2931
}
@@ -85,43 +87,58 @@ public extension File {
8587
////////////////////////////////////////////////////////////////////////////////
8688
// MARK: - Resize image
8789

88-
public func resizeImage(newName: String, size: CGSize) {
90+
@discardableResult public func resizeImage(newName: String, size: CGSize, overwrite: Bool = true) throws -> File? {
91+
if !overwrite && FileManager.default.fileExists(atPath: newName) { return nil }
8992
run("/usr/local/bin/convert", path, "-resize", "\(size.width)x\(size.height)",newName)
93+
return try File(path: newName)
9094
}
9195

92-
public func resizeAt123x(width: Int, height: Int, outputDir: Folder) throws {
96+
public func resizeAt123x(width: Int, height: Int, outputDir: Folder, overwrite: Bool = true) throws {
9397
print(name)
9498

9599
let res1name = outputDir.path.appendingPathComponent(path: name)
96-
resizeImage(newName: res1name, size: CGSize(width: width, height: height))
100+
try resizeImage(newName: res1name, size: CGSize(width: width, height: height), overwrite: overwrite)
97101

98102
let res2name = outputDir.path.appendingPathComponent(path: nameExcludingExtension + "@2x." + (self.extension ?? ""))
99-
resizeImage(newName: res2name, size: CGSize(width: 2 * width, height: 2 * height))
103+
try resizeImage(newName: res2name, size: CGSize(width: 2 * width, height: 2 * height), overwrite: overwrite)
100104

101105
let res3name = outputDir.path.appendingPathComponent(path: nameExcludingExtension + "@3x." + (self.extension ?? ""))
102-
resizeImage(newName: res3name, size: CGSize(width: 3 * width, height: 3 * height))
106+
try resizeImage(newName: res3name, size: CGSize(width: 3 * width, height: 3 * height), overwrite: overwrite)
103107
}
104108

105109
////////////////////////////////////////////////////////////////////////////////
106110
// MARK: - Audio Processing
107111

108-
@discardableResult public func slowDownAudio(newName: String, percent: Float) throws -> File {
112+
@discardableResult public func slowDownAudio(newName: String, percent: Float, overwrite: Bool = true) throws -> File? {
113+
if !overwrite && FileManager.default.fileExists(atPath: newName) { return nil }
109114
run(ScriptToolkit.soxPath, path, newName, "tempo", "-s", String(percent))
110115
return try File(path: newName)
111116
}
112117

113-
@discardableResult public func convertToWav(newName: String) throws -> File {
118+
@discardableResult public func convertToWav(newName: String, overwrite: Bool = true) throws -> File? {
119+
if !overwrite && FileManager.default.fileExists(atPath: newName) { return nil }
114120
run(ScriptToolkit.ffmpegPath, "-i", path, "-sample_rate", "44100", newName.deletingPathExtension + ".wav")
115121
return try File(path: newName)
116122
}
117123

118-
@discardableResult public func convertToM4A(newName: String) throws -> File {
124+
@discardableResult public func convertToM4A(newName: String, overwrite: Bool = true) throws -> File? {
125+
if !overwrite && FileManager.default.fileExists(atPath: newName) { return nil }
119126
run(ScriptToolkit.ffmpegPath, "-i", path, "-sample_rate", "44100", newName.deletingPathExtension + ".m4a")
120127
return try File(path: newName)
121128
}
122129

123-
@discardableResult public func addSilence(newName: String) throws -> File {
130+
@discardableResult public func addSilence(newName: String, overwrite: Bool = true) throws -> File? {
131+
if !overwrite && FileManager.default.fileExists(atPath: newName) { return nil }
124132
run(ScriptToolkit.soxPath, ScriptToolkit.silenceFilePath, path, newName)
125133
return try File(path: newName)
126134
}
135+
136+
////////////////////////////////////////////////////////////////////////////////
137+
// MARK: - PDF
138+
139+
@discardableResult public func cropPDF(newName: String, insets: NSEdgeInsets, overwrite: Bool = true) throws -> File? {
140+
if !overwrite && FileManager.default.fileExists(atPath: newName) { return nil }
141+
run(ScriptToolkit.pdfCropPath, "--margins", insets.left, insets.top, insets.right, insets.bottom, path, newName)
142+
return try File(path: newName)
143+
}
127144
}

Sources/ScriptToolkit/FolderExtension.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,26 @@ public extension Folder {
4343
////////////////////////////////////////////////////////////////////////////////
4444
// MARK: - Flatten folder structure
4545

46-
public func flattenFolderStructure(outputDir: String, move: Bool) throws {
46+
public func flattenFolderStructure(outputDir: String, move: Bool, overwrite: Bool = true) throws {
4747
let outputFolder = try Folder(path: outputDir)
4848

4949
let inputFolderPath = path
5050
let index = inputFolderPath.index(inputFolderPath.startIndex, offsetBy: inputFolderPath.count)
5151

5252
try makeSubfolderSequence(recursive: true).forEach { folder in
53-
print("folder: \(folder), files: \(folder.files)")
5453
let folderPath = folder.path[index...]
55-
let folderPrefix = folderPath.replacingOccurrences(of: "/", with: " ")
54+
let folderPrefix = folderPath.replacingOccurrences(of: "/", with: " ").trimmingCharacters(in: .whitespaces)
5655

5756
for file in folder.files {
57+
let newName = folderPrefix + " " + file.name
58+
if !overwrite && FileManager.default.fileExists(atPath: newName) { continue }
59+
5860
if move {
59-
try file.rename(to: folderPrefix + " " + file.name, keepExtension: true)
61+
try file.rename(to: newName, keepExtension: true)
6062
}
6163
else {
6264
let newFile = try file.copy(to: outputFolder)
63-
try newFile.rename(to: folderPrefix + " " + file.name, keepExtension: true)
65+
try newFile.rename(to: newName, keepExtension: true)
6466
}
6567
}
6668
}

Sources/ScriptToolkit/ScriptToolkit.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ import Foundation
99
import Files
1010
import SwiftShell
1111

12+
enum ScriptError: Error {
13+
case fileExists
14+
}
15+
1216
public struct ScriptToolkit {
1317

1418
// Setup of absolute paths
19+
static let pdfCropPath = "/usr/local/bin/pdfcrop"
1520
static let ffmpegPath = "/usr/local/bin/ffmpeg"
1621
static let soxPath = "/usr/local/bin/sox"
1722
static let silenceFilePath = "/Users/dan/Documents/[Development]/[Projects]/SwiftScripts/practise/silence.wav"

0 commit comments

Comments
 (0)