Skip to content

Commit

Permalink
Update custom-folder extension (raycast#14235)
Browse files Browse the repository at this point in the history
- Merge branch \'contributions/merge-1724887250511210000\'
- Pull contributions
- chore: fix lint
- Merge pull request raycast#3 from Ph-lo/fix-first-icon-swap
- chore: changelog
- Merge pull request raycast#2 from Ph-lo/fix-first-icon-swap
- fix: first icon swap refresh, handling path with space & sanitizing handled by quoting
  • Loading branch information
Ph-lo authored and tisfeng committed Sep 13, 2024
1 parent cee11f8 commit d0ee3f4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
4 changes: 4 additions & 0 deletions extensions/custom-folder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Custom folder Changelog

## [Fix First icon swap] - 2024-08-28
- Fixed the issue with the first icon swap.
- Now handles spaces in paths.

## [Feat Easily apply the custom icon] - 2024-08-25
- Added the ability to easily apply the new custom icon to a target folder.

Expand Down
5 changes: 2 additions & 3 deletions extensions/custom-folder/src/result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useEffect, useState } from "react";
import { base64ToFile } from "./utils/saveFile";
import path from "path";
import { ReplaceAction } from "./replaceAction";
import { sanitizePngFilePath } from "./utils/verifications";

export default function Result({ formValues }: { formValues: FolderForm }) {
const [isLoading, setIsLoading] = useState<boolean>(false);
Expand Down Expand Up @@ -39,7 +38,7 @@ export default function Result({ formValues }: { formValues: FolderForm }) {
});
return;
}
base64ToFile(imageResult?.baseImage, sanitizePngFilePath(output))?.then(async (res) => {
base64ToFile(imageResult?.baseImage, output)?.then(async (res) => {
if (res === "success") {
await showToast({
style: Toast.Style.Success,
Expand Down Expand Up @@ -98,7 +97,7 @@ export default function Result({ formValues }: { formValues: FolderForm }) {
<ActionPanel>
{formValues.targetFolderPath && formValues.targetFolderPath.length > 0 && (
<ReplaceAction
iconPath={sanitizePngFilePath(`${tmpDirectory}/${imageResult?.name}`)}
iconPath={`${tmpDirectory}/${imageResult?.name}`}
targetFolderPath={formValues?.targetFolderPath?.[0]}
onAction={() => saveFile(`${tmpDirectory}/${imageResult?.name}`, "applied")}
/>
Expand Down
44 changes: 18 additions & 26 deletions extensions/custom-folder/src/utils/replaceIconScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,41 @@ import { checkPathIsDirectory, sanitizeDirectoryPath } from "./verifications";

export const replaceIconScript = (sanitizedIconPath: string, targetPath: string) => {
const sanitizedTargetPath = sanitizeDirectoryPath(targetPath);
const isDirValid = checkPathIsDirectory(sanitizedTargetPath);
const isDirValid = checkPathIsDirectory(targetPath);

if (isDirValid) {
return `
bash -c
#!/bin/sh
# Sets a custom icon to a directory
# Usage setIcon.sh /path/to/iconImage /path/to/targetFolder
iconSource=${sanitizedIconPath}
iconDestination=${sanitizedTargetPath}
icon=/tmp/$(basename $iconSource)
rsrc=/tmp/icon.rsrc
echo $icon
echo $iconSource
iconDestination="${sanitizedTargetPath}"
iconSource="${sanitizedIconPath}"
icon="/tmp/$(basename "$iconSource")"
rsrc="/tmp/icon.rsrc"
# Create icon from the iconSource
cp $iconSource $icon
cp "$iconSource" "$icon"
# Add icon to image file, meaning use itself as the icon
sips -i $icon
sips -i "$icon"
if [ -d $iconDestination ]; then
# Destination is a directory
# If the Icon? file already exists - delete it by updating the item via applescript, a simple rm wouldn't trigger a render refresh
if [ -f $iconDestination/Icon? ]; then
rm $iconDestination/Icon?
osascript -e 'tell application "Finder" to update item POSIX file "'"$iconDestination"'"'
if [ -d "$iconDestination" ]; then
if [ -f "$iconDestination"/$'Icon\r' ]; then
rm "$iconDestination"/$'Icon\r'
fi
osascript -e 'tell application "Finder" to update item POSIX file "'"$iconDestination"'"'
# Take that icon and put it into a rsrc file
DeRez -only icns $icon >$rsrc
DeRez -only icns "$icon" >"$rsrc"
# Apply the rsrc file to
SetFile -a C $iconDestination
# Apply the rsrc file to the destination
SetFile -a C "$iconDestination"
touch $iconDestination/$'Icon\r'
Rez -append $rsrc -o $iconDestination/Icon?
SetFile -a V $iconDestination/Icon?
touch "$iconDestination"/$'Icon\r'
Rez -append "$rsrc" -o "$iconDestination"/$'Icon\r'
SetFile -a V "$iconDestination"/$'Icon\r'
fi
echo $iconDestination
rm $rsrc $icon
rm "$rsrc" "$icon"
`;
}
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion extensions/custom-folder/src/utils/verifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ export const sanitizePngFilePath = (filePath: string): string => {
};

export const sanitizeDirectoryPath = (directoryPath: string): string => {
return directoryPath.replace(/[^a-zA-Z0-9/_-]/g, "");
return directoryPath.replace(/[^a-zA-Z0-9/_ -]/g, "");
};

0 comments on commit d0ee3f4

Please sign in to comment.