Skip to content

Instantly share code, notes, and snippets.

@norio-nomura
Created November 20, 2014 12:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norio-nomura/4ea824cc4cd02af492d6 to your computer and use it in GitHub Desktop.
Save norio-nomura/4ea824cc4cd02af492d6 to your computer and use it in GitHub Desktop.
指定したディレクトリ以下の名前がCachesなサブディレクトリにNSURLIsExcludedFromBackupKey属性を設定するスクリプト
#!/usr/bin/env xcrun swift
import Cocoa
extension NSURL {
var isDirectory: Bool {
return resourceValue(NSURLIsDirectoryKey)
}
var isExcludedFromBackup: Bool {
return resourceValue(NSURLIsExcludedFromBackupKey)
}
func resourceValue(resourceKey: NSString) -> Bool {
var resourceValue: AnyObject?
var error: NSError?
var result = false
if getResourceValue(&resourceValue, forKey: resourceKey, error: &error) {
if let number = resourceValue as? NSNumber {
result = number.boolValue
} else {
println("error: \(self) for key: \(resourceKey)")
}
} else {
println("error: \(error?.localizedDescription) on \(self) for key: \(resourceKey)")
}
return result
}
func setResourceValue(value: NSNumber , forResourceKey resourceKey: NSString) {
var error: NSError?
var result = false
if !setResourceValue(value, forKey: resourceKey, error: &error) {
println("error: \(error?.localizedDescription) on \(self) for key: \(resourceKey)")
}
}
}
func setExcludedFromBackupContentsToCaches(targetURL: NSURL) {
let keys = [NSURLIsDirectoryKey, NSURLIsExcludedFromBackupKey] as [AnyObject]
var error: NSError?
if let contents = NSFileManager.defaultManager().contentsOfDirectoryAtURL(targetURL, includingPropertiesForKeys: keys, options: nil, error: &error) as? [NSURL] {
for url in contents {
if url.isDirectory {
if url.lastPathComponent == "Caches" {
url.setResourceValue(NSNumber(bool: true), forResourceKey: NSURLIsExcludedFromBackupKey)
println(url.path!)
} else {
setExcludedFromBackupContentsToCaches(url)
}
}
}
} else {
println("\(targetURL.path!): \(error!.localizedDescription)")
}
}
var path = Process.arguments.count > 1 ? Process.arguments[1] : NSFileManager.defaultManager().currentDirectoryPath
if let url = NSURL(fileURLWithPath: path) {
setExcludedFromBackupContentsToCaches(url)
}
@norio-nomura
Copy link
Author

% ./setExcludedFromBackupToCaches.swift ~/Library/Containers

な感じで。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment