Created
November 20, 2014 12:31
-
-
Save norio-nomura/4ea824cc4cd02af492d6 to your computer and use it in GitHub Desktop.
指定したディレクトリ以下の名前がCachesなサブディレクトリにNSURLIsExcludedFromBackupKey属性を設定するスクリプト
This file contains hidden or 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/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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
% ./setExcludedFromBackupToCaches.swift ~/Library/Containers
な感じで。