├── .github └── workflows │ └── release.yml ├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── alfred.py ├── builtins.json ├── icon.png ├── info.plist └── pipe.py /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | tags: 6 | - v*.* 7 | 8 | jobs: 9 | release: 10 | runs-on: macos-latest 11 | steps: 12 | - uses: actions/checkout@v3 13 | - name: create-alfredworkflow 14 | run: make 15 | - uses: ncipollo/release-action@v1 16 | with: 17 | artifacts: "*.alfredworkflow" 18 | draft: true 19 | generateReleaseNotes: true 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | ._* 3 | *.py[cod] 4 | *~ 5 | pipe.alfredworkflow 6 | prefs.plist 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2013-2023 Robin Breathe 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | zip -j9 --filesync pipe.alfredworkflow *.{json,plist,png,py} 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pipe transformation workflow for Alfred 2 | 3 | An [Alfred](http://www.alfredapp.com/) workflow enabling easy transformation of the current contents of the clipboard by piping through arbitrary shell one-liners. 4 | 5 | ## Requirements 6 | 7 | - [Alfred](http://www.alfredapp.com/) (version 2.0+) 8 | - The [Alfred Powerpack](http://www.alfredapp.com/powerpack/). 9 | - Python 3 (`xcode-select --install` or [Homebrew](https://brew.sh/)) 10 | 11 | ## Usage 12 | 13 | Trigger the workflow by hotkey or keyword (default=`|`, override with the `keyword` variable) followed by an arbitrarily simple or complex shell one-liner to transform the contents of the clipboard in-place; optionally use the `Cmd`-modifier to immediately paste the results into the foreground app, or the `Alt`-modifier to show the results in large type. 14 | 15 | Two hotkeys are configurable: 16 | 17 | - transform the current contents of the clipboard (like the keyword); recommended hotkey: `Ctrl-Cmd-\` 18 | - transform the current selection in the foreground app; recommended hotkey: `Ctrl-Cmd-C` 19 | 20 | When triggered via hotkey, the leading keyword (e.g. `|`) is not required. 21 | 22 | ### Examples 23 | 24 | - Transform to UPPERCASE: `| perl -nle 'print uc'` or `| tr a-z A-Z` 25 | - Base64 encode: `| base64` 26 | - Base64 decode: `| base64 --decode` 27 | - Top 10 unique lines with counts: `| sort | uniq -c | sort -rn | head -10` 28 | 29 | ### Built-ins 30 | 31 | A number of example pipelines (including those above) are [built-in](https://github.com/isometry/alfred-pipe/raw/master/builtins.json). 32 | 33 | Built-ins can be disabled en-mass by setting the `load_builtins` variable to any value other than `yes`. 34 | 35 | ### Aliases 36 | 37 | To save repetitive typing, custom aliases can be defined with the following syntax: 38 | 39 | `| alias NAME=PIPE | LINE @@@` 40 | 41 | The trailing `@@@` (override with the `alias_terminator` variable) terminates the alias definition and causes it to be saved. 42 | 43 | #### Examples 44 | 45 | - `| alias tac=sed '1!G;h;$!d' @@@` 46 | - `| alias top10=sort | uniq -c | sort -rn | head -10 @@@` 47 | 48 | #### Alias removal 49 | 50 | Any custom alias can be removed with: 51 | 52 | `| alias NAME=@@@` 53 | 54 | ## Contributions & Thanks 55 | 56 | - ctwise 57 | -------------------------------------------------------------------------------- /alfred.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import os 3 | import sys 4 | 5 | from itertools import islice 6 | from xml.etree.ElementTree import Element, SubElement, tostring 7 | 8 | _MAX_RESULTS_DEFAULT = 9 9 | UNESCAPE_CHARACTERS = u"""\\ ()[]{};`'"$""" 10 | 11 | 12 | class Item(object): 13 | def __init__(self, attributes, title, subtitle, icon=None): 14 | self.attributes = attributes 15 | self.title = title 16 | self.subtitle = subtitle 17 | self.icon = icon 18 | 19 | def __str__(self): 20 | return tostring(self.xml(), encoding='unicode') 21 | 22 | def xml(self): 23 | item = Element(u'item', self.attributes) 24 | for attribute in (u'title', u'subtitle', u'icon'): 25 | value = getattr(self, attribute) 26 | if value is None: 27 | continue 28 | SubElement(item, attribute, {}).text = value 29 | return item 30 | 31 | 32 | def args(characters=None): 33 | return tuple(unescape(arg, characters) for arg in sys.argv[1:]) 34 | 35 | 36 | def config(): 37 | return _create('config') 38 | 39 | 40 | def unescape(query, characters=None): 41 | for character in (UNESCAPE_CHARACTERS if (characters is None) else characters): 42 | query = query.replace('\\%s' % character, character) 43 | return query 44 | 45 | 46 | def work(volatile): 47 | path = { 48 | True: os.getenv('alfred_workflow_cache', os.getenv('TMPDIR', '/tmp')), 49 | False: os.getenv('alfred_workflow_data', os.getenv('HOME', '/tmp')) 50 | }[bool(volatile)] 51 | return _create(os.path.expanduser(path)) 52 | 53 | 54 | def write(text): 55 | sys.stdout.write(text) 56 | 57 | 58 | def xml(items, maxresults=_MAX_RESULTS_DEFAULT): 59 | root = Element('items') 60 | for item in islice(items, maxresults): 61 | root.append(item.xml()) 62 | return tostring(root, encoding='unicode') 63 | 64 | 65 | def _create(path): 66 | if not os.path.isdir(path): 67 | os.mkdir(path) 68 | if not os.access(path, os.W_OK): 69 | raise IOError('No write access: %s' % path) 70 | return path 71 | -------------------------------------------------------------------------------- /builtins.json: -------------------------------------------------------------------------------- 1 | { 2 | "base64": "encode base64", 3 | "gzip -c | base64": "encode base64+gzip", 4 | "base64 --decode": "decode base64", 5 | "base64 --decode | gzcat": "decode base64+gzip", 6 | "openssl enc -d -base64 -aes256 -k X": "AES-256 decrypt with passphrase 'X'", 7 | "openssl enc -e -base64 -aes256 -k X": "AES-256 encrypt with passphrase 'X'", 8 | "openssl x509 -noout -fingerprint": "x509 fingerprint", 9 | "openssl x509 -noout -hash": "x509 hash", 10 | "openssl x509 -noout -text": "x509 certificate information", 11 | "perl -ne 'print unless $a{$_}++'": "unsorted unique lines", 12 | "perl -ne 'print unless /^$/'": "remove all blank lines", 13 | "perl -nle 'print lc'": "lowercase", 14 | "perl -nle 'print uc'": "uppercase", 15 | "perl -pe '$_ = \"$. $_\"'": "number all lines", 16 | "python3 -c \"import json,sys; print(json.dumps(json.load(sys.stdin),indent=4,separators=(',',': ')))\"": "reformat JSON", 17 | "sed '1!G;h;$!d'": "reverse lines", 18 | "sed G": "doublespace", 19 | "sort -n -t. -k1,1 -k2,2 -k3,3 -k4,4": "sort IPv4 addresses", 20 | "python3 -c \"import ipaddress,sys; print('\n'.join(str(x) for x in sorted([ipaddress.ip_network(line.strip(), strict=False) for line in sys.stdin])))\"": "sort IPv6 networks", 21 | "sort -u": "sorted unique lines", 22 | "sort | uniq -c | sort -rn | head -10": "top 10 unique lines", 23 | "tr 'A-Za-z' 'N-ZA-Mn-za-m'": "rot13" 24 | } 25 | -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isometry/alfred-pipe/5cd66c3f84fb2ac5fd4f07fadad8263515232c26/icon.png -------------------------------------------------------------------------------- /info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | bundleid 6 | net.isometry.alfred.pipe 7 | category 8 | Tools 9 | connections 10 | 11 | 16039760-F173-4AB8-9C73-DA7401D5DE23 12 | 13 | 14 | destinationuid 15 | 1D296119-4F9D-4FDC-9777-1DA7CF655D19 16 | modifiers 17 | 0 18 | modifiersubtext 19 | 20 | vitoclose 21 | 22 | 23 | 24 | 1D296119-4F9D-4FDC-9777-1DA7CF655D19 25 | 26 | 27 | destinationuid 28 | 9B8A5712-D806-47E3-9045-EC31AB0C6485 29 | modifiers 30 | 0 31 | modifiersubtext 32 | 33 | vitoclose 34 | 35 | 36 | 37 | destinationuid 38 | CB1F4AD1-CC6A-4CE9-9953-5BA92C4B2634 39 | modifiers 40 | 0 41 | modifiersubtext 42 | 43 | vitoclose 44 | 45 | 46 | 47 | 39932D9C-E166-4541-96DD-CB688AC3AC97 48 | 49 | 50 | destinationuid 51 | FA80CA00-3581-44C7-BE72-66A1285D4198 52 | modifiers 53 | 0 54 | modifiersubtext 55 | 56 | vitoclose 57 | 58 | 59 | 60 | 51CD3227-55F9-4AB5-BF83-79C74807C152 61 | 62 | 63 | destinationuid 64 | 16039760-F173-4AB8-9C73-DA7401D5DE23 65 | modifiers 66 | 0 67 | modifiersubtext 68 | 69 | vitoclose 70 | 71 | 72 | 73 | 60EC2263-CC57-4763-8CEA-629C2E53E6EB 74 | 75 | 76 | destinationuid 77 | 74681B4B-CBE4-403F-BDF7-39F4D2319ABD 78 | modifiers 79 | 0 80 | modifiersubtext 81 | 82 | vitoclose 83 | 84 | 85 | 86 | 74681B4B-CBE4-403F-BDF7-39F4D2319ABD 87 | 88 | 89 | destinationuid 90 | 51CD3227-55F9-4AB5-BF83-79C74807C152 91 | modifiers 92 | 524288 93 | modifiersubtext 94 | Display in large type 95 | vitoclose 96 | 97 | 98 | 99 | destinationuid 100 | 16039760-F173-4AB8-9C73-DA7401D5DE23 101 | modifiers 102 | 0 103 | modifiersubtext 104 | 105 | vitoclose 106 | 107 | 108 | 109 | destinationuid 110 | D4DF549F-4556-4AA6-BF1A-159154EB3051 111 | modifiers 112 | 1048576 113 | modifiersubtext 114 | Paste on completion 115 | vitoclose 116 | 117 | 118 | 119 | 8E5E1774-FB5B-47C7-A0E0-AE958708254F 120 | 121 | 122 | destinationuid 123 | 39932D9C-E166-4541-96DD-CB688AC3AC97 124 | modifiers 125 | 0 126 | modifiersubtext 127 | 128 | vitoclose 129 | 130 | 131 | 132 | 9B8A5712-D806-47E3-9045-EC31AB0C6485 133 | 134 | 135 | destinationuid 136 | 8E5E1774-FB5B-47C7-A0E0-AE958708254F 137 | modifiers 138 | 0 139 | modifiersubtext 140 | 141 | vitoclose 142 | 143 | 144 | 145 | A94F5286-0602-4B7F-A5D3-55FDD8AB3217 146 | 147 | 148 | destinationuid 149 | 42BF3CF4-67C1-4182-B7F5-7456DB227F9F 150 | modifiers 151 | 0 152 | modifiersubtext 153 | 154 | vitoclose 155 | 156 | 157 | 158 | CB1F4AD1-CC6A-4CE9-9953-5BA92C4B2634 159 | 160 | 161 | destinationuid 162 | 0FCF109F-A181-4D53-A4DB-DB54DA7440B8 163 | modifiers 164 | 0 165 | modifiersubtext 166 | 167 | vitoclose 168 | 169 | 170 | 171 | D4DF549F-4556-4AA6-BF1A-159154EB3051 172 | 173 | 174 | destinationuid 175 | 16039760-F173-4AB8-9C73-DA7401D5DE23 176 | modifiers 177 | 0 178 | modifiersubtext 179 | 180 | vitoclose 181 | 182 | 183 | 184 | D637E280-521E-432F-85C1-3F58E064A66E 185 | 186 | 187 | destinationuid 188 | 74681B4B-CBE4-403F-BDF7-39F4D2319ABD 189 | modifiers 190 | 0 191 | modifiersubtext 192 | 193 | vitoclose 194 | 195 | 196 | 197 | FF37CF97-11D9-4324-ADDD-6F67B697BD34 198 | 199 | 200 | destinationuid 201 | A94F5286-0602-4B7F-A5D3-55FDD8AB3217 202 | modifiers 203 | 0 204 | modifiersubtext 205 | 206 | vitoclose 207 | 208 | 209 | 210 | 211 | createdby 212 | Robin Breathe 213 | description 214 | Process clipboard contents through shell command 215 | disabled 216 | 217 | name 218 | pipe 219 | objects 220 | 221 | 222 | config 223 | 224 | autopaste 225 | 226 | clipboardtext 227 | {query} 228 | ignoredynamicplaceholders 229 | 230 | transient 231 | 232 | 233 | type 234 | alfred.workflow.output.clipboard 235 | uid 236 | A94F5286-0602-4B7F-A5D3-55FDD8AB3217 237 | version 238 | 3 239 | 240 | 241 | config 242 | 243 | action 244 | 0 245 | argument 246 | 1 247 | focusedappvariable 248 | 249 | focusedappvariablename 250 | 251 | hotkey 252 | 34 253 | hotmod 254 | 1310720 255 | hotstring 256 | C 257 | leftcursor 258 | 259 | modsmode 260 | 0 261 | relatedAppsMode 262 | 0 263 | 264 | type 265 | alfred.workflow.trigger.hotkey 266 | uid 267 | FF37CF97-11D9-4324-ADDD-6F67B697BD34 268 | version 269 | 2 270 | 271 | 272 | config 273 | 274 | externaltriggerid 275 | pipe 276 | passinputasargument 277 | 278 | passvariables 279 | 280 | workflowbundleid 281 | self 282 | 283 | type 284 | alfred.workflow.output.callexternaltrigger 285 | uid 286 | 42BF3CF4-67C1-4182-B7F5-7456DB227F9F 287 | version 288 | 1 289 | 290 | 291 | config 292 | 293 | action 294 | 0 295 | argument 296 | 0 297 | focusedappvariable 298 | 299 | focusedappvariablename 300 | 301 | hotkey 302 | 42 303 | hotmod 304 | 1310720 305 | hotstring 306 | \ 307 | leftcursor 308 | 309 | modsmode 310 | 0 311 | relatedAppsMode 312 | 0 313 | 314 | type 315 | alfred.workflow.trigger.hotkey 316 | uid 317 | D637E280-521E-432F-85C1-3F58E064A66E 318 | version 319 | 2 320 | 321 | 322 | config 323 | 324 | alignment 325 | 0 326 | backgroundcolor 327 | 328 | fadespeed 329 | 0 330 | fillmode 331 | 0 332 | font 333 | 334 | ignoredynamicplaceholders 335 | 336 | largetypetext 337 | {query} 338 | textcolor 339 | 340 | wrapat 341 | 50 342 | 343 | type 344 | alfred.workflow.output.largetype 345 | uid 346 | FA80CA00-3581-44C7-BE72-66A1285D4198 347 | version 348 | 3 349 | 350 | 351 | config 352 | 353 | autopaste 354 | 355 | clipboardtext 356 | {query} 357 | ignoredynamicplaceholders 358 | 359 | transient 360 | 361 | 362 | type 363 | alfred.workflow.output.clipboard 364 | uid 365 | 8E5E1774-FB5B-47C7-A0E0-AE958708254F 366 | version 367 | 3 368 | 369 | 370 | config 371 | 372 | argument 373 | {query} 374 | passthroughargument 375 | 376 | variables 377 | 378 | display 379 | 1 380 | 381 | 382 | type 383 | alfred.workflow.utility.argument 384 | uid 385 | 51CD3227-55F9-4AB5-BF83-79C74807C152 386 | version 387 | 1 388 | 389 | 390 | config 391 | 392 | inputstring 393 | {var:paste} 394 | matchcasesensitive 395 | 396 | matchmode 397 | 1 398 | matchstring 399 | 1 400 | 401 | type 402 | alfred.workflow.utility.filter 403 | uid 404 | 9B8A5712-D806-47E3-9045-EC31AB0C6485 405 | version 406 | 1 407 | 408 | 409 | config 410 | 411 | inputstring 412 | {var:display} 413 | matchcasesensitive 414 | 415 | matchmode 416 | 0 417 | matchstring 418 | 1 419 | 420 | type 421 | alfred.workflow.utility.filter 422 | uid 423 | 39932D9C-E166-4541-96DD-CB688AC3AC97 424 | version 425 | 1 426 | 427 | 428 | config 429 | 430 | concurrently 431 | 432 | escaping 433 | 0 434 | script 435 | pbpaste | {query} 436 | scriptargtype 437 | 0 438 | scriptfile 439 | 440 | type 441 | 5 442 | 443 | type 444 | alfred.workflow.action.script 445 | uid 446 | 16039760-F173-4AB8-9C73-DA7401D5DE23 447 | version 448 | 2 449 | 450 | 451 | config 452 | 453 | alfredfiltersresults 454 | 455 | alfredfiltersresultsmatchmode 456 | 0 457 | argumenttreatemptyqueryasnil 458 | 459 | argumenttrimmode 460 | 0 461 | argumenttype 462 | 0 463 | escaping 464 | 0 465 | keyword 466 | {var:keyword} 467 | queuedelaycustom 468 | 1 469 | queuedelayimmediatelyinitially 470 | 471 | queuedelaymode 472 | 0 473 | queuemode 474 | 1 475 | runningsubtext 476 | Processing… 477 | script 478 | 479 | scriptargtype 480 | 1 481 | scriptfile 482 | pipe.py 483 | subtext 484 | Enter one-liner or alias 485 | title 486 | Pipe clipboard through one-liner 487 | type 488 | 8 489 | withspace 490 | 491 | 492 | type 493 | alfred.workflow.input.scriptfilter 494 | uid 495 | 74681B4B-CBE4-403F-BDF7-39F4D2319ABD 496 | version 497 | 3 498 | 499 | 500 | type 501 | alfred.workflow.utility.junction 502 | uid 503 | 1D296119-4F9D-4FDC-9777-1DA7CF655D19 504 | version 505 | 1 506 | 507 | 508 | config 509 | 510 | autopaste 511 | 512 | clipboardtext 513 | {query} 514 | ignoredynamicplaceholders 515 | 516 | transient 517 | 518 | 519 | type 520 | alfred.workflow.output.clipboard 521 | uid 522 | 0FCF109F-A181-4D53-A4DB-DB54DA7440B8 523 | version 524 | 3 525 | 526 | 527 | config 528 | 529 | availableviaurlhandler 530 | 531 | triggerid 532 | pipe 533 | 534 | type 535 | alfred.workflow.trigger.external 536 | uid 537 | 60EC2263-CC57-4763-8CEA-629C2E53E6EB 538 | version 539 | 1 540 | 541 | 542 | config 543 | 544 | argument 545 | {query} 546 | passthroughargument 547 | 548 | variables 549 | 550 | paste 551 | 1 552 | 553 | 554 | type 555 | alfred.workflow.utility.argument 556 | uid 557 | D4DF549F-4556-4AA6-BF1A-159154EB3051 558 | version 559 | 1 560 | 561 | 562 | config 563 | 564 | inputstring 565 | {var:paste} 566 | matchcasesensitive 567 | 568 | matchmode 569 | 0 570 | matchstring 571 | 1 572 | 573 | type 574 | alfred.workflow.utility.filter 575 | uid 576 | CB1F4AD1-CC6A-4CE9-9953-5BA92C4B2634 577 | version 578 | 1 579 | 580 | 581 | readme 582 | ## Usage 583 | 584 | Trigger the workflow by hotkey or keyword (default=`|`, override with the `keyword` variable) followed by an arbitrarily simple or complex shell one-liner to transform the contents of the clipboard in-place; optionally use the `Cmd`-modifier to immediately paste the results into the foreground app. 585 | 586 | ### Examples 587 | 588 | - Transform to UPPERCASE: `| perl -nle 'print uc'` or `| tr a-z A-Z` 589 | - Base64 encode: `| base64` 590 | - Base64 decode: `| base64 --decode` 591 | - Top 10 unique lines with counts: `| sort | uniq -c | sort -rn | head -10` 592 | 593 | ### Built-ins 594 | 595 | A number of example pipelines (including those above) are [built-in](https://github.com/isometry/alfred-pipe/raw/master/builtins.json). 596 | 597 | Built-ins can be disabled en-mass by setting the `load_builtins` variable to any value other than `yes`. 598 | 599 | ### Aliases 600 | 601 | To save repetitive typing, custom aliases can be defined with the following syntax: 602 | 603 | `| alias NAME=PIPE | LINE @@@` 604 | 605 | The trailing `@@@` (override with the `alias_terminator` variable) terminates the alias definition and causes it to be saved. 606 | 607 | #### Examples 608 | 609 | - `| alias tac=sed '1!G;h;$!d' @@@` 610 | - `| alias top10=sort | uniq -c | sort -rn | head -10 @@@` 611 | 612 | #### Alias removal 613 | 614 | Any custom alias can be removed with: 615 | 616 | `| alias NAME=@@@` 617 | uidata 618 | 619 | 0FCF109F-A181-4D53-A4DB-DB54DA7440B8 620 | 621 | xpos 622 | 700 623 | ypos 624 | 240 625 | 626 | 16039760-F173-4AB8-9C73-DA7401D5DE23 627 | 628 | note 629 | Run the one-liner 630 | xpos 631 | 430 632 | ypos 633 | 180 634 | 635 | 1D296119-4F9D-4FDC-9777-1DA7CF655D19 636 | 637 | xpos 638 | 560 639 | ypos 640 | 210 641 | 642 | 39932D9C-E166-4541-96DD-CB688AC3AC97 643 | 644 | note 645 | display==1 646 | xpos 647 | 850 648 | ypos 649 | 150 650 | 651 | 42BF3CF4-67C1-4182-B7F5-7456DB227F9F 652 | 653 | xpos 654 | 470 655 | ypos 656 | 20 657 | 658 | 51CD3227-55F9-4AB5-BF83-79C74807C152 659 | 660 | note 661 | display=1 662 | xpos 663 | 350 664 | ypos 665 | 150 666 | 667 | 60EC2263-CC57-4763-8CEA-629C2E53E6EB 668 | 669 | xpos 670 | 30 671 | ypos 672 | 250 673 | 674 | 74681B4B-CBE4-403F-BDF7-39F4D2319ABD 675 | 676 | note 677 | Prompt for one-liner or alias 678 | xpos 679 | 190 680 | ypos 681 | 180 682 | 683 | 8E5E1774-FB5B-47C7-A0E0-AE958708254F 684 | 685 | xpos 686 | 700 687 | ypos 688 | 120 689 | 690 | 9B8A5712-D806-47E3-9045-EC31AB0C6485 691 | 692 | note 693 | paste!=1 694 | xpos 695 | 630 696 | ypos 697 | 150 698 | 699 | A94F5286-0602-4B7F-A5D3-55FDD8AB3217 700 | 701 | xpos 702 | 330 703 | ypos 704 | 20 705 | 706 | CB1F4AD1-CC6A-4CE9-9953-5BA92C4B2634 707 | 708 | note 709 | paste==1 710 | xpos 711 | 630 712 | ypos 713 | 270 714 | 715 | D4DF549F-4556-4AA6-BF1A-159154EB3051 716 | 717 | note 718 | paste=1 719 | xpos 720 | 350 721 | ypos 722 | 260 723 | 724 | D637E280-521E-432F-85C1-3F58E064A66E 725 | 726 | note 727 | Transform clipboard 728 | xpos 729 | 30 730 | ypos 731 | 110 732 | 733 | FA80CA00-3581-44C7-BE72-66A1285D4198 734 | 735 | xpos 736 | 920 737 | ypos 738 | 120 739 | 740 | FF37CF97-11D9-4324-ADDD-6F67B697BD34 741 | 742 | note 743 | Transform selection 744 | xpos 745 | 190 746 | ypos 747 | 20 748 | 749 | 750 | userconfigurationconfig 751 | 752 | 753 | config 754 | 755 | default 756 | | 757 | placeholder 758 | 759 | required 760 | 761 | trim 762 | 763 | 764 | description 765 | 766 | label 767 | Keyword 768 | type 769 | textfield 770 | variable 771 | keyword 772 | 773 | 774 | config 775 | 776 | default 777 | 778 | required 779 | 780 | text 781 | Load built-in aliases 782 | 783 | description 784 | 785 | label 786 | Built-ins 787 | type 788 | checkbox 789 | variable 790 | load_builtins 791 | 792 | 793 | config 794 | 795 | default 796 | @@@ 797 | placeholder 798 | 799 | required 800 | 801 | trim 802 | 803 | 804 | description 805 | Magic string used to terminate alias entry 806 | label 807 | Alias terminator 808 | type 809 | textfield 810 | variable 811 | alias_terminator 812 | 813 | 814 | config 815 | 816 | default 817 | 9 818 | placeholder 819 | Positive integer required 820 | required 821 | 822 | trim 823 | 824 | 825 | description 826 | Maximum number of results to show 827 | label 828 | Max. Results 829 | type 830 | textfield 831 | variable 832 | max_results 833 | 834 | 835 | version 836 | 2.1 837 | webaddress 838 | https://github.com/isometry/alfred-pipe 839 | 840 | 841 | -------------------------------------------------------------------------------- /pipe.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # pipe.alfredworkflow, v2.0 4 | # Robin Breathe, 2013-2023 5 | 6 | import alfred 7 | import json 8 | import sys 9 | import os 10 | 11 | from fnmatch import fnmatch 12 | from itertools import chain 13 | from os import path 14 | 15 | DEFAULT_MAX_RESULTS = 9 16 | DEFAULT_ALIAS_TERMINATOR = "@@@" 17 | ALIASES_FILE = "aliases.json" 18 | BUILTINS_FILE = "builtins.json" 19 | 20 | 21 | def fetch_aliases(_path): 22 | file = path.join(alfred.work(volatile=False), _path) 23 | if not path.isfile(file): 24 | return {} 25 | return json.load(open(file, 'r')) 26 | 27 | 28 | def write_aliases(_dict, _path): 29 | file = path.join(alfred.work(volatile=False), _path) 30 | json.dump(_dict, open(file, 'w'), indent=4, separators=(',', ': ')) 31 | 32 | 33 | def define_alias(_dict, definition, alias_file): 34 | if '=' in definition: 35 | (alias, pipe) = definition.split('=', 1) 36 | else: 37 | (alias, pipe) = (definition, '') 38 | 39 | terminator = os.getenv("alias_terminator", DEFAULT_ALIAS_TERMINATOR) 40 | 41 | if not alias: 42 | return alfred.xml([alfred.Item( 43 | attributes={'valid': 'no'}, 44 | title="alias NAME=ARBITRARY-ONE-LINER", 45 | subtitle="Terminate ONE-LINER with '{0}' to save or 'NAME={0}' to delete alias".format(terminator), 46 | icon='icon.png' 47 | )]) 48 | 49 | if pipe and pipe == terminator: 50 | _dict.pop(alias, None) 51 | write_aliases(_dict, alias_file) 52 | return alfred.xml([alfred.Item( 53 | attributes={'valid': 'no', 'autocomplete': ''}, 54 | title="alias {0}={1}".format(alias, pipe), 55 | subtitle='Alias deleted! TAB to continue', 56 | icon='icon.png' 57 | )]) 58 | 59 | if pipe and pipe.endswith(terminator): 60 | pipe = pipe[:-len(terminator)] 61 | _dict[alias] = pipe 62 | write_aliases(_dict, alias_file) 63 | return alfred.xml([alfred.Item( 64 | attributes={'valid': 'no', 'autocomplete': alias}, 65 | title="alias {0}={1}".format(alias, pipe), 66 | subtitle='Alias saved! TAB to continue', 67 | icon='icon.png' 68 | )]) 69 | 70 | return alfred.xml([alfred.Item( 71 | attributes={'valid': 'no'}, 72 | title="alias {0}={1}".format(alias, pipe), 73 | subtitle='Terminate with {0} to save'.format(terminator), 74 | icon='icon.png' 75 | )]) 76 | 77 | 78 | def exact_alias(_dict, query): 79 | pipe = _dict[query] 80 | return alfred.xml([alfred.Item( 81 | attributes={'uid': 'pipe:{}'.format(pipe), 'arg': pipe}, 82 | title=pipe, 83 | subtitle='(expanded alias)', 84 | icon='icon.png' 85 | )]) 86 | 87 | 88 | def match_aliases(_dict, query): 89 | results = [] 90 | for (alias, pipe) in _dict.items(): 91 | if (pipe != query) and fnmatch(alias, '{}*'.format(query)): 92 | results.append(alfred.Item( 93 | attributes={'uid': 'pipe:{}'.format(pipe), 'arg': pipe, 'autocomplete': pipe}, 94 | title=pipe, 95 | subtitle='(alias: {})'.format(alias), 96 | icon='icon.png' 97 | )) 98 | return results 99 | 100 | 101 | def fetch_builtins(_path): 102 | return json.load(open(_path, 'r')) 103 | 104 | 105 | def match_builtins(_dict, query): 106 | results = [] 107 | for (pipe, desc) in _dict.items(): 108 | if fnmatch(pipe, '*{}*'.format(query)) or fnmatch(desc, '*{}*'.format(query)): 109 | results.append(alfred.Item( 110 | attributes={'uid': 'pipe:{}'.format(pipe), 'arg': pipe, 'autocomplete': pipe}, 111 | title=pipe, 112 | subtitle='(builtin: {})'.format(desc), 113 | icon='icon.png' 114 | )) 115 | return results 116 | 117 | 118 | def verbatim(query): 119 | return alfred.Item( 120 | attributes={'uid': 'pipe:{}'.format(query), 'arg': query}, 121 | title=query, 122 | subtitle=None, 123 | icon='icon.png' 124 | ) 125 | 126 | 127 | def complete(): 128 | query = sys.argv[1] 129 | 130 | max_results = int(os.getenv('max_results', DEFAULT_MAX_RESULTS)) 131 | load_builtins = bool(int(os.getenv('load_builtins', '1'))) 132 | 133 | aliases = fetch_aliases(ALIASES_FILE) 134 | builtins = load_builtins and fetch_builtins(BUILTINS_FILE) or {} 135 | 136 | if query.startswith('alias '): 137 | return define_alias(aliases, query[6:], ALIASES_FILE) 138 | 139 | results = [] 140 | 141 | if query not in builtins: 142 | results.append(verbatim(query)) 143 | 144 | results.extend(chain(match_aliases(aliases, query), match_builtins(builtins, query))) 145 | 146 | return alfred.xml(results, maxresults=max_results) 147 | 148 | 149 | if __name__ == '__main__': 150 | print(complete()) 151 | --------------------------------------------------------------------------------