Skip to content

Commit c6407ad

Browse files
committed
Ini
0 parents  commit c6407ad

File tree

10 files changed

+670
-0
lines changed

10 files changed

+670
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) Chris Spiegl <chris@chrisspiegl.com> (https://ChrisSpiegl.com)
4+
5+
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:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
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.

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# alfred-rename
2+
3+
> [Alfred](https://alfredapp.com) workflow to rename files and folders via your clipboard
4+
5+
<img src="media/screenshot.png" style="width: 100%;">
6+
7+
Add prefix, postfix, and replace `{replace}` strings inside of file and folder names easily.
8+
9+
## Install
10+
11+
```bash
12+
npm install --global alfred-rename
13+
```
14+
15+
*Requires [Node.js](https://nodejs.org) 14+ and the Alfred [Powerpack](https://alfredapp.com/powerpack/).*
16+
17+
## Usage
18+
19+
*Careful: the file rename actions can not be reversed!*
20+
21+
If you want to test if you like the result of the workflow, duplicate the files and folders!
22+
23+
* Copy a simple text (one line only) into the clipboard
24+
* Invoke the Alfred File Browser on one or multiple files / folders
25+
* Select `Rename File(s) via Clipboard` action and hit <kbd>Enter</kbd>
26+
* You will be presented with multiple options to place your clipboard content
27+
* When hitting <kbd>Enter</kbd> you will copy the new file names into clipboard
28+
* This is for the purpose of previewing the change.
29+
* To then actually do the change, you will have to copy your initial text element again…
30+
* To actually make the rename happen, hold down <kbd>Command</kbd> and hit <kbd>Enter</kbd>
31+
* The files will be renamed based on your selection
32+
33+
### Replace Variable
34+
35+
If you are using a lot of template files and you would like to place a certain text element into them, you can do the following:
36+
37+
* Create your files
38+
* Put `{replace}` into the filename at the place where you want the other text to go
39+
* Now copy something into your clipboard (like a project name)
40+
* Select the files, and run the `Rename File(s) via Clipboard` action
41+
* Then select the `Replace Variable with Clipboard` action
42+
* Hold down <kbd>Command</kbd> and hit <kbd>Enter</kbd> to replace the variable in the selected files
43+
44+
## Related
45+
46+
* [More Alfred Workflows](https://github.com/chrisspiegl/alfred-workflows) - My Alfred Workflow Directory
47+
* [alfy](https://github.com/sindresorhus/alfy) - Create Alfred workflows with ease

index.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import process from 'node:process';
2+
import path from 'node:path';
3+
import alfy from 'alfy';
4+
import pupa from 'pupa';
5+
import {findFilter, isFileAction, slugifyLine} from './src/utils.js';
6+
7+
const {inputWithoutFilter, foundFilter: filter} = findFilter(alfy.input);
8+
const input = inputWithoutFilter.replaceAll('\t', '\n');
9+
const clipboard = process.env.text_to_add.trim();
10+
11+
const options = [
12+
{
13+
prefix: 'Prefix with Clipboard',
14+
action: (line, text) => {
15+
let slug = slugifyLine(text);
16+
slug = (slug) ? slug + '-' : '';
17+
return `${slug}${line}`;
18+
},
19+
},
20+
{
21+
prefix: 'Postfix with Clipboard',
22+
action: (line, text) => {
23+
const {name} = path.parse(line); //=> "hello"
24+
const {ext} = path.parse(line); //=> ".html"
25+
let slug = slugifyLine(text);
26+
slug = (slug) ? '-' + slug : '';
27+
return `${name}${slug}${ext}`;
28+
},
29+
},
30+
{
31+
prefix: 'Replace Variable with Clipboard',
32+
action: (line, text) => pupa(line, {replace: text}),
33+
},
34+
{
35+
prefix: 'Remove Clipboard Content from Filename',
36+
action: (line, text) => line.replace(text, ''),
37+
},
38+
];
39+
40+
function run(input) {
41+
if (isFileAction(input)) {
42+
return options.map(option => {
43+
const files = input.split('\n').map(filepath => {
44+
filepath = path.resolve(filepath);
45+
const filenameBefore = path.basename(filepath);
46+
const filenameAfter = option.action(filenameBefore, clipboard);
47+
const filepathBefore = filepath;
48+
const filepathAfter = path.resolve(path.dirname(filepath), filenameAfter);
49+
return {
50+
filenameBefore,
51+
filenameAfter,
52+
filepathBefore,
53+
filepathAfter,
54+
};
55+
}).filter(element => element.filenameBefore !== element.filenameAfter);
56+
return files.length > 0
57+
? {
58+
title: `${option.prefix}: ${files[0].filenameAfter}`,
59+
subtitle: 'Copy to clipboard… Hold CMD Key to actually rename files.',
60+
match: option.prefix,
61+
valid: true,
62+
arg: files.map(element => element.filenameAfter),
63+
64+
variables: {
65+
action: 'copy',
66+
},
67+
mods: {
68+
cmd: {
69+
subtitle: 'Actually rename files!',
70+
arg: JSON.stringify(files),
71+
variables: {
72+
action: 'rename',
73+
},
74+
},
75+
},
76+
}
77+
: false;
78+
}).filter(element => Boolean(element));
79+
}
80+
81+
return [
82+
{
83+
title: 'Input must be valid and existing file(s) or folder(s)',
84+
valid: false,
85+
},
86+
];
87+
}
88+
89+
function filterOutput(filter, output) {
90+
const filterSplit = filter.split(' ');
91+
for (const filter of filterSplit) {
92+
output = alfy.matches(filter, output, 'match');
93+
}
94+
95+
return output;
96+
}
97+
98+
const output = run(input);
99+
if (output.length > 0) {
100+
alfy.output(filter ? filterOutput(filter, output) : output);
101+
} else {
102+
alfy.output([{
103+
title: 'Nothing to process…',
104+
subtitle: 'Maybe the clipboard is empty?',
105+
}]);
106+
}

0 commit comments

Comments
 (0)