Skip to content

Commit 0a7db07

Browse files
committed
feature(*) implement icon scan and iew in menu item
1 parent 60250b7 commit 0a7db07

File tree

6 files changed

+68
-19
lines changed

6 files changed

+68
-19
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1010

1111
- Refactor application.
1212

13+
## [0.0.2-alpha] - 2021-11-14
14+
15+
### Added
16+
17+
- The scanning icons for menu items from the app `Info.plist`.
18+
1319
## [0.0.1-alpha] - 2021-11-03
1420

1521
### Added

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The Quick Symlink is a `Finder extension` which provides a `contextual menu item` for the symbolic links creation on macOS.
44

5-
[![status](https://img.shields.io/badge/status-active-active?style=flat-square)](BADGES_GUIDE.md#status) [![version](https://img.shields.io/badge/version-0.0.1--alpha-informational?style=flat-square)](BADGES_GUIDE.md#version) [![oss lifecycle](https://img.shields.io/badge/oss_lifecycle-active-important?style=flat-square)](BADGES_GUIDE.md#oss-lifecycle) [![maintenance](https://img.shields.io/badge/maintenance-yes-informational?style=flat-square)](BADGES_GUIDE.md#maintenance) [![last release](https://img.shields.io/badge/last_release-November_03,_2021-informational?style=flat-square)](BADGES_GUIDE.md#release-date) [![last commit](https://img.shields.io/badge/last_commit-November_03,_2021-informational?style=flat-square)](BADGES_GUIDE.md#commit-date)
5+
[![status](https://img.shields.io/badge/status-active-active?style=flat-square)](BADGES_GUIDE.md#status) [![version](https://img.shields.io/badge/version-0.0.2--alpha-informational?style=flat-square)](BADGES_GUIDE.md#version) [![oss lifecycle](https://img.shields.io/badge/oss_lifecycle-active-important?style=flat-square)](BADGES_GUIDE.md#oss-lifecycle) [![maintenance](https://img.shields.io/badge/maintenance-yes-informational?style=flat-square)](BADGES_GUIDE.md#maintenance) [![last release](https://img.shields.io/badge/last_release-November_03,_2021-informational?style=flat-square)](BADGES_GUIDE.md#release-date) [![last commit](https://img.shields.io/badge/last_commit-November_14,_2021-informational?style=flat-square)](BADGES_GUIDE.md#commit-date)
66

77
[![license](https://img.shields.io/badge/license-MIT-informational?style=flat-square)](LICENSE) [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg?style=flat-square)](CODE_OF_CONDUCT.md)
88

empty-new-file/commons/EmptyNewFileDefaults.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public extension EmptyNewFileDefaults {
3636

3737
public enum EmptyNewFileExtension {
3838

39-
static var extensions: [String] {
39+
static var extensions: [String: String] {
40+
4041
get {
4142
return extensionsDefaults.get();
4243

@@ -47,5 +48,19 @@ public enum EmptyNewFileExtension {
4748
}
4849
}
4950

50-
private static var extensionsDefaults = EmptyNewFileDefaults(key: "extensions", defaultValue: ["js", "css", "html", "md"]);
51+
static var incs: [String: String] {
52+
53+
get {
54+
return icnsDefaults.get();
55+
56+
}
57+
set {
58+
icnsDefaults.set(newValue);
59+
60+
}
61+
}
62+
63+
private static var extensionsDefaults = EmptyNewFileDefaults(key: "extensions", defaultValue: ["js": "js", "css": "css", "html": "html", "md": "md"]);
64+
65+
private static var icnsDefaults = EmptyNewFileDefaults(key: "icns", defaultValue: ["":""]);
5166
}

empty-new-file/commons/ExtensionScanner.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ public extension Sequence where Element: Hashable {
2525

2626
public struct UTTypeConformsTo: Codable {
2727
var typeConformsTo: [String]?
28+
var iconConformsTo: String?
2829

2930
private enum CodingKeys : String, CodingKey {
3031
case typeConformsTo = "CFBundleTypeExtensions"
32+
case iconConformsTo = "CFBundleTypeIconFile"
3133
}
3234
}
3335

@@ -49,6 +51,7 @@ public class AppExtentionsScanner: ExtentionsScanner {
4951
public func scan() -> [String]! {
5052
let applicationsDirectory = FileManager.default.urls(for: .applicationDirectory, in: .localDomainMask)[0];
5153
var extentions: [String] = [];
54+
var icns: [String: String] = [:];
5255

5356
do {
5457
let apps = try FileManager.default.contentsOfDirectory(at: applicationsDirectory, includingPropertiesForKeys: nil);
@@ -66,6 +69,14 @@ public class AppExtentionsScanner: ExtentionsScanner {
6669

6770
for extensionName in exportedTypeDeclaration.typeConformsTo! {
6871
extentions.append(extensionName.lowercased())
72+
if (exportedTypeDeclaration.iconConformsTo != nil) {
73+
var iconName = exportedTypeDeclaration.iconConformsTo!;
74+
if (!iconName.contains(".icns")) {
75+
iconName.append(".icns");
76+
}
77+
print(iconName)
78+
icns.updateValue(app.appendingPathComponent("Contents").appendingPathComponent("Resources").appendingPathComponent(iconName).absoluteString, forKey: extensionName.lowercased())
79+
}
6980
}
7081
}
7182
}
@@ -78,6 +89,8 @@ public class AppExtentionsScanner: ExtentionsScanner {
7889
NSLog("ExtensionScanner.scan() failed to scan apps: %@", error.description as NSString);
7990
}
8091

92+
EmptyNewFileExtension.incs = icns;
93+
8194
return extentions;
8295
}
8396
}

empty-new-file/empty-new-file-menu/FinderSync.swift

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,38 @@ class FinderSync: FIFinderSync {
4848

4949
override func menu(for menuKind: FIMenuKind) -> NSMenu {
5050
// Produce a menu for the extension (to be shown when right clicking a folder in Finder)
51-
let quickSymlinkMenu = NSMenu(title: "");
51+
let emptyBlankFileMenu = NSMenu(title: "");
52+
emptyBlankFileMenu.setAccessibilityIndex(0)
5253

53-
var exts = EmptyNewFileExtension.extensions;
54+
let exts = EmptyNewFileExtension.extensions;
55+
let icns = EmptyNewFileExtension.incs;
5456
for ext in exts {
55-
print(ext)
57+
let item = NSMenuItem.init();
58+
item.title = "\(ext.value)";
59+
item.action = #selector(createSymlink(_:));
60+
item.target = self
5661

57-
quickSymlinkMenu.addItem(
58-
withTitle: "\(ext)",
59-
action: #selector(createSymlink(_:)),
60-
keyEquivalent: ""
61-
);
62+
if (icns["\(ext.key)"] != nil) {
63+
item.image = NSImage.init(byReferencing: URL.init(string: icns[ext.key]!)!)
64+
}
65+
66+
emptyBlankFileMenu.addItem(item);
6267
}
6368

6469

6570

66-
let quickSymLinkMainMenu = NSMenu(title: "");
67-
let quickSymlinkMenuItem = NSMenuItem(
71+
let emptyBlankFileMainMenu = NSMenu(title: "");
72+
let emptyBlankFileMenuItem = NSMenuItem(
6873
title: "New file",
6974
action: nil,
7075
keyEquivalent: ""
7176
);
72-
quickSymLinkMainMenu.setSubmenu(quickSymlinkMenu, for: quickSymlinkMenuItem);
73-
quickSymLinkMainMenu.addItem(quickSymlinkMenuItem);
74-
return quickSymLinkMainMenu;
77+
emptyBlankFileMainMenu.setSubmenu(emptyBlankFileMenu, for: emptyBlankFileMenuItem);
78+
emptyBlankFileMainMenu.addItem(emptyBlankFileMenuItem);
79+
return emptyBlankFileMainMenu;
7580
}
7681

7782
@IBAction func createSymlink(_ sender: NSMenuItem!) {
78-
print("123")
7983
guard let target = FIFinderSyncController.default().targetedURL() else {
8084

8185
NSLog("Failed to obtain targeted URL: %@")
@@ -102,5 +106,16 @@ class FinderSync: FIFinderSync {
102106
NSLog("Failed to create file: %@", error.description as NSString)
103107
}
104108
}
109+
110+
private func loadImage(filePath: String) -> NSImage? {
111+
let fileURL = URL.init(string: filePath);
112+
do {
113+
let imageData = try Data(contentsOf: fileURL!)
114+
return NSImage(data: imageData)
115+
} catch {
116+
print("Error loading image : \(error)")
117+
}
118+
return nil
119+
}
105120
}
106121

empty-new-file/empty-new-file/ViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ class ViewController: NSViewController {
4141
}
4242

4343
@IBAction func saveExtensions(_ sender: Any) {
44-
var extensions: [String] = [];
44+
var extensions: [String: String] = [:];
4545

4646
for checkbutton in self.extensionsCheckboxes {
4747
if (checkbutton.state == .on) {
48-
extensions.append(checkbutton.title);
48+
extensions.updateValue(checkbutton.title, forKey: checkbutton.title);
4949
}
5050
}
5151

0 commit comments

Comments
 (0)