Last active
August 31, 2018 06:22
-
-
Save doraTeX/bcae413d54c92613596c to your computer and use it in GitHub Desktop.
指定したディレクトリ内の全PDFファイルのページ数をカウントするSwiftスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/swift | |
import Cocoa | |
import Quartz | |
var openPanel = NSOpenPanel() | |
openPanel.canChooseDirectories = true | |
openPanel.canChooseFiles = false | |
let returnCode = openPanel.runModal() | |
if returnCode == .OK { | |
let directory = openPanel.urls.first!.path | |
let contents = try! FileManager.default.contentsOfDirectory(atPath: directory) | |
var sum = 0 | |
for fileName in contents { | |
let fullPath = (directory as NSString).appendingPathComponent(fileName) | |
if (fullPath as NSString).pathExtension.lowercased() == "pdf" { | |
if let doc = PDFDocument(url: URL(fileURLWithPath:fullPath)) { | |
let count = doc.pageCount | |
print("\(fileName) : \(count) page" + (count>1 ? "s":"")) | |
sum += count | |
} | |
} | |
} | |
print("Total: \(sum) page" + (sum>1 ? "s":"")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment