https://github.com/dagronf/dsffinderlabels
macOS Finder tags and labels support library for Swift and Objective-C
https://github.com/dagronf/dsffinderlabels
color finder labels macos objective-c swift tags
Last synced: 2 months ago
JSON representation
macOS Finder tags and labels support library for Swift and Objective-C
- Host: GitHub
- URL: https://github.com/dagronf/dsffinderlabels
- Owner: dagronf
- License: mit
- Created: 2019-02-09T00:12:19.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-07-26T02:11:40.000Z (about 3 years ago)
- Last Synced: 2025-04-09T01:41:20.268Z (6 months ago)
- Topics: color, finder, labels, macos, objective-c, swift, tags
- Language: Swift
- Homepage:
- Size: 117 KB
- Stars: 11
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Swift/ObjC Finder Tag/Label/Color class
Class to retrieve and update Finder tags and colors for file URLs.
## Why?
It seems quite difficult to update string and color tags for a particular url so that they appear in the Finder, and can be searched by Spotlight
* The built-in API for colors only lets you assign a single color. The Finder allows you to set multiple colors.
* Colors seem to be internally managed as String labels. For example, if you set a string tag called "Blue" (in English) then the finder item will have the internal "Blue" color set.
* When in other languages (ie. you set up your computer in German), the localized names for the colors are difficult to mapThis class (`DSFFinderLabels`) wraps these API calls to make a consistent method for updating Finder tags and colors
## Features
* Load all tags and colors for a url
* Handles localization issues
* Set multiple colors/tags all with a single call
* Simple convenience UI elements for display## Simple install
### Minimal file install
Add `DSFFinderLabels.swift` to your project.
Additionally, if you need Objective-C support also add `DSFFinderLabels+objc.swift`
### Cocoapods
Add`pod 'DSFFinderLabels', :git => 'https://github.com/dagronf/DSFFinderLabels'`
to your Podfile## Simple usage
There are some tests which show the usage as well
### Set tags and colors for some files
#### Swift
```swift
let labels = DSFFinderLabels()// Add some colors
labels.set(colors: [.blue, .green])
labels += .green// Add a tag
labels.set(tags: ["Work Related"])
labels += "Client"// And update some files label(s) and color(s)
do {
let url = URL(string: "file:///Users/blah/Desktop/file.txt")!
let url2 = URL(string: "file:///Users/blah/Desktop/file2.txt")!
let url3 = URL(string: "file:///Users/blah/Desktop/file3.txt")!try labels.update([url, url2, url3])
}
catch {
print(error)
}
```#### Objective-C
```objective-c
NSURL* fileUrl = ...
DSFFinderLabels* labels = [fileUrl finderLabels];
NSSet* tags = [labels getTags];
NSSet* colorValues = [labels getColorValues];[labels addColorWithIndex:DSFFinderLabelsColorIndexBlue];
...
NSError* error = nil;
[fileUrl setFinderLabelsWithFinderLabels:labels error:&error];
```### Add a color and tag to a file
```swift
let url = URL(string: "file:///Users/blah/Desktop/file.txt")!let labels = url.finderLabels()
// Add a new color
labels.colors.insert(.red)// Add a new tag
labels.tags.insert("Completed")// And update the file and some others with the new label(s) and color(s)
do {
try url.setFinderLabels(labels)
}
catch {
print(error)
}
```### Retrieve finder standard colors and their indexes
```swift
let colors = DSFFinderLabels.FinderColors// colors.colors[0] = { .none, "None", }
// colors.colors[1] = { .gray, "Gray", }
// colors.colors[2] = { .green, "Green", }
// ...
```## Demo screenshot

## License
```
MIT LicenseCopyright (c) 2019 Darren Ford
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```