Skip to content

Commit

Permalink
feat: added advanced mdfind filtering options (oliverschwendener#1235)
Browse files Browse the repository at this point in the history
* Added advanced mdfind filtering options

* Fixed test
  • Loading branch information
oliverschwendener authored Oct 23, 2024
1 parent c66407a commit 69cb030
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ describe(ApplicationSearch, () => {
getExtensionSettingKey("ApplicationSearch", "windowsFileExtensions"),
getExtensionSettingKey("ApplicationSearch", "includeWindowsStoreApps"),
getExtensionSettingKey("ApplicationSearch", "macOsFolders"),
getExtensionSettingKey("ApplicationSearch", "mdfindFilterOption"),
]);
});
});
3 changes: 3 additions & 0 deletions src/main/Extensions/ApplicationSearch/ApplicationSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class ApplicationSearch implements Extension {
getExtensionSettingKey("ApplicationSearch", "windowsFileExtensions"),
getExtensionSettingKey("ApplicationSearch", "includeWindowsStoreApps"),
getExtensionSettingKey("ApplicationSearch", "macOsFolders"),
getExtensionSettingKey("ApplicationSearch", "mdfindFilterOption"),
];
}

Expand All @@ -69,13 +70,15 @@ export class ApplicationSearch implements Extension {
openApplication: "Open application",
openApplicationAsAdministrator: "Open application as administrator",
copyFilePathToClipboard: "Copy file path to clipboard",
advanced: "Advanced",
},
"de-CH": {
extensionName: "Anwendungssuche",
searchResultItemDescription: "Anwendung",
openApplication: "Anwendung starten",
openApplicationAsAdministrator: "Anwendung als Administrator starten",
copyFilePathToClipboard: "Dateipfad in Zwischenablage kopieren",
advanced: "Erweitert",
},
};
}
Expand Down
1 change: 1 addition & 0 deletions src/main/Extensions/ApplicationSearch/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class Settings {
"/Applications",
join(this.app.getPath("home"), "Applications"),
],
mdfindFilterOption: "kMDItemKind=='Application'",
includeWindowsStoreApps: true,
windowsFileExtensions: ["lnk"],
windowsFolders: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class MacOsApplicationRepository implements ApplicationRepository {
}

private async getAllFilePaths(): Promise<string[]> {
return (await this.commandlineUtility.executeCommand(`mdfind "kMDItemKind == 'Application'"`))
return (await this.commandlineUtility.executeCommand(`mdfind "${this.getMdFindFilterOption()}"`))
.split("\n")
.map((filePath) => normalize(filePath).trim())
.filter((filePath) => filePath.endsWith(".app"))
Expand All @@ -43,6 +43,20 @@ export class MacOsApplicationRepository implements ApplicationRepository {
.filter((filePath) => ![".", ".."].includes(filePath));
}

private getMdFindFilterOption() {
const filterOptions: Record<string, string> = {
"kind:application": "kind:application",
"kMDItemKind=='Application'": "kMDItemKind == 'Application'",
"kMDItemContentType=='com.apple.application-bundle'":
"kMDItemContentType == 'com.apple.application-bundle'",
};

return (
filterOptions[this.settings.getValue<string>("mdfindFilterOption")] ??
filterOptions[<string>this.settings.getDefaultValue("mdfindFilterOption")]
);
}

private filterFilePathByConfiguredFolders(filePath: string): boolean {
return this.settings.getValue<string[]>("macOsFolders").some((folderPath) => filePath.startsWith(folderPath));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useContextBridge, useExtensionSetting } from "@Core/Hooks";
import { Setting } from "@Core/Settings/Setting";
import { SettingGroup } from "@Core/Settings/SettingGroup";
import { SettingGroupList } from "@Core/Settings/SettingGroupList";
import { Button, Input, Tooltip } from "@fluentui/react-components";
import { Button, Dropdown, Input, Option, Tooltip } from "@fluentui/react-components";
import { AddRegular, DismissRegular, FolderRegular } from "@fluentui/react-icons";
import { useState } from "react";
import { useTranslation } from "react-i18next";

export const MacOsSettings = () => {
const { contextBridge } = useContextBridge();
Expand All @@ -12,11 +14,18 @@ export const MacOsSettings = () => {

const [newFolder, setNewFolder] = useState<string>("");

const { t } = useTranslation(`extension[${extensionId}]`);

const { value: folders, updateValue: setFolders } = useExtensionSetting<string[]>({
extensionId,
key: "macOsFolders",
});

const { value: mdfindFilterOption, updateValue: updateMdfindFilterOption } = useExtensionSetting<string>({
extensionId,
key: "mdfindFilterOption",
});

const removeFolder = async (indexToRemove: number) => {
await setFolders(folders.filter((_, index) => index !== indexToRemove));
};
Expand Down Expand Up @@ -82,6 +91,29 @@ export const MacOsSettings = () => {
/>
</div>
</SettingGroup>
<SettingGroup title={t("advanced")}>
<Setting
label="mdfind filter"
control={
<Dropdown
style={{ width: "100%" }}
selectedOptions={[mdfindFilterOption]}
value={mdfindFilterOption}
onOptionSelect={(_, { optionValue }) => {
if (optionValue) {
updateMdfindFilterOption(optionValue);
}
}}
>
<Option value="kind:application">kind:application</Option>
<Option value="kMDItemKind=='Application'">kMDItemKind=='Application'</Option>
<Option value="kMDItemContentType=='com.apple.application-bundle'">
kMDItemContentType=='com.apple.application-bundle'
</Option>
</Dropdown>
}
/>
</SettingGroup>
</SettingGroupList>
);
};

0 comments on commit 69cb030

Please sign in to comment.