Skip to content

Commit 935ca87

Browse files
committed
Init
0 parents  commit 935ca87

File tree

9 files changed

+495
-0
lines changed

9 files changed

+495
-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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# alfred-copy-names
2+
3+
> [Alfred](https://alfredapp.com) workflow to copy just the file and folder names to your clipboard
4+
5+
<img src="media/screenshot.png" style="width: 100%;">
6+
7+
Alfred itself provides a file action to copy the file and folder paths.
8+
9+
However, it does not provide an action to copy just the file and folder names.
10+
11+
This workflow provides this in a few variants.
12+
13+
* Copy File Names
14+
* Copy File Names (no extension)
15+
* Copy Folder Names
16+
* Copy all Names
17+
* Copy all Names (no file extension)
18+
19+
## Install
20+
21+
```
22+
npm install --global alfred-copy-names
23+
```
24+
25+
*Requires [Node.js](https://nodejs.org) 14+ and the Alfred [Powerpack](https://alfredapp.com/powerpack/).*
26+
27+
## Usage
28+
29+
1. Select a folder in the macOS Finder and then activate the Alfred File Browser.
30+
2. For me this is setup to be <kbd>Command</kbd><kbd>Command</kbd>.
31+
3. Then you can enter `Copy Names…` and press <kbd>Enter</kbd>.
32+
4. You will be presented with three options:
33+
1. Copy File Names
34+
2. Copy Folder Names
35+
3. Copy File and Folder Names
36+
5. You can filter the options by adding `!file` or `!folder` or `!fil ext` at the end of the Alfred input field.
37+
6. Choose the one you want and hit <kbd>Enter</kbd> and the names will be copied to your clipboard.
38+
39+
## Related
40+
41+
* [More Alfred Workflows](https://github.com/chrisspiegl/alfred-workflows) - My Alfred Workflow Directory
42+
* [alfy](https://github.com/sindresorhus/alfy) - Create Alfred workflows with ease
43+
* [Copy name [1.6]](https://www.alfredforum.com/topic/1733-file-action-16-copy-folderfile-name/) - My workflow was inspired by this workflow by [mcskrzypczak](https://www.alfredforum.com/profile/336-mcskrzypczak/?tab=field_core_pfield_12)

index.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import alfy from 'alfy';
4+
import {filterOutput, findFilter, isFileAction} from './src/utils.js';
5+
6+
const {inputWithoutFilter, foundFilter: filter} = findFilter(alfy.input);
7+
const input = inputWithoutFilter.replaceAll('\t', '\n').split('\n');
8+
9+
const options = [
10+
{
11+
prefix: 'Copy File Names',
12+
match: 'copy file name names filenames filename files',
13+
action: filepath => {
14+
const stat = fs.lstatSync(filepath);
15+
if (!stat.isFile()) {
16+
return false;
17+
}
18+
19+
const {name} = path.parse(filepath); //=> "hello"
20+
const {ext} = path.parse(filepath); //=> ".html"
21+
return `${name}${ext}`;
22+
},
23+
},
24+
{
25+
prefix: 'Copy File Names (no extension)',
26+
match: 'copy file name names filenames filename files without no extension',
27+
action: filepath => {
28+
const stat = fs.lstatSync(filepath);
29+
if (!stat.isFile()) {
30+
return false;
31+
}
32+
33+
const {name} = path.parse(filepath); //=> "hello"
34+
return `${name}`;
35+
},
36+
},
37+
{
38+
prefix: 'Copy Folder Names',
39+
match: 'copy folder name names foldernames foldername folders',
40+
action: filepath => {
41+
const stat = fs.lstatSync(filepath);
42+
if (!stat.isDirectory()) {
43+
return false;
44+
}
45+
46+
return path.basename(filepath);
47+
},
48+
},
49+
{
50+
prefix: 'Copy All Names',
51+
match: 'copy all name names allnames',
52+
action: filepath => path.basename(filepath),
53+
},
54+
{
55+
prefix: 'Copy All Names (no file extension)',
56+
match: 'copy all name names allnames without no extension',
57+
action: filepath => {
58+
const stat = fs.lstatSync(filepath);
59+
if (stat.isDirectory()) {
60+
return path.basename(filepath);
61+
}
62+
63+
const {name} = path.parse(filepath); //=> "hello"
64+
return `${name}`;
65+
},
66+
},
67+
];
68+
69+
function run(input) {
70+
if (!isFileAction(input)) {
71+
return [
72+
{
73+
title: 'Input must be valid and existing file(s) or folder(s)',
74+
valid: false,
75+
},
76+
];
77+
}
78+
79+
return options.map(options => {
80+
const files = input.map(filepath => options.action(filepath.trim())).filter(element => Boolean(element));
81+
return {
82+
title: `${options.prefix}: ${files}`,
83+
subtitle: 'Copy to clipboard',
84+
match: options.match,
85+
arg: files.join('\n'),
86+
};
87+
});
88+
}
89+
90+
const output = run(input);
91+
if (output.length > 0) {
92+
alfy.output(filter ? filterOutput(filter, output) : output);
93+
} else {
94+
alfy.output([
95+
{
96+
title: 'Nothing to process…',
97+
subtitle: 'No files selected?',
98+
},
99+
]);
100+
}

0 commit comments

Comments
 (0)