Skip to content

Instantly share code, notes, and snippets.

@doraTeX
Last active August 31, 2018 06:22
Show Gist options
  • Save doraTeX/bcae413d54c92613596c to your computer and use it in GitHub Desktop.
Save doraTeX/bcae413d54c92613596c to your computer and use it in GitHub Desktop.
指定したディレクトリ内の全PDFファイルのページ数をカウントするSwiftスクリプト
#!/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